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.

However, I think it did get the command correctly, otherwise it won't show:

w:180:180

d:180:0

s:0:0 and so on...it knows I typein "w", so it execute move(180,180) and "d" for move(180,0)....

I've never done this before, but this example shows that you should use the variable type int for the switch statement.

// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character.  For 
// example 'a' = 97, 'b' = 98, and so forth 

Yeh, the "switch" statement works only on ordinal types; but the char-type is one: it is a number pointing to the relevant character in the charset; so it's perfectly fine to use or refer to these numbers as a quoted character in a switch statement. (In most cases, is much easier to read and maintain than a long if-else-if, too.)

If you want to test a single variable for a multiple of different values / situations; a switch/case is usually the way to go. It'll effectively do things with less CPU ticks: an "if" would be unnecessary comparing the same variable again and again, while the switch would only look at it (fetch it) once.

Depending on usage cases, switch-case also has a neat "fall-through" capability that allows to branch into more than 1 case, (something that an "if" cannot do that easily), which can be terribly useful in some situations...

This whole time you have been assuming that the x-bees are working. Did you configure them? Have you done any tests to isolate the x-bees themselves to be sure they are working?

Yeah, I thought of that too, but those data are from Xbee. If Xbee is not working it won't get something like "W:180:180..." etc. I have use same XBee module in other machine and they are working fine. So, I am pretty sure they both XBee working individually. The problem maybe somewhere in the code. I will try "if...else" code instead of "switch..case" tonight.

you could replace your case statement with a group of nested if statements to make sure everything else is/is not functioning. Also, were it my choice I would pick your original code over Jad-Berro's as your code would wait for input rather than compute the outcome of an if statement endlessly. One more thing, how are you getting w: 180:180 as output when your code does not show that it ever prints the servo commands?

That's the thing I don't get it. The motors does received the command but not execute the command but if I direct input it without XBee it works as the first code. It just doesn't like the command from XBee.

It still the same result:

w:180:180

d:180:0

s:0:0

a:0:180

w:180:180

d:180:0

s:0:0

a:0:180

w:180:180

... It does read the signal and looks good but motors are just not moving. It's very strange the green code works but our method doesn't. The result format looks exactly the same. I had a feeling it must be a tiny bug when we figure out the cause.

This should work

#include
Servo motorL;
Servo motorR;
int pos = 0;
void setup()  {
  motorL.attach(9);
  motorR.attach(10);
  Serial.begin(9600);
}
void loop()  {
  if (Serial.available() > 0) {
  int 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){
  motorL.write(L);
  motorR.write(R);
}

Did you change anything in the code?

P.S.Sorry for the first line should be "#include <Servo.h>"