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?

This should

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®;
}

Did you change anything in

Did you change anything in the code?

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

Yes i did. changed loop to

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.

It still the same

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.

Just for fun …

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

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.

You sure about those X-bees?

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,

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.

Case statement

I believe the case label has to reduce to an integer. If that is not happening, it won’t do anything. It just may be that the letters aren’t being seen as integers.

I’ve never done this before,

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 

Thanks for input:

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)…

Another possibility why it fails

After thinking and thinking thru whole night, what may cause this problem? I guess there’s another possible reason that XBee keep sending something behind. So, when it gets either “w”,“s” or “a” command and so on, it also gets some signal other than those character right after so it become something else. It might actually execute “w” for million second but it gets another signal very quick right asfter, so the “w” command had been overwrite by something else and that’s probablly why it won’t move. Could it be?? I will add “default” tonight and see how it goes.

I noticed your green code…

has the delay() right after move(). Try putting the delay() in the same place in your red code and see if that makes a difference; although, I am liking your thought on extra data being sent.

I also noticed…

You dropped your delay from 80 ms to 30 ms. Could it be that your motors just need more time to react?

That’s cool! didn’t think of

That’s cool! didn’t think of that way to detect.Even I have delay(30) after switch case, I will add extra delay after move(); also extend the time to maybe 2 sec and see if that was the problem. Can’t wait to go home and try! Gotta to get my tank moving!!

yeah, I will extend that and

yeah, I will extend that and see. Thanks!

I’m still on the X-bees…

I would set-up a simple loop and just be super-sure that your data is even getting through…

I would start with:

if serial.available > 0

data_received=serial.read();

serial.println(data_recieved);

loopy-loop

Open up a terminal and start sending charicters. If you get back what you sent, great! Move on to the next step. If not, well…

When things don’t work, strip code down to bare essentials and test. One piece at a time…

Yeah, tried. It’s working to

Yeah, tried. It’s working to response what I typed.

Oh my God! I don’t know

Oh my God! I don’t know which part goes wrong. None of method working below! XBee did get signal from each other but motors are not moving. It just doesn’t listen to the commands from XBee. Guess I need miracle now.