But the following RED CODE is not moving at all. I am using XBee to send the command and I am pretty(100%) 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) { //<-----------this line sure works, not an issue.
} // 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(80); //<-----------------no matter how many seconds, I even tried delay(1000);
}
I have seen some code use move(char L, char R) instead of int. I tried, but doesn't work either.
I can get following string from terminal: a:0:180 d:180:0 w:180:180 s:0:0 z:0:0 x:90:90 w:180:180 a:0:180
Which means it does get the command thru XBee but just not execute it, but if I insert "move(60,60);" in setup(), it does work. I don't know why it doesn't recognize "move(60,60);" which send from XBee.
hardmouse:
if I insert "move(60,60);" in setup(), it does work. I don't know why it doesn't recognize "move(60,60);" which send from XBee.
Perhaps your servos don't respond to settings at the extreme edge of movement - try putting move(60,60) in the move forward case instead of move(180,180). Alternatively, try all your move commands in the switch individually from setup, to see if they do anything.
The servo library doc says that you should call refresh every 50ms. Your sketch does not have it, not sure if this is the issue, might be worth a try though. Note that you will have to rethink your serial.read logic a bit (or put the refresh in the while loop).
equals a high possibility of no serial comms if you send more than 1 or 2 characters at a time. Neither the delay or the "< 1" are necessary. It doesn't appear to be the source of your current issue, but it IS an issue.
wildbill:
The servo library doc says that you should call refresh every 50ms. Your sketch does not have it, not sure if this is the issue, might be worth a try though. Note that you will have to rethink your serial.read logic a bit (or put the refresh in the while loop).
AWOL:
There hasn't been a 'refresh' method for quite a few versions now.
What happens if you eliminate the XBee, and just the serial monitor to drive the serial line?
Your switch doesn't have a default case - may be an idea to see if non-printing characters are screwing things up.
Yes, it will work. Like the green code up there without XBee.
I do have default before but it doesn't matter either way.
wildbill:
Perhaps your servos don't respond to settings at the extreme edge of movement - try putting move(60,60) in the move forward case instead of move(180,180). Alternatively, try all your move commands in the switch individually from setup, to see if they do anything.
ok, works now. they were outta range and I finally narrow it down to 20~150. When you met such problems you can only keep trying to find out the range.