Detecting multiple keypresses in arduino processing

Hi... I am making a robotic car and want to make a program in arduino processing that can detect inputs for forward backward right and left commands and send a comination to com port for the desired action. also when multiple keys are pressed (eg. forward and right) it should send a combination for the combined command to com port. arduino will be connected to the com port.

Hi... I am making a robotic car and want to make a program in arduino processing that can detect inputs for forward backward right and left commands and send a comination to com port for the desired action. also when multiple keys are pressed (eg. forward and right) it should send a combination for the combined command to com port. arduino will be connected to the com port.

want to make a program in arduino processing

What do you think "arduino processing" is? There is an Arduino IDE and a Processing IDE. They are completely unrelated.

I think you'll need to pick different keys for forward and right, forward and left, etc.

Here is one example that just uses single key presses.

/*
simple control test
*/

volatile int gear = 150; //default speed First Gear

volatile char val;         // variable to receive data from the serial port

byte M1L = 3;              // PWM left drive motor 
byte M2L = 5;              // PWM left drive motor
byte M1R = 9;              // PWM right drive motor
byte M2R = 6;              // PWM right drive motor
byte ledpin = 13;          // LED connected to pin 2 (on-board LED)

/*IMPORTANT*/
//M1 => High & M2 => Low: forward movement
//M1 => Low & M2 => High: Backward movement

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(M1L, OUTPUT);                                
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps
  
  Serial.println("Commands, Press * to bring up help menu");
}

void loop() {
  if( Serial.available() )       // if data is available to read
  { 
    digitalWrite(ledpin, HIGH);
    val = Serial.read();
    volatile int skewGear = gear/2;
    if(val == '1')
    {
      Serial.println("First Gear ");
      gear = 150;
    }
    else if (val == '2')
    { 
      Serial.println("Second Gear ");
      gear = 200;
    }
    else if (val == '3')
    { 
      Serial.println("Third Gear ");
      gear = 255;
    }


    if( val == 'W' || val == 'w' )//forwards               
    {
      Serial.println("Moving Forwards"); 
      //      Left Motor     :     Right Motor
      analogWrite(M1L, gear); 
      analogWrite(M1R, gear);
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                 
    }
    else if( val == 'S' || val == 's' )//backwards              
    {
      Serial.println("Moving Backwards");
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, gear); 
      analogWrite(M2R, gear);                   
    }
    else if( val == 'A' || val == 'a' )//Left         
    {
      Serial.println("LEFT");
      digitalWrite(M1L, LOW); 
      analogWrite(M1R, gear);
      analogWrite(M2L, gear); 
      digitalWrite(M2R, LOW);                      
    }
    else if( val == 'D' || val == 'd' )              
    {
      Serial.println("RIGHT");   
      analogWrite(M1L, gear);
      digitalWrite(M1R, LOW);
      digitalWrite(M2L, LOW); 
      analogWrite(M2R, gear);                 
    }
    else if( val == 'T' || val == 't' )              
    {
      Serial.println("Skew Left Forward");   
      analogWrite(M1L, skewGear); 
      analogWrite(M1R, gear);
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                  
    }
    else if( val == 'Y' || val == 'y' )              
    {
      Serial.println("Skew Right Forward");  
      analogWrite(M1L, gear); 
      analogWrite(M1R, skewGear); 
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                  
    }
    else if( val == 'G' || val == 'g' )              
    {
      Serial.println("Skew Left Backward");   
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, skewGear); 
      analogWrite(M2R, gear);                  
    }
    else if( val == 'H' || val == 'h' )              
    {
      Serial.println("Skew Right Backward");   
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, gear); 
      analogWrite(M2R, skewGear);                  
    }
    else if(val == ' ' || val == 'x' || val == 'X'){  
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW); 
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);
    }
    else if(val == '*'){
      Help();
    }    
  }
  else{
    digitalWrite(ledpin, LOW);
  }
}

void Help() {
  Serial.println("W = Forward");
  Serial.println("S = Backward");
  Serial.println("A = Left");
  Serial.println("D = Right");
  Serial.println("T = Skew Forward Left");
  Serial.println("Y = Skew Forward Right");
  Serial.println("G = Skew Backward Left");
  Serial.println("H = Skew Backward Right");
  Serial.println("Space Bar = All stop");
  return; 
}

Im working on the multiple key press code.

If I understood your post correctly you are using the Processing language www.processing.org running on a PC to read your key presses and send commands to your Arduino. This is very doable.

The Processing reference and forums here Reference / Processing.org would be helpfull to you. In general you will use the keyPressed() and keyReleased() functions to read the keys and use a boolean variable for each key to keep track of whether it is down or up. Then at a given interval your program can look at what keys are down, decide what command to send to the Arduino, and use the Serial library to send the command to the serial port.

From there you will need to make a separate Arduino sketch that reads the serial data and does whatever it is that needs doing. This should be quite doable, so give it a try and post back with questions.

@jroorda yes u got it right... but i dont understand how to use keyPressed()... i mean if im checking for forward so wud it be like
if(keyPressed('w'))
{
...
}
or if im checking forward + right
if(keyPressed('w') && keyPressed('d'))
{
...
}
i mean what wud be the syntax cuz in the example on that web page they used it as a function

Oh, your actually using the Processing IDE to read your keys then send them to your arduino.

Your code will look more like this. Link HERE

// Click on the window to give it focus,
// and press the 'B' key.

void draw() {
  if (keyPressed) {
    if (key == 'b' || key == 'B') {
      fill(0);
    }
  } else {
    fill(255);
  }
  rect(25, 25, 50, 50);
}

@mzeeque, do not cross-post. Threads merged.

@HazardsMind thanks mann... @Coding Badly sorry for that...