Switch case problem

Hello,

I’m a novice Arduino user and I have been struggling with this code for a couple of days now. So, I have a robot equipped with a pixy camera which is supposed to search for colored balls (Pink, Orange, Yellow, Green). I have already used pixy cam’s software to save the output of colors and I can use the signals sent by the cam on the serial monitor (Pink = 1, Orange = 2, Yellow = 3, Green = 4). In case the cam doesn’t detect any colors, the robot has to search around itself autonomously and approach the balls in a specific order (1 → 2 → 3 → 4). For now, all I could do is to give the robot some basic instructions like in case a = 1 go forward and so on. I want to know if I can use multiple values with a switch case function to make the robot go near the balls as close as possible (the value I used was b = 150, near enough for me). I tried a couple of things as you can see in my code but couldn’t find a solution.

Here’s my code :

#include <motor.h>
#include <SPI.h>
#include <Pixy.h>
Pixy pixy;

// left and right motors with corresponding pins
Motor leftMotor(leftMotorPwmPin, leftMotorDirPin);
Motor rightMotor(rightMotorPwmPin, rightMotorDirPin);

// global variables
int uL = 0;
int uR = 0;
int leftInc = 10;
int rightInc = 10;
int c = 0;


// ************** SETUP *********************
void setup() {
 Serial.begin(9600);
 pixy.init();
 //search();
}
// *******************************************

void search()//search
{
 Serial.println("search");
 leftMotor.setU(-200);
 rightMotor.setU(200);
 delay(500);
}

void searchmid()//search
{
 Serial.println("midsearch");
 leftMotor.setU(-50);
 rightMotor.setU(50);
 delay(500);
}

void forward()//forward
{
 Serial.println("fwd");
 leftMotor.setU(100);
 rightMotor.setU(100);
 delay(200);
}

void right()//turn right
{
 Serial.println("right");
 leftMotor.setU(101);
 rightMotor.setU(-100);
 delay(200);
}

void left()//turn left
{
 Serial.println("left");
 leftMotor.setU(-100);
 rightMotor.setU(100);
 delay(200);
}

void condstop()//stop
{
 Serial.println("stop");
 leftMotor.stop();
 rightMotor.stop();
}

// *************** LOOP **********************
void loop() {
 static int i = 0;
 int a = 0;
 int b = 0;

 int j;
 uint16_t blocks;
 blocks = pixy.getBlocks();
 char buf[32];

 if (blocks)
 {
   i++;
   if (i % 50 == 0)
   {
     sprintf(buf, "Detected %d:\n", blocks);
     Serial.print(buf);
     for (j = 0; j < blocks; j++)
     {
       sprintf(buf, "  block %d: ", j);
       Serial.print(buf);
       pixy.blocks[j].print();


       //////////////////////////////////
       a = pixy.blocks[0].signature;
       b = pixy.blocks[0].width;
       Serial.print("\n");
       Serial.print(a);
       Serial.print("\n");
       Serial.print("\n");
       Serial.print(b);
       Serial.print("\n");

       /*if (c == 0)
         {
         search();
         c++;
         }
         else */

       /*
               switch (true)
               {
                 case a == 1 && c == 0:
                   if (b < 150)
                   {
                     forward();
                   }
                   else
                     c++;
                   break;
                 case a == 2 && c == 1:
                   right();
                   c++;
                   break;
                 case a == 3 && c == 2:
                   left();
                   c++;
                   break;
                 case a == 4 && c == 3:
                   condstop();
                   c = 0;
                   break;
               }*/
       //////////////////////////////////

       switch (a)
       {
         case 1:
           forward();
           break;
         case 2:
           right();
           break;
         case 3:
           left();
           break;
         case 4:
           condstop();
           break;
         default:
           search();
       }
     }
   }
 }
}
// *******************************************

The first thing to do in order to get help is to read read this before posting a programming question and post your code in code tags to make it easier to copy to an editor.

The second thing you do is Google "c switch statement" and get a clue about the proper syntax for a switch statement! You can't just make up your own syntax and hope it works. What you're doing is not even close to right, and shows you have not even bothered to look at a single example!

Regards,
Ray L.

I have tried a lot of different things and I searched on the internet too, I know that the switch case I've put in comments (/* */) is not right but I wanted to know how to do something like that. It is not because I didn't bother to search but because I couldn't find a solution to my problem. I'm sorry if I didn't make myself clear. I was just hopping to get some pointers to see where I went wrong with my code.

I can't actually tell from that code what you're trying to achieve.

But if it involves testing multiple conditions it's usually easier just to use if/else rather than switch/case. Give it a try. And few comments here and there might also help e.g. variable names a, b and c don't exactly clarify what's going on.

Steve

This will do what you seemed to want to do in your bogus switch:

        if (c == 0)
        {
          search();
          c++;
        }
        else
          switch (a*10+c)
          {
            case 10:  // This will never happen since c != 0.
              if (b < 150)
              {
                forward();
              }
              else
                c++;
              break;
              
            case 21:
              right();
              c++;
              break;
              
            case 32:
              left();
              c++;
              break;
              
            case 43:
              condstop();
              c = 0;
              break;
          }
      }
    }
  }
}
// *******************************************