1
votes

i am trying to send char's from processing to arduino, but arduino only recognizes 2 of them,

the problem is with the '2' char

when i press the 's' key the processing code is sending the '2' char because i can see the arduino rx led lighting but the motor does nothing,

with the '1' or '0' chars i have no problem, i switched the '2' in the arduino code to correspond to drive_forward and then to drive_reverse, but the one that had '2' char assigned to it did not work in both cases, as i said the '1'and '0' char are sent and received well

i guess it is something in the arduino code, but i don't know what

arduino code:

int motor1 = 4;
int motor2 = 5;
char val; 

// --------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);

// Setup motors

pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);

}


// ---------------------------------------------------------------- Loop
void loop() { 
  if (Serial.available()>0) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
  if (val == '2'){
       drive_forward();
        }
  if (val == '1'){
      drive_reverse();
                  }
  if (val == '0'){
  motor_stop();

                  }

}

// --------------------------------------------------------------------------- Drive

void motor_stop(){

digitalWrite(motor1, LOW); 
digitalWrite(motor2, LOW);


}
void drive_forward(){

digitalWrite(motor1, HIGH); 
digitalWrite(motor2, LOW); 
delay(15);
digitalWrite(motor1, LOW); 
digitalWrite(motor2, LOW);
delay(15);


}
void drive_reverse(){

digitalWrite(motor2, HIGH); 
digitalWrite(motor1, LOW); 
delay(15);
digitalWrite(motor2, LOW); 
digitalWrite(motor1, LOW);
delay(15);


}

processing code:

import processing.serial.*;

Serial myPort;  


void setup() 
{
  size(200,200); 
  myPort = new Serial(this, Serial.list()[2], 9600);


}
void draw() {





  } 
void keyPressed() {
    if (key == 'w' || key == 'W')
    { 
      myPort.write('1');
    println("1");}
      if (key == 's' || key == 'S')
    { 
      myPort.write('2');
    println("2");}
}
void keyReleased() {
myPort.write('0');
println("0");

}

1
Unlikely to be the issue, but you should initialize val to '0'. - struthersneil
Have you tried usuing the Arduino serial monitor? - ladislas
only one program can use the com port, so if i use the serial monitor i will not be able to connect the processing program to the arduino and see what exactly it is receiving, because something it is as i see the RX led lights up when i am trying to send the '2' char from processing - dranca darius
Try using readChar(), instead of read(). Also try using print(), instead of write(), but I'm not sure if that's correct. - The Guy with The Hat
try sending those strings as integers, it seems to be working better for me - tailedmouse

1 Answers

1
votes

As @tailedmouse said, send data as integer.

Processing code:

//skipped some code
void keyPressed() {
    if (key == 'w' || key == 'W') {
        myPort.write(1); 
    }

    if (key == 's' || key == 'S') {
        myPort.write(2);
        println("2");
    }
} 

void keyReleased() {
    myPort.write(0);
    println("0");
}

Arduino code:

//skipped some code.
void loop() {
    if (Serial.available()>0) {
        // If data is available to 
        read, val = Serial.read(); 
        // read it and store it in val 
    }

    if (val == 2) {
        drive_forward();
    }

    if (val == '1') {
        drive_reverse();
    }

    if (val == '0') {
        motor_stop();
    }
}