2

Author Topic: problem with switch case statement  (Read 1896 times)

0 Members and 1 Guest are viewing this topic.

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
problem with switch case statement
« on: June 11, 2010, 05:36:02 AM »
Hi

Well I just got my IkeaBot working and now I'm on improving it.
Basically an object avoider, but for now only looking right on front of him.
What I am trying to do is to get it scanning the room and avoid object smartly (means : get left if the object is right etc)

when i verify my code, arduino tells me that I got a duplicate case value. Well, it's not true ( or I'm really really blind cause I checked it many times).
I looked on the arduino forum but no one seems to ever had my problem. Is there a limit to the number of case I can have?

Here's my code :
Code: [Select]
// * Ikeabot - Robot avoiding objects V1.2 11JUN10 * //


#include <Servo.h>

const int sharpPin=5; // set n of the pin
const int ledPin=2;

Servo leftservo;
Servo rightservo;
Servo scanservo;
int distance=0;
int threshold=0;
int leftspeed=0;
int rightspeed=0;
int ScanPos=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;


void setup() {
  pinMode(sharpPin, INPUT); //set the pin of sharpIR and servo
  pinMode(ledPin, OUTPUT);
  scanservo.attach(7);
  ScanPos=90;
  scanservo.write(ScanPos);
  delay(2000); // calibrate
  setThreshold() ;
  leftservo.attach(5);     
  rightservo.attach(6);
  ScanPos=0;

}

void setThreshold() {  //set the threshold value
  a=analogRead(sharpPin);
  delay(40);
  b=analogRead(sharpPin);
  delay(40);
  c=analogRead(sharpPin);
  delay(40);
  d=analogRead(sharpPin);
  delay(40);
  e=analogRead(sharpPin);
  threshold=(a+b+c+d+e)/5;
}

void loop()
{
  for (ScanPos=0 ; ScanPos<180 ; ScanPos+=10) {
    scanservo.write(ScanPos);
    delay (40);
    distance=analogRead(sharpPin);


    if (distance > threshold)  {          // If no object in the way

      digitalWrite(ledPin, HIGH);
      leftspeed=180;
      rightspeed=180;
      leftservo.write(leftspeed);      // go forward
      rightservo.write(rightspeed);
    }
    else {
      digitalWrite(ledPin, LOW);
      switch (ScanPos) {
      case '0':
        leftspeed=100;
        rightspeed=90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        break;

      case '10' :
        leftspeed = 110;
        rightspeed= 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case'20' :
        leftspeed = 120;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '30' :
        leftspeed = 130;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '40' :
        leftspeed = 140;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '50' :
        leftspeed = 150;
        rightspeed = 80;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '60' :
        leftspeed = 160;
        rightspeed = 70;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '70' :
        leftspeed = 170;
        rightspeed = 60;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '80' :
        leftspeed = 170;
        rightspeed = 40;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '90' :
        leftspeed = 180;
        rightspeed = 0;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '100' :
        leftspeed = 40;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay(1000);
        break;

      case '110' :
        leftspeed = 60;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '120' :
        leftspeed = 70;
        rightspeed = 160;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

        case '130' ;
        leftspeed = 80;
        rightspeed = 150;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '140' :
        leftspeed = 90;
        rightspeed = 140;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '150' :
        leftspeed = 90;
        rightspeed = 130;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '160' :
        leftspeed = 90;
        rightspeed = 120;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '170' :
        leftspeed = 90;
        rightspeed = 110;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '180' :
        leftspeed = 90;
        rightspeed = 100;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;
      }
    }
  }
 
  if (ScanPos = 180) {
    ScanPos = 0;
    scanservo.write(ScanPos);
    delay(90);
  }
}






Ty for your help

PS : the delays are still waiting to be experimented.
May the Achtuche be with you...

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: problem with switch case statement
« Reply #1 on: June 11, 2010, 06:15:43 AM »
Code: [Select]
case '130' ;

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: problem with switch case statement
« Reply #2 on: June 11, 2010, 06:33:10 AM »
oups. I fixed that. However, this is not the source of my problem as :
  • compiler stop at case '110'
  • compiler stop at case '120' if I change 110 for 115
  • I just corrected it and got the same error

Seems really weird to me.
May the Achtuche be with you...

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: problem with switch case statement
« Reply #3 on: June 11, 2010, 07:33:43 AM »
Well, it seems I found the problem : compiler thinks that case '10' and case '110 are the same ( etc for 20 and 120, 30 and 130, ...)
I just replaced my code with that :
Code: [Select]
else {
      digitalWrite(ledPin, LOW);
      switch (ScanPos/10) {               // divide ScanPos value by 10
      case '0':
        leftspeed=100;
        rightspeed=90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        break;

      case '1' :
        leftspeed = 110;
        rightspeed= 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case'2' :
        leftspeed = 120;
        rightspeed = 90;
        leftservo.write(leftspeed);
        ...

and it compiled. Seems like you can't put 3 digit numbers into switch case. Really strange.
Btw, will ScanPos/10 work ? I can put math in the switch case () ?

Ty
May the Achtuche be with you...

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: problem with switch case statement
« Reply #4 on: June 11, 2010, 07:57:54 AM »
change

Code: [Select]
case '10':
to

Code: [Select]
case 10:
single quotes are used for characters, your using integers.

The compiler probably gave you a warning like

Code: [Select]
warning: multi-character character constant
because single quote must be used on a single character.

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: problem with switch case statement
« Reply #5 on: June 12, 2010, 03:38:18 AM »
Ok I tried as you say, and it compilled. But it doesn't really work as it should.

I got my sharp + scan servo scanning. But my wheels go as if my sharp always sees an object : it execute each case of the switch case. And when there's actually an object in front of the robot, then I got it turning away. But then my scan servo goes mad.

I double checked the wiring with my multimeter and can't figure what's going wrong. I change for (scanpos/10) ... case '3', '4', ... : there I got my robot turning around as if there was ALWAYS a object in front of it, but the scan servo is actually scanning.

I don't get it. Perhaps it simple but I really don't know what to do.

Perhaps get away of this switch case thing for a simple unique equation, but can't get it.

If you got any advice, you're welcome ;-)

May the Achtuche be with you...

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: problem with switch case statement
« Reply #6 on: June 12, 2010, 06:55:09 AM »
Code: [Select]
// * Ikeabot - Robot avoiding objects V1.2 11JUN10 * //


#include <Servo.h>

const int sharpPin=5; // set n of the pin
const int ledPin=2;

Servo leftservo;
Servo rightservo;
Servo scanservo;
int distance=0;
int threshold=0;
int leftspeed=0;
int rightspeed=0;
int ScanPos=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;


void setup() {
  pinMode(sharpPin, INPUT); //set the pin of sharpIR and servo
  pinMode(ledPin, OUTPUT);
  scanservo.attach(7);
  ScanPos=90;
  scanservo.write(ScanPos);
  delay(2000); // calibrate
  setThreshold() ;
  leftservo.attach(5);    
  rightservo.attach(6);
  ScanPos=0;

}

void setThreshold() {  //set the threshold value
  a=analogRead(sharpPin);
  delay(40);
  b=analogRead(sharpPin);
  delay(40);
  c=analogRead(sharpPin);
  delay(40);
  d=analogRead(sharpPin);
  delay(40);
  e=analogRead(sharpPin);
  threshold=(a+b+c+d+e)/5;
}

void loop()
{
  for (ScanPos=0 ; ScanPos<180 ; ScanPos+=10) {
    scanservo.write(ScanPos);
    delay (40);
    distance=analogRead(sharpPin);


    if (distance > threshold)  {          // If no object in the way

      digitalWrite(ledPin, HIGH);
      leftspeed=180;
      rightspeed=180;
      leftservo.write(leftspeed);      // go forward
      rightservo.write(rightspeed);
    }
    else {
      digitalWrite(ledPin, LOW);
      int pos = ScanPos/10;
      switch (pos) {
      case '0':
        leftspeed=100;
        rightspeed=90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        break;

      case '1' :
        leftspeed = 110;
        rightspeed= 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case'2' :
        leftspeed = 120;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '3' :
        leftspeed = 130;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '4' :
        leftspeed = 140;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '5' :
        leftspeed = 150;
        rightspeed = 80;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '6' :
        leftspeed = 160;
        rightspeed = 70;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '7' :
        leftspeed = 170;
        rightspeed = 60;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '8' :
        leftspeed = 170;
        rightspeed = 40;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '9' :
        leftspeed = 180;
        rightspeed = 0;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '10' :
        leftspeed = 40;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay(1000);
        break;

      case '11' :
        leftspeed = 60;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '12' :
        leftspeed = 70;
        rightspeed = 160;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

        case '13' :
        leftspeed = 80;
        rightspeed = 150;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '14' :
        leftspeed = 90;
        rightspeed = 140;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '15' :
        leftspeed = 90;
        rightspeed = 130;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '16' :
        leftspeed = 90;
        rightspeed = 120;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '17' :
        leftspeed = 90;
        rightspeed = 110;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case '18' :
        leftspeed = 90;
        rightspeed = 100;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;
      }
    }
  }
  
  if (ScanPos = 180) {
    ScanPos = 0;
    scanservo.write(ScanPos);
    delay(90);
  }
}

Explanation: make a variable that is ScanPos divided by 10.
« Last Edit: June 12, 2010, 06:59:57 AM by Ro-Bot-X »
Check out the uBotino robot controller!

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: problem with switch case statement
« Reply #7 on: June 12, 2010, 10:16:47 AM »
OK I'm on V1.5 now, and it seems to work when power supply by the usb. Not if it's by the 9V battery ( there it goes mad, doing random thing around)

I made a few changes based on what I found when checking each composant of the robot.
First I figured that I was telling the left servo to go the opposite of what I was thinking.
Second, I found that distance > threshold was true if it was in fact wrong ( do you still follow me ?  ;) )
Third I found that sometimes, my sharpIR detect something where's there nothing and ( more annoying) get stuck-ed. So it bugs, then stay like if there was something there. I called the ghostbuster who came and checked the room : no ghost here  ;D. I don't know what the sharp sees...

Here's V1.5. Thank you already to all who helped me !
Code: [Select]
// * Ikeabot - Robot avoiding objects V1.5 12JUN10 * //


#include <Servo.h>

const int sharpPin=5; // set n of the pin
const int ledPin=2;

Servo leftservo;
Servo rightservo;
Servo scanservo;
int distance=0;
int threshold=0;
int leftspeed=0;
int rightspeed=0;
int ScanPos=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int boom=0;


void setup() {
  pinMode(sharpPin, INPUT); //set the pin of sharpIR and servo
  pinMode(ledPin, OUTPUT);
  scanservo.attach(7);
  ScanPos=90;
  scanservo.write(ScanPos);
  delay(2000); // wait while setting threshold
  setThreshold() ;
  leftservo.attach(5);     
  rightservo.attach(6);
  ScanPos=30;
  Serial.begin(9600);

}

void setThreshold() {  //set the threshold value
  a=analogRead(sharpPin);
  delay(40);
  b=analogRead(sharpPin);
  delay(40);
  c=analogRead(sharpPin);
  delay(40);
  d=analogRead(sharpPin);
  delay(40);
  e=analogRead(sharpPin);
  threshold=(a+b+c+d+e)/5;
}

void loop()
{
  for (ScanPos=30 ; ScanPos<150 ; ScanPos+=10) {
    scanservo.write(ScanPos);
    delay (40);
    distance=analogRead(sharpPin);
   


    if (distance < threshold)  {          // If no object in the way

      digitalWrite(ledPin, LOW);
     
      leftspeed=0;
      rightspeed=180;
      boom=0;
      Serial.println(boom);
      leftservo.write(leftspeed);      // go forward
      rightservo.write(rightspeed);
      delay(500);
    }
    else {
      digitalWrite(ledPin, HIGH);
      boom=666;
      Serial.println(boom);
      switch (ScanPos) { // avoid object smartly using pos of the SharpIR

      case 30 :
        leftspeed = 60;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 40 :
        leftspeed = 50;
        rightspeed = 90;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 50 :
        leftspeed = 40;
        rightspeed = 80;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 60 :
        leftspeed = 30;
        rightspeed = 70;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 70 :
        leftspeed = 20;
        rightspeed = 60;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 80 :
        leftspeed = 20;
        rightspeed = 40;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 90 :
        leftspeed = 0;
        rightspeed = 0;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1250);
        break;

      case 100 :
        leftspeed = 150;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay(1000);
        break;

      case 110 :
        leftspeed = 130;
        rightspeed = 170;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 120 :
        leftspeed = 120;
        rightspeed = 160;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 130 :
        leftspeed = 110;
        rightspeed = 150;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 140 :
        leftspeed = 90;
        rightspeed = 140;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      case 150 :
        leftspeed = 90;
        rightspeed = 130;
        leftservo.write(leftspeed);
        rightservo.write(rightspeed);
        delay (1000);
        break;

      default :
      leftspeed = 0;
      rightspeed = 180;
      leftservo.write(leftspeed);
      rightservo.write(rightspeed);
      delay (500);
      break;
     
      }
    }
  }

  if (ScanPos = 150) {
    ScanPos = 30;
    scanservo.write(ScanPos);
    delay(90);
  }
}

May the Achtuche be with you...

 


Get Your Ad Here

data_list