Arduino code - switch statement

Hi, I code a program for my autonomous robot. I am sending numbers from 0 to 8 from Raspberry pi to Arduino through Serial communication. I have Rasp code. But arduino code has some problems. Please can you tell me how to write it best? I want to use switch statement to choose direction for my robot/motors. But when I compile that code to arduino on robot - my robot start doing only default commands. Please why????

 

void loop() { 

  if(Serial.available()){

    char smer = Serial.read();

      switch (smer) {

        case 0:

         lmbrake = 1;

         rmbrake = 1;

         Serial.println("stop");

         Motors();

         break; 

        case 1:

         lmspeed = 255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println("rovno");

         Motors();

         break; 

        case 2:

         lmspeed = 50;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println("otocit -> 2, lavohore");

         Motors();

         delay(400);

         break; 

        .

  .

  .

  . 

        case 8:

         lmspeed = 255;

         rmspeed = 50;

         lmbrake = 0;

         rmbrake = 0;

         Serial.println("otocit -> 8, pravohore");

         Motors();

         delay(400);

         break;

        default:

          lmspeed = 255;

         rmspeed = 255;

         lmbrake = 0;

         rmbrake = 0;

         Motors();

         break; 

       }

    }

}

switch debug

Try putting  { Serial.print("smer = "); Serial.println(smer);} before the switch statement to display the value being used…

 

**Return char **
Hi, thanks for response, I change a littlebit my code and now I have char as options in switch. But I don’t know if I can return char instead of returning integer, in function in raspberry pi to receive that char in arduino :slight_smile: can you help me if this is possible?

You are probably sending

You are probably sending from the Pi values that are between 0 and 8 in ASCII not in decimal or base 10.  Your tests are based on the decimal or base 10 value, not on the ASCII values.  

http://www.ascii-code.com/ 

has the table of values.

For instance, ‘0’  is equal to decimal 48.

char value = 48;

char value2 = ‘0’ ;

if (value == ‘0’)

printf(“statement is true”);

In this case, the if statement would be true.  Also, value and value2 would be equal.  If you change your values in your switch from 0 to ‘0’ through 8 ‘8’ and it begins to work, that will be the reason.  

Good luck.

Regards,

Bill

 

 

thanks!

yees! I am so stupid that I forget for this easy mistake :/…thanks :slight_smile:

rasp code

I have one question…But I have a code in raspberry pi, in which I measure sonar values - distance and send char to arduino - but in that function (on rasp) I’m returning INT (integer). Is that a problem? I think yes, how can I solve that?

Can I return in rasp functions char - and send it to arduino and recieve char - ‘0’ and it will work

OR

Can I return in rasp functions int - and send it to arduino and recieve it like number I want and it will work (for example - near robot isn’t any obstacle so robot can move forward - Rasp send 1, Arduni recieve it and in function switch it read that robot have to go forward. 

 

So it is char vs int, which I have to use and where please? :slight_smile:

No worries. Easy mistake

No worries.  Easy mistake to make.

Regards,

Bill

Code below
Hi, please can you look to my question below ? :slight_smile: thanks