This video demonstrates another breakthrough with the Garden Weeding Robot that allows the user to wirelessly control the robot via a graphic interface. Here is the code used in both Arduino and Processing.
Arduino Code
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
char val; // variable to receive data from the serial port
void setup() {
Serial.begin(9600); // start serial communication at 9600bps
motor1.setSpeed(100); // set the speed to 100
motor2.setSpeed(100); // set the speed to 100
motor3.setSpeed(100); // set the speed to 100
motor4.setSpeed(100); // set the speed to 100
}
void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in ‘val’
}
if( val == ‘F’ ) // if ‘F’ was received, move robot forward
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
if( val == ‘B’ ) // if ‘B’ was received, move robot backward
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
if( val == ‘R’ ) // if ‘R’ was received, turn robot to the right
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
if( val == ‘L’ ) // if ‘L’ was received, turn robot to the left
{
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
if( val == ‘J’ ) // if ‘J’ was received, stop the robot
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
}
Here’s the code for Processing:
//import class to set up serial connection with wiring board
import processing.serial.*;
Serial port;
color currentcolor;
CircleButton circle1, circle2, circle3, circle4;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
size(400, 400);
smooth();
color baseColor = color(102);
currentcolor = baseColor;
// List all the available serial ports in the output pane.
// You will need to choose the port that the Wiring board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Wiring board is connected to (in this case 1
// which is the second open port in the array)
// Make sure to open the port at the same speed Wiring is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
// Define and create circle button
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
circle1 = new CircleButton(95, 110, 24, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(204);
highlight = color(153);
circle2 = new CircleButton(170, 110, 24, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(153);
highlight = color(102);
circle3 = new CircleButton(130, 150, 24, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(140);
highlight = color(112);
circle4 = new CircleButton(130, 75, 24, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(102);
highlight = color(51);
rect1 = new RectButton(113, 90, 40, buttoncolor, highlight);
}
void draw()
{
background(162);
stroke(255);
update(mouseX, mouseY);
circle1.display();
circle2.display();
circle3.display();
circle4.display();
rect1.display();
}
void update(int x, int y)
{
if(locked == false) {
circle1.update();
circle2.update();
circle3.update();
circle4.update();
rect1.update();
}
else {
locked = false;
}
if(mousePressed) {
if(circle1.pressed()) {
currentcolor = circle1.basecolor;
port.write(‘L’);
}
else if(circle2.pressed()) {
currentcolor = circle2.basecolor;
port.write(‘R’);
}
else if(circle3.pressed()) {
currentcolor = circle3.basecolor;
port.write(‘B’);
}
else if(circle4.pressed()) {
currentcolor = circle3.basecolor;
port.write(‘F’);
}
else if(rect1.pressed()) {
currentcolor = rect1.basecolor;
port.write(‘J’);
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
boolean overCircle(int x, int y, int diameter)
{
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
}
else {
return false;
}
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overCircle(x, y, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
ellipse(x, y, size, size);
}
}
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
Both sets of code were pieced together from other examples, tutorials and a lot of trial and error….so there’s likely a more efficient way to write it. The next goal is to post the Processing graphic interface on a website and control the robot from any internet enabled computer in the world.

