Let's Make Robots!

Arduino code question and problem.

Can anyone help me to take a look of the code following? CODE 1 works fine to have motor runs forward and backward:

#include <Servo.h>
Servo motorL;
Servo motorR;
int pos = 0;
void setup() {
motorL.attach(9);
motorR.attach(10);
Serial.begin(9600);
}

void loop() {
for(pos = 0; pos < 180; pos += 1){
move(pos, pos);
delay(80);
}
for(pos = 180; pos>=1; pos-=1){
move(pos, pos);
delay(80);
}
}

void move(int L, int R){
Serial.print(L);
Serial.print(":");
Serial.println(R);
 motorL.write(L);
motorR.write(R);
}

But the following is not moving at all. I am using XBee to send the command and I am pretty sure arduino gets the command but just no movement.

#include <Servo.h>
Servo motorL;
Servo motorR;
int pos = 0;
void setup() {
motorL.attach(9);
motorR.attach(10);
Serial.begin(9600);
}
void loop() {
while (Serial.available() < 1) {
} // Wait until a character is received
char val = Serial.read();
Serial.print(val);
Serial.print(":");
switch(val){
case 'w'://Move Forward
move(180,180);
break;
case 's'://Move Backwards
move(0,0);
break;
case 'a'://Turn Left
move(0,180);
break;
case 'd'://Turn Right
move(180,0);
break;
case 'x'://Stop
move(90,90);
break;
}
delay(30);
}

void move(int L, int R){
Serial.print(L);
Serial.print(":");
Serial.println(R);
motorL.write(L);
motorR.write(R);
}

Any ideas?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Yes i did. changed loop to if statement, char to int and (Serial.available() < 1) to (Serial.available() > 0)

 

Try it and tell me.

 

Edit: Add  }   before void move.