Using Processing to send serial data to Arduino

Hi. I am trying to control two DC motors using an Arduino Mega 2560 and an adafruit motorshield v1. I want to be able to control the motors using the 'w' 'a' 's' and 'd' keys. I stripped the code down to get one motor working in both directions just to keep it simple for noe. Once I accomplish this, I will add the other motor.

The Arduino code alone works fine. When I upload the code and open the serial monitor up, I am able to control the direction of my motor with the 'W' and 'X' keys (Currently I am using the 'S' key to release the motor, but I will change that after I successfully implement the code with processing.) Now when I try to run the processing code I was expecting that it would send data through the serial port to Arduino to get the motors to run without the Arduino serial monitor. The idea is to have it where if I hold the 'w' key down, the motor will run forward. When I release the key the motor will stop.

I hope someone with knowledge on the subject can take a look at the code and point out where I am going wrong.

Arduino Code

#include <AFMotor.h> //Include adafruit motorshield V1 library

//Select which terminal M!, M2, M3, or M4. Depends on motor wiring
AF_DCMotor TestMotor(4);

char val; //Data recieved from serial port
int incomingByte; //a variable to read incoming serial data

void setup() {
  Serial.begin(9600); //set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v1 -DC Motor Test.");
  
 //Set the speed to start, from 0 (off) (max speed)
  TestMotor.setSpeed(200);
  
}
  
 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 W is pressed, run motor forward
    if (incomingByte == 'W' || incomingByte == 'w') {
      TestMotor.setSpeed(200);
      TestMotor.run(FORWARD);
      }
   //If "X" is pressed, run motor backwards
   if (incomingByte == 'X' || incomingByte == 'x') {
      TestMotor.setSpeed(200);
      TestMotor.run(BACKWARD);
      }
    if (incomingByte == 'S' || incomingByte == 's') {
      TestMotor.run(RELEASE);
    }
 }
 }

Processing Code:

import processing.serial.*;   
      
//use the Serial library

Serial myPort;                      
//Create object from Serial class  



int  standBy = 0; 




void setup() { 
  
size(200, 200);                                  //set window size
  
String portName = Serial.list()[0];              //list serial ports
  
myPort = new Serial(this, portName, 9600);       //initialize the serial port object

}




void draw() {                   //this is like the 'loop' section of code on Arduino
  
background(255);                   

}



void keyPressed() { 

                                
//if the key is "W"
    
if (  key == 'W' ){      
//Forward Movement
  
myPort.write(key);  
  
println("Forward");     
    
}

                
//if the key is "X"
    
if (  key == 'X' ){      
//Reverse Movement
  
myPort.write(key);  
  
println("Reverse");     
    
}

                
  
}
  


void keyReleased() {


    
if (  key == 'W' ){
    
myPort.write(standBy);
  
println("RC Car is on stand-by.");
    
}

 
   
if (  key == 'X' ){
    
myPort.write(standBy);
  
println("RC Car is on stand-by.");
    
}
     
 
}

Kcaines:
The idea is to have it where if I hold the 'w' key down, the motor will run forward. When I release the key the motor will stop.

I'm not familiar with Processing but if I was doing that with Python I would detect when the w is pressed and separately when it is released.
When it is pressed I would send a character to the Arduino to start the motor. When it is released I would send another character to tell the Arduino to stop the motor.

The idea of sending a stream of characters while a button is pressed is a non-starter.

...R

THANK YOU SO MUCH.

That wasn't the main question of this topic, but that did cover my next biggest issue.
The original problem I had was processing wasn't sending data to the serial port. I have yet to find the exact cause of the problem, however, I did manage to find some old processing code that I found off the internet that successfully communicated through the serial port. As I said, I haven't fully analyzed why my code wasn't working just yet, but will get to that some other time. In the mean time, I've edited the code I found online to serve my purpose.

The next issue I had was with trying to get the key release function to work, and after checking back on the thread, I see you have answered my question and I thank you for that.

Btw the project is to control an RC car using my computer:
The next stage of this project is to be able to send multiple characters to the serial port at one time, so if I wanted to control the car to make a forward, left turn, all I would have to do is press 'w' and 'a' together. I saw a thread discussing this earlier so I will reference back to it if I can find it. I'll also upload my code for anybody else in the future that is trying to get this to work.

For the hardware I am using an Arduino Mega 2560 with an adafruit v1 motorshield.
The arduino code is using the adafruit v1 motorshield library

Arduino Code:

#include <AFMotor.h> //Include adafruit motorshield V1 library

//Select which terminal M!, M2, M3, or M4. Depends on motor wiring
AF_DCMotor ThrottleMotor(4); //In this case, Throttle Motor is connect to terminal 4
AF_DCMotor SteeringMotor(1); //In this case, Steering Motor is connected to terminal 1

char val; //Data recieved from serial port
int incomingByte; //a variable to read incoming serial data

void setup() {
  Serial.begin(9600); //set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v1 -DC Motor Test.");
  
 //Set the speed to start, from 0 (off) (max speed)
  ThrottleMotor.setSpeed(200);
  SteeringMotor.setSpeed(200);
  
}
  
 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 serial reads W or w, car moves forwards
    if (incomingByte == 'W' || incomingByte == 'w') {
      ThrottleMotor.setSpeed(200);
      ThrottleMotor.run(FORWARD);
      }
   //If serial reads S or s, car moves backwards backwards
   if (incomingByte == 'S' || incomingByte == 's') {
      ThrottleMotor.setSpeed(200);
      ThrottleMotor.run(BACKWARD);
      }
   //If serial reads A or a, car makes forward left turn
   if (incomingByte == 'A' || incomingByte == 'a') {
      ThrottleMotor.setSpeed(200);
      SteeringMotor.setSpeed(200);
      ThrottleMotor.run(FORWARD);
      SteeringMotor.run(FORWARD);
      }
   //If serial reads D or d, car makes forward right turn
   if (incomingByte == 'D' || incomingByte == 'd') {
      ThrottleMotor.setSpeed(200);
      SteeringMotor.setSpeed(200);
      ThrottleMotor.run(FORWARD);
      SteeringMotor.run(BACKWARD);
      }
    //If serial reads X or x, all motors are released
    if (incomingByte == 'X' || incomingByte == 'x') {
      ThrottleMotor.run(RELEASE);
      SteeringMotor.run(RELEASE);
  }
 }
 }

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, portName, 9600);       //initialize the serial port object
}

void draw() {                    //this is like the 'loop' section of code on Arduino
  background(255);           // set background color
}
void keyPressed() {
//if a key is pressed, do the following
    if (  key == 'w' || key == 'W'  ){  myPort.write(key);  println(key);  }   // if the key is f or F, write it to the serial port
    if (  key == 's' || key == 'S'  ){  myPort.write(key);  println(key);  }   // if the key is s or S, write it to the serial port
    if (  key == 'a' || key == 'A'  ){  myPort.write(key);  println(key);  }   // if the key is a or A, write it to the serial port
    if (  key == 'd' || key == 'D'  ){  myPort.write(key);  println(key);  }   // if the key is d or D, write it to the serial port
}
void keyReleased() {
//if a key is pressed, do the following
    if (  key == 'w' || key == 'W'  ){  myPort.write('x');  println(key);   }   // When w or W is released, write x to serial port
    if (  key == 's' || key == 'S'  ){  myPort.write('x');  println(key);   }   // When s or S is released, write x to serial port
    if (  key == 'a' || key == 'A'  ){  myPort.write('x');  println(key);   }   // When a or A is released, write x to serial port
    if (  key == 'd' || key == 'D'  ){  myPort.write('x');  println(key);   }   // When d or D is released, write x to serial port
}

The next stage of this project is to be able to send multiple characters to the serial port at one time, so if I wanted to control the car to make a forward, left turn, all I would have to do is press 'w' and 'a' together

Why not just send the two characters one after the other? Assuming you have different characters for keyDown and keyUp the Arduino will know to keep going with wDown as well as aDown.

...R