Go Down

Topic: Problem with switch case!! (Read 788 times) previous topic - next topic

Sporerodas

Hi!
My arduino project is about phone controlled robots. I'm using bluetooth to connect my phone to a bluetooth hc-06 module. I think that I'm going to use a switch case statement. I'm new at programming, and I wonder if you can say like this:

case 'U': {
          if case'U'&&'R'
          motor1.run(BACKWARD);
          motor2.run(FORWARD);
          motor3.run(BACKWARD);
          motor4.run(FORWARD);
          motor1.setSpeed(255);
          motor2.setSpeed(255);
          motor3.setSpeed(255);
          motor4.setSpeed(255);
          if case 'U'&&'L'
          motor1.run(FORWARD);
          motor2.run(BACKWARD);
          motor3.run(FORWARD);
          motor4.run(BACKWARD);
          motor1.setSpeed(255);
          motor2.setSpeed(255);
          motor3.setSpeed(255);
          motor4.setSpeed(255);
          }break;

I want arduino to first check if a 'U' is comming, and then check if  'U'  is comming with a 'R' or 'L' and do different things depending to wich of the cases i'm sending out.

Arduino says this:

Arduino:1.6.9 (Windows 7), Board:"Arduino Nano, ATmega328"

WARNING: Spurious .github folder in 'Adafruit Motor Shield library' library
In function 'void loop()':

bluetooth_controlled_car_4_motors:121: error: expected '(' before 'case'

           if case'U'&&'R'

              ^

bluetooth_controlled_car_4_motors:130: error: expected '(' before 'case'

           if case 'U'&&'L'

              ^

exit status 1
expected '(' before 'case'


I have adafruit AF motor shield library.
Thanks for helping me!


PaulS

Code: [Select]
          if case'U'&&'R'
What the heck is this? You are IN a case ('U') already. You can not also be in case 'R', unless you do something like:
Code: [Select]

   switch(whatever)
   {
       case 'U':
       case 'R':
           // do some stuff
           break;
       // other cases
   }
The art of getting good answers lies in asking good questions.

Sporerodas

'U' is backlights on, 'L' is spin left and 'R' is spin right. I mean that if the backlights is on and the spin left button (or right) is pressed, should the motors drive like in the code above.

PaulS

Quote
'U' is backlights on
No! 'U' means to turn the back light on. Once that happen, the character is discarded, so you can not say that it means anything.

Quote
I mean that if the backlights is on
You need to remember that you have turned them on, completely independent of what serial data is arriving.

I really do not understand why the motors should behave differently if the LCD backlight is on or off.
The art of getting good answers lies in asking good questions.

Sporerodas

Is it impossible to write that the direction of the motor change if the backlights is turned on and if I press the 'L' or 'R' button?

jremington

#5
Oct 16, 2016, 04:38 pm Last Edit: Oct 16, 2016, 04:39 pm by jremington
Quote
Is it impossible to write that the direction of the motor change if the backlights is turned on and if I press the 'L' or 'R' button?
Of course not. You merely need to write a program to do that. The program needs to remember if the backlight is on or off and act accordingly.

Sporerodas

Can you recommend a YouTube tutorial or website that can show me how to do that program you are talking about?

jremington

I recommend to learn the C/C++ programming language. Then you can write any program.

There are many on-line tutorials for C/C++.

Go Up