Hi guys, I'm new here so any help would be greatly appreciated.
I've got a bit of a problem with an rc car which i am controlling through Processing and can't tell where it needs to be fixed.
So I wired each individual control on the remote (up, down, left, right) to its own pin on the board and when I send the pin LOW it runs. (When the control is grounded it triggers and the car moves.)
While it does work I'm having a bit of a problem with Processing and was hoping someone might be a genius in that department. I have WSAD set up as my controls but say i hit right and up at the same time triggering the car to go forward and turn right, when I let go of right, the car will stop even though I'm still holding down the up key.
I've added my code to see what you think and like I said anything would be greatly appreciated! Thank you.
Arduino Code:
const int right = 13;
const int left = 12;
const int reverse = 11;
const int forward = 10;
const int forwardLeft = 10 + 12;
const int forwardRight = 10 + 13;
const int reverseLeft = 11 + 12;
const int reverseRight = 11 + 13;
int incomingByte;
void setup() {
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
if (incomingByte == 'w') {
digitalWrite(forward, LOW);
}
if (incomingByte == 'w + a' {
digitalWrite(forwardLeft, LOW);
}
if (incomingByte == 'w + d') {
digitalWrite(forwardRight, LOW);
}
if (incomingByte == 's') {
digitalWrite(reverse, LOW);
}
if (incomingByte == 's + a') {
digitalWrite(reverseLeft, LOW);
}
if (incomingByte == 's + d') {
digitalWrite(reverseLeft, LOW);
}
if (incomingByte == 'a') {
digitalWrite(left, LOW);
}
if (incomingByte == 'd') {
digitalWrite(right, LOW);
}
if (incomingByte == 'A') {
digitalWrite(forward, HIGH);
}
if (incomingByte == 'A') {
digitalWrite(reverse, HIGH);
}
if (incomingByte == 'A') {
digitalWrite(left, HIGH);
}
if (incomingByte == 'A') {
digitalWrite(right, HIGH);
}
}
}
and Processing code:
import processing.serial.*; //use the Serial library
Serial myPort; // Create object from Serial class
void setup() {
size(200, 200); //set window size to 200 by 200 pixels
String portName = Serial.list()[0]; //list serial ports, save the first one as portName
myPort = new Serial(this, Serial.list()[15], 9600); //initialize the serial port object
}
void draw() { //this is like the 'loop' section of code on Arduino
background(255); // set background color
if(keyPressed) { //if a key is pressed, do the following
if ( key == 'W' || key == 'w' ){ println("FORWARD"); myPort.write(key); println(key); }
}
if(keyPressed) { //if a key is pressed, do the following
if ( key == 'S' || key == 's' ){ println("REVERSE"); myPort.write(key); println(key); }
}
if(keyPressed) { //if a key is pressed, do the following
if ( key == 'A' || key == 'a' ){ println("LEFT"); myPort.write(key); println(key); }
}
if(keyPressed) { //if a key is pressed, do the following
if ( key == 'D' || key == 'd' ){ println("RIGHT"); myPort.write(key); println(key); }
}
else {
println("OFF");
myPort.write('A'); // Send "L" over serial to set LED to LOW
}
}