// initial setup————————————————————————————————————————————-
import processing.serial.*; //set up serial comunication
String Status=""; //location for any information to be sent back to the GUI from the robot
Serial myPort; //initializes variable "myPort"
int speed = 90; //initializes variable "speed" (this is the value sent to the robot)
int speedLocation = 500; //initializes variable "speedLocation" (this is the location of the indicator on the computer)
void setup() {
size(1300, 1000); //this creates the size of the entire window
myPort = new Serial(this, "COM5", 38400); // Starts the serial communication
myPort.bufferUntil('\n'); // sets the ending character for seriel communication. We are using /n (this new line character)
}
void serialEvent (Serial myPort) { // Checks for available data in the Serial Port
Status = myPort.readStringUntil('\n'); //Reads the data sent from the Arduino "Status" variable
}
//————————————————————————————————————————————————————
//everything under the following function is drawing the screen
//background sets the background color. This can be done with background(ammount of black) or background(red, green blue)
//fill determines the color of the next shapes. Color is determined the same way as the background function
//stroke() determines the color of the outline the same way background does.
//strokeWeight() determines the thickness of the outline
//rect(x coordinate, y coordinate, width, height, filet) draws a rectangle with the given parameters
//textsize(size) sets the font size using a single integer
//text("text", x coordinate, y coordinate) write the "text" with the left top most point at the given coordinate value
void draw() {
background(150); // grey color
fill(200, 0, 0); // red color
stroke(33); // light grey color
strokeWeight(1); // thin outline
rect(400, 50, 200, 200, 10); // forward 2 sec
rect(400, 275, 200, 200, 10); // back 2 sec
rect(625, 162, 200, 200, 10); // right 90 degrees
rect(175, 162, 200, 200, 10); // left 90 degrees
rect(25, 500, 950, 10, 10); //seperation rectangle
rect(400, 550, 200, 200, 10); // forward
rect(400, 775, 200, 200, 10); // back
rect(625, 662, 200, 200, 10); // right
rect(175, 662, 200, 200, 10); // left
rect(1100, 50, 150, 900); //speed control bar
fill(60, 60, 60); // changes color
rect(1100, speedLocation, 150, 100); // speed indication box
fill(255); // white color for text
textSize(32); // font size is 32
text("forward", 442, 125);
text("2", 490, 150);
text("seconds", 438, 175);
text("backward", 428, 350);
text("2", 490, 375);
text("seconds", 438, 400);
text("right", 688, 238);
text("90", 705, 269);
text("degrees", 667, 294);
text("left", 248, 238);
text("90", 255, 269);
text("degrees", 217, 294);
//bottom controller
textSize(32);
text("forward", 442, 662);
text("backward", 428, 885);
text("turn", 692, 740);
text("right", 688, 770);
text("turn", 242, 740);
text("left", 248, 770);
//speed control
text(str(speed), 1150, speedLocation+60);
textSize(30);
textSize(16);
text("Program made by Logan Whitaker\nHardware by Kevin Bartchlett\nu.osu.edu/howtorobotbluetooth/", 800, 30);
//————————————————————————————————————————————————————
//the rest of the code determines the reaction to buttons being pressed
//————————————————————————————————————————————————————
//forward 2 seconds was pressed and writes the integer 1 to the arduino via Bluetooth and the program waits 2 seconds before doing anything else
if (mousePressed && mouseX>400 && mouseX<600 && mouseY>50 && mouseY<250) {
myPort.write(1);
delay(2000);
}
// back 2 seconds was pressed and writes the integer 2 to the arduino via Bluetooth and the program waits 2 seconds before doing anything else
if (mousePressed && mouseX>400 && mouseX<600 && mouseY>275 && mouseY<475) {
myPort.write(2);
}
//turn 90 degrees right was pressed and writes the integer 3 to the arduino via Bluetooth and the program waits 2 seconds before doing anything else
if (mousePressed && mouseX>625 && mouseX<825 && mouseY>162 && mouseY<362) {
myPort.write(3); // Sends the character '3'
delay(2000);
}
//turn left 90 degrees was pressed and writes the integer 4 to the arduino via Bluetooth and the program waits 2 seconds before doing anything else
if (mousePressed && mouseX>175 && mouseX<376 && mouseY>162 && mouseY<362) {
myPort.write(4); // Sends the character '4'
delay(2000);
}
// if no buttons are pressed the program defaults back to 0 to show no movement
if (!mousePressed) {
myPort.write(0);
}
//————————————————————————————————————————————————————
//forward was pressed and writes the integer 1 to the arduino via Bluetooth and flashes a rectangle with no fill to make the box look highlighted
if (mousePressed && mouseX>400 && mouseX<600 && mouseY>550 && mouseY<750) {
myPort.write(1);
stroke(0, 0, 255);
strokeWeight(5);
noFill();
rect(400, 550, 200, 200, 10);
}
// back was pressed and writes the integer 2 to the arduino via Bluetooth and flashes a rectangle with no fill to make the box look highlighted
if (mousePressed && mouseX>400 && mouseX<600 && mouseY>775 && mouseY<975) {
myPort.write(2);
stroke(0, 0, 255);
strokeWeight(5);
noFill();
rect(400, 775, 200, 200, 10);
}
//turn right was pressed and writes the integer 3 to the arduino via Bluetooth and flashes a rectangle with no fill to make the box look highlighted
if (mousePressed && mouseX>625 && mouseX<825 && mouseY>662 && mouseY<862) {
myPort.write(3);
stroke(0, 0, 255);
strokeWeight(5);
noFill();
rect(625, 662, 200, 200, 10);
}
//turn left was pressed and writes the integer 4 to the arduino via Bluetooth and flashes a rectangle with no fill to make the box look highlighted
if (mousePressed && mouseX>175 && mouseX<376 && mouseY>662 && mouseY<862) {
myPort.write(4);
stroke(0, 0, 255);
strokeWeight(5);
noFill();
rect(175, 662, 200, 200, 10);
}
//———————————————————————————————————————————————————–
//speed control sees exactly where the mouse clicked and sends a value between 5 and 180 to the arduino to designate the new speed
if (mousePressed && mouseX>1100 && mouseX<1250 && mouseY>100 && mouseY<900) {
speed = ((mouseY-76)*175/796);
myPort.write(speed);
speedLocation= mouseY-50;
}
}