go_away

Author Topic: code for simple line follower using switch case  (Read 1444 times)

0 Members and 1 Guest are viewing this topic.

Offline V!KR@MTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
code for simple line follower using switch case
« on: August 09, 2012, 01:00:13 PM »
Hello everyone, I'm new to robotics and i'm trying to write a line follower robot code using switch case

The problem comes with case statement. That is if i use 3 line sensors, i get 3 inputs. So how can i write switch case for different cases? Like..
Code: [Select]
case 101:
break;
 case 110:
break;


Before going ahead, the code i have tried for testing:
Code: [Select]
int s[1]; //2 sensors
void setup()
{
  pinMode(2, INPUT);
  pinMode(12, INPUT);
  pinMode(13, OUTPUT);
}
void loop()
{
  s[0]= digitalRead(2);
  s[1]= digitalRead(12);

  switch (s[])
{
case {1,0}:
digitalWrite(13, LOW);//led connected to pin 13
break;

case {1,1}:
digitalWrite(13, HIGH);
break;

}
}

But it shows the error : expected primary expression before ']' token.
Looks like the switch statement doesn't take array.
Is there a way to do with switch case or it's not possible? How to combine all the inputs from sensors and put it in switch statement?

I hope i have expressed properly. Any help would be appreciated.
Sorry for my bad english


Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: code for simple line follower using switch case
« Reply #1 on: August 10, 2012, 12:36:35 AM »
The switch statement expects a reference to a value to compare to your case. Thus, it can use an array, but only if it knows exactly to what index refer to. What I mean is that the switch does not now what value from your array to use for comparison. The switch statement works with a single variable, and an array is basically a collection of variables.
Knowledge does not weigh.

Offline ebrady

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: code for simple line follower using switch case
« Reply #2 on: August 10, 2012, 05:36:56 AM »
You can not use an array with a case statement.  The easiest method would be to convert your input data into a single int using shift and add.  Here is a modified verions of your original code.

Code: [Select]
int s; //2 sensors
void setup()
{
  pinMode(2, INPUT);
  pinMode(12, INPUT);
  pinMode(13, OUTPUT);
}
void loop()
{
  s = digitalRead(2);  //Read Pin 2 into s
  s = (s << 1) + digitalRead(12); //Shift s one and add pin 12 

//At this point s contains a value from 0 - 3 which is a 2 bit representation of the two pins.
//Use s in your switch statement and act on it as a single int.

switch (s)
{

case 0: // This is the { 0,0 } case
break;

case 1: // This is the { 0,1 } case
break;

case 2:  //This is the { 1,0 } case
digitalWrite(13, LOW);//led connected to pin 13
break;

case 3:  //This is the { 1,1 } case.
digitalWrite(13, HIGH);
break;

default:
break

}
}

Hope this helps

Offline V!KR@MTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: code for simple line follower using switch case
« Reply #3 on: August 10, 2012, 06:58:23 AM »
You can not use an array with a case statement.  The easiest method would be to convert your input data into a single int using shift and add.  Here is a modified verions of your original code.

Code: [Select]
int s; //2 sensors
void setup()
{
  pinMode(2, INPUT);
  pinMode(12, INPUT);
  pinMode(13, OUTPUT);
}
void loop()
{
  s = digitalRead(2);  //Read Pin 2 into s
  s = (s << 1) + digitalRead(12); //Shift s one and add pin 12 

//At this point s contains a value from 0 - 3 which is a 2 bit representation of the two pins.
//Use s in your switch statement and act on it as a single int.

switch (s)
{

case 0: // This is the { 0,0 } case
break;

case 1: // This is the { 0,1 } case
break;

case 2:  //This is the { 1,0 } case
digitalWrite(13, LOW);//led connected to pin 13
break;

case 3:  //This is the { 1,1 } case.
digitalWrite(13, HIGH);
break;

default:
break

}
}

Hope this helps
Ok i understand. But does it do a binary addition or decimal addition?
Like what does it do when it comes across an addition like 101+1. Does it give 110 or 102?

Offline ebrady

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: code for simple line follower using switch case
« Reply #4 on: August 10, 2012, 07:06:39 PM »
Ok i understand. But does it do a binary addition or decimal addition?
Like what does it do when it comes across an addition like 101+1. Does it give 110 or 102?

There really isn't a such thing as binary addition or decimal addition.  There is only "addition".  What you are asking about, is what kind of value does s represent, a decimal or binary number. In the case of the example I posted, think of all of the additions as being "decimal addition".
 

 


Get Your Ad Here

data_list