Just so that I don't start a whole new thread, I'll ask my question on here.
I'm trying to set up a Processing interface for my serial controlled RC car. I've (tried) to base the code off of the SimpleWrite example in Processing. When I run the program, I get no response at all from my arduino. Maybe it's a loop problem?
Processing
import controlP5.*;
import processing.serial.*;
ControlP5 controlP5;
Serial arduino;
PFont myriad;
int Speed = 0;
char function;
void setup()
{
size(400, 400);
myriad = loadFont("MyriadPro-Regular-25.vlw");
textFont(myriad, 25);
String portName = Serial.list()[0];
arduino = new Serial(this, portName, 9600);
controlP5 = new ControlP5(this);
controlP5.addSlider("Speed", 0, 255, 0, 50, 25, 255, 12);
}
void draw()
{
background(150, 0, 0);
fill(0, 0, 150);
text(Speed, 5, 40);
//arduino.write(function + Speed);
if(mouseOverForward() == true)
{
fill(0, 0, 255);
function = 'f';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(150, 75, 100, 100);
if(mouseOverReverse() == true)
{
fill(0, 0, 255);
function = 'r';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(150, 200, 100, 100);
if(mouseOverRight() == true)
{
fill(0, 0, 255);
function = 'a';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(275, 150, 40, 80);
if(mouseOverLeft() == true)
{
fill(0, 0, 255);
function = 'b';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(85, 150, 40, 80);
if((mousePressed == true) && (mouseX >= 25) && (mouseX <= 75) && (mouseY >= 325) &&
(mouseY <= 375))
{
fill(0, 0, 255);
function = 'c';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(25, 325, 50, 50);
if((mousePressed == true) && (mouseX >= 90) && (mouseX <= 140) && (mouseY >= 325) &&
(mouseY <= 375))
{
fill(0, 0, 255);
function = 'd';
arduino.write(function + Speed);
}
else
{
fill(0, 0, 150);
function = 'e';
arduino.write(function + Speed);
}
rect(90, 325, 50, 50);
}
boolean mouseOverForward()
{
return ((mouseX >= 150) && (mouseX <= 250) && (mouseY >= 75) && (mouseY <= 175));
}
boolean mouseOverReverse()
{
return ((mouseX >= 150) && (mouseX <= 250) && (mouseY >= 200) && (mouseY <= 300));
}
boolean mouseOverRight()
{
return ((mouseX >= 275) && (mouseX <= 315) && (mouseY >= 150) && (mouseY <= 230));
}
boolean mouseOverLeft()
{
return ((mouseX >= 85) && (mouseX <= 125) && (mouseY >= 150) && (mouseY <= 230));
}
Arduino
#include <AFMotor.h>
String readString, function, mspeed;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
const int rightLEDs = 9;
const int leftLEDs = 10;
const int buttonPin = 2;
const int buzzerPin = A0;
int buttonState = 0;
int ledState = HIGH;
void setup()
{
pinMode(rightLEDs, OUTPUT);
pinMode(leftLEDs, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(rightLEDs, HIGH);
digitalWrite(leftLEDs, HIGH);
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
delay(1);
if (Serial.available() > 0)
{
char c = Serial.read();
readString += c;
}
}
if(readString.length() > 0)
{
function = readString.substring(0, 1);
mspeed = readString.substring(1, 4);
if(function == 'f') function = "FORWARD";
if(function == 'r') function = "REVERSE";
if(function == 'a') function = "RIGHT_TURN";
if(function == 'b') function = "LEFT_TURN";
if(function == 'c') function = "HEADLIGHTS";
if(function == 'd') function = "HORN";
if(function == 'e') function = "RELEASE";
word newSpeed;
char carray1[9];
mspeed.toCharArray(carray1, sizeof(carray1));
newSpeed = atoi(carray1);
buttonState = digitalRead(buttonPin);
if(buttonState == LOW)
{
if(function == "FORWARD")
{
motor1.setSpeed(newSpeed);
motor2.setSpeed(newSpeed);
motor3.setSpeed(newSpeed);
motor4.setSpeed(newSpeed);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
if(function == "REVERSE")
{
motor1.setSpeed(newSpeed);
motor2.setSpeed(newSpeed);
motor3.setSpeed(newSpeed);
motor4.setSpeed(newSpeed);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
if(function == "RIGHT_TURN")
{
motor1.setSpeed(newSpeed);
motor2.setSpeed(newSpeed);
motor3.setSpeed(newSpeed);
motor4.setSpeed(newSpeed);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
if(function == "LEFT_TURN")
{
motor1.setSpeed(newSpeed);
motor2.setSpeed(newSpeed);
motor3.setSpeed(newSpeed);
motor4.setSpeed(newSpeed);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
if(function == "HEADLIGHTS")
{
ledState = !ledState;
digitalWrite(rightLEDs, ledState);
digitalWrite(leftLEDs, ledState);
}
if(function == "HORN")
{
analogWrite(buzzerPin, 255);
}
if(function == "RELEASE");
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
}
}
}