A programming question

Hello guys, i’m new to arduino so if u c any tereble mistakes, take it eazy :slight_smile:
so i’m trying to drive a car with motor driver, and it soposte to have 2 modes (auto and manual), so i have put the modes in one switch case and defenetly i did it wrong, if someone could help me how to do the right way and explain, plz. And sry about my english.

this is the all code

  #include <NewPing.h>


#define DIRECTIONA 4
#define MOTORA 2

#define DIRECTIONB 7
#define MOTORB 3


#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

int led_red=13;
int led_green=8;


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance;

void setup() {
 
  pinMode(led_green,OUTPUT);
  pinMode(led_red, OUTPUT);
  pinMode (MOTORA, OUTPUT);
  pinMode (DIRECTIONA, OUTPUT);
  pinMode (MOTORB, OUTPUT);
  pinMode (DIRECTIONB, OUTPUT);
  Serial.begin(115200);
  delay(100);
  Serial.println("Press any key to continue");
  while(Serial.available()==0);
  digitalWrite(led_red, HIGH);
}
void loop() {
  if (Serial.available()){                        
      char input = Serial.read();                    
      switch (input){
          case 'auto':
                if (distance >20){
                     analogWrite(MOTORA, 180);
                     analogWrite(MOTORB, 180);
                     digitalWrite(DIRECTIONA,0);
                     digitalWrite(DIRECTIONB,0);
                     digitalWrite(led_green,HIGH);
                     digitalWrite(led_red,LOW);
                     Serial.print(" All clear, going fast ");
         
                }
  
                else if (distance >=10 ){
                     analogWrite (MOTORA, 120);
                     analogWrite (MOTORB, 120);
                     digitalWrite (DIRECTIONA, 0); 
                     digitalWrite (DIRECTIONB, 0); 
                     digitalWrite (led_green,HIGH);
                     digitalWrite (led_red,LOW);
                     Serial.print(" Something on the way, slow down ");
       
                 }//if
       
                 else if (distance>5 && distance < 10){ // turning
                       analogWrite (MOTORA, 180);
                       analogWrite (MOTORB, 180);
                       digitalWrite (DIRECTIONA, 1); 
                       digitalWrite (DIRECTIONB, 0); 
                       digitalWrite (led_green,HIGH);
                       digitalWrite (led_red,LOW);
                       Serial.print(" Something is in front of me, turning ");
        
       
       
                  } 
                  else if (distance < 5 ){
                       analogWrite (MOTORA, 0);
                       analogWrite (MOTORB, 0);
                       delay(2000);
                       analogWrite (MOTORA, 120);
                       analogWrite (MOTORB, 120);
                       digitalWrite (DIRECTIONA, 1); 
                       digitalWrite (DIRECTIONB, 1); 
                       delay(5000);
                       digitalWrite (DIRECTIONA, 0);
                       digitalWrite (DIRECTIONB, 1);
                       digitalWrite (led_green,LOW);
                       digitalWrite (led_red,HIGH);
                       Serial.print(" No ather way, going back and turning ");
                       break;
        
                  }
           case 'manual':
                  
                  
                  switch (input){
 
    
                     case 's': // go up
                        digitalWrite (DIRECTIONA, 1); 
                        digitalWrite (DIRECTIONB, 1); 
                        analogWrite (MOTORA, 180);
                        analogWrite (MOTORB, 180);
                                                break;
      
                     case 'a': // turn left
     
                        digitalWrite (DIRECTIONA, 1); 
                        digitalWrite (DIRECTIONB, 0); 
                        analogWrite (MOTORA, 100);
                        analogWrite (MOTORB, 100);
      
                          break;

                      case 'w': 
                          digitalWrite (DIRECTIONA, 0); 
                          digitalWrite (DIRECTIONB, 0); 
                          analogWrite (MOTORA, 180);
                          analogWrite (MOTORB, 180);
     
                            break;

                      case 'd': //turn right
                            digitalWrite (DIRECTIONA, 0); 
                            digitalWrite (DIRECTIONB, 1); 
                            analogWrite (MOTORA, 100);
                            analogWrite (MOTORB, 100);
      
                             break;
      
                      case 'q': // stop
                            analogWrite (MOTORA, 0);
                            analogWrite (MOTORB, 0); 
                      
                           
                            break;  
                   break;
      
                  } // end of switch   
           }   
     } 
}

this is the auto mode

  switch (input){
          case 'auto':
                if (distance >20){
                     analogWrite(MOTORA, 180);
                     analogWrite(MOTORB, 180);
                     digitalWrite(DIRECTIONA,0);
                     digitalWrite(DIRECTIONB,0);
                     digitalWrite(led_green,HIGH);
                     digitalWrite(led_red,LOW);
                     Serial.print(" All clear, going fast ");
         
                }
  
                else if (distance >=10 ){
                     analogWrite (MOTORA, 120);
                     analogWrite (MOTORB, 120);
                     digitalWrite (DIRECTIONA, 0); 
                     digitalWrite (DIRECTIONB, 0); 
                     digitalWrite (led_green,HIGH);
                     digitalWrite (led_red,LOW);
                     Serial.print(" Something on the way, slow down ");
       
                 }//if
       
                 else if (distance>5 && distance < 10){ // turning
                       analogWrite (MOTORA, 180);
                       analogWrite (MOTORB, 180);
                       digitalWrite (DIRECTIONA, 1); 
                       digitalWrite (DIRECTIONB, 0); 
                       digitalWrite (led_green,HIGH);
                       digitalWrite (led_red,LOW);
                       Serial.print(" Something is in front of me, turning ");
        
       
       
                  } 
                  else if (distance < 5 ){
                       analogWrite (MOTORA, 0);
                       analogWrite (MOTORB, 0);
                       delay(2000);
                       analogWrite (MOTORA, 120);
                       analogWrite (MOTORB, 120);
                       digitalWrite (DIRECTIONA, 1); 
                       digitalWrite (DIRECTIONB, 1); 
                       delay(5000);
                       digitalWrite (DIRECTIONA, 0);
                       digitalWrite (DIRECTIONB, 1);
                       digitalWrite (led_green,LOW);
                       digitalWrite (led_red,HIGH);
                       Serial.print(" No ather way, going back and turning ");
                       break;
        
                  }

and the manual mode

  case 'manual':
                  
                  
                  switch (input){
 
    
                     case 's': // go up
                        digitalWrite (DIRECTIONA, 1); 
                        digitalWrite (DIRECTIONB, 1); 
                        analogWrite (MOTORA, 180);
                        analogWrite (MOTORB, 180);
                                                break;
      
                     case 'a': // turn left
     
                        digitalWrite (DIRECTIONA, 1); 
                        digitalWrite (DIRECTIONB, 0); 
                        analogWrite (MOTORA, 100);
                        analogWrite (MOTORB, 100);
      
                          break;

                      case 'w': 
                          digitalWrite (DIRECTIONA, 0); 
                          digitalWrite (DIRECTIONB, 0); 
                          analogWrite (MOTORA, 180);
                          analogWrite (MOTORB, 180);
     
                            break;

                      case 'd': //turn right
                            digitalWrite (DIRECTIONA, 0); 
                            digitalWrite (DIRECTIONB, 1); 
                            analogWrite (MOTORA, 100);
                            analogWrite (MOTORB, 100);
      
                             break;
      
                      case 'q': // stop
                            analogWrite (MOTORA, 0);
                            analogWrite (MOTORB, 0); 
                      
                           
                            break;  
                   break;
      
                  } // end of switch
switch (input){
          case 'auto':

A character called 'auto' ?

      char input = Serial.read();                    
      switch (input){
          case 'auto':

You're reading one character and then checking against a string, which is even defined in single quotes.

Change the case to a single character:

  case 'a':

and

  case 'm':

In the manual case you probably have to read another character first before checking the input variable in another switch statement, but that depends on what the PC is sending on the serial interface (you didn't tell us yet).

what do you mean by "what the pc is sending"?
i use the WIFI shield, which is comunicating wiyh arduino by serial port (rx/tx)

i send a chars like (manual, auto, and w,a,s,d)

"manual" and "auto" are not chars and they are not even strings or Strings until you make them so.

so you r taling me to put instend like one letter, for example "q"... did i undrestud right?

In a switch statement you will only be able to compare single characters or integer/float numbers.
You cannot use a string as a case value unless you want to compare the strings pointer ( C++ subtleties ).

A different method must be used to compare two strings as each character must be compared individually using a loop or helper function like strcmp().

And no, do not use:

case "a":

Use:

case 'a':

Or drop the switch and compare "auto" in a loop. Or use a switch on the result of strcmp().