i am trying to send char's from processing to arduino, but arduino only recognizes 2 of them, i don't know were the problem is
when i press the 'w' i can see the arduino rx led lighting but the motor does nothing, 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_inpoi();
}
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_inpoi(){
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");
}