How to use switch case for light dependent resistor sensor's value

Hello, I am trying to build a robot (line follower) which is equipped with light resistor sensors, so I can read variation of voltage on each of them in the .

I am trying to use swich case to send commands to motors if I specific value is selected between 0 and 1023. I don’t know how to do it for 4 resistors. Maybe I should use array…

code: #

int MotControl1=6; //Assigne la pin #6 (“a” tourne à gauche)
int MotControl2=7; //Assigne la pin #7 (“s” toutne à droite)
int MotControl3=2; //Assigne la pin #3 (voiture avance)
int MotControl4=3; //Assigne la pin #3 (voiture recule)

int LDR1=0; //LDR1 cathode to A0
int LDR2=1; //LDR1 cathode to A1
int LDR3=2; //LDR1 cathode to A2
int LDR4=3; //LDR1 cathode to A3

int LDR={LDR1, LDR2, LDR3, LDR4}; //Assigne value 0 to integer of variable val
int val=0;
int byteout=0; //Key pressed on DEC
int space=32; //Space
float msr=0; //Voltage measure on LDR

void setup(){
Serial.begin(9600);
pinMode(LDR1,INPUT);
pinMode(LDR1,LOW);
}

void loop(){
val=analogRead(LDR[0]);
Serial.print(“range=”);
Serial.println(val);
msr=analogRead(LDR[5])*5.00/1023;
Serial.write(space);
Serial.print(“Voltage=”);
Serial.print(msr,3);
Serial.write(space);
byteout=Serial.read();
delay(500);

switch (val){
case 157: // Lorsque la touche “w” est activée, la voiture tourne à gauche
Serial.print(“MotControl4=”);
Serial.print(“MotControl1=”);
digitalWrite(MotControl4,HIGH);
digitalWrite(MotControl3,LOW);
Serial.print(byteout);
Serial.write(space);
digitalWrite(MotControl1,HIGH);
digitalWrite(MotControl2,LOW);
Serial.print(byteout);
Serial.write(space);
delay (500);
break;
}

//switch (LDR[0]){
//case 157:
//Serial.print(“MotControl4=”);
//Serial.print(“MotControl1=”);
//digitalWrite(MotControl4,HIGH);
//digitalWrite(MotControl3,LOW);
//Serial.print(byteout);
//Serial.write(space);
//digitalWrite(MotControl1,HIGH);
//digitalWrite(MotControl2,LOW);
//Serial.print(byteout);
//Serial.write(space);
//delay (400);
//break;
//}
}

When posting code, use code tags! (the button with the hash '#' sign is used to generate them)

Using a switch/case combo for just one value doesn't make sense. Just use an if statement instead:

if (val == 157) {
  // your code here
}
int LDR[]={LDR1, LDR2, LDR3, LDR4};     //Assigne value 0 to integer of variable val

So, the array holds 4 values, numbered 0, 1, 2, and 3.

msr=analogRead(LDR[5])*5.00/1023;

You can’t read from the pin in the 6th position of a 4 element array.

//switch (LDR[0]){

Switching on a pin number hardly makes sense.

Actually switching on any single value read from any of the LDRs doesn’t make sense. A line following robot reads all 4 values, and uses the 4 values to determine where the line is, relative to it’s current heading, and adjusts speed to get the line back in the middle.

[quote author=pylon link=topic=232036.msg1672682#msg1672682 date=1397053234]
When posting code, use code tags! (the button with the hash '#' sign is used to generate them)

Using a switch/case combo for just one value doesn't make sense. Just use an if statement instead:


int MotControl1=6;                      //Assigne la pin #6 ("a" tourne à gauche)
int MotControl2=7;                      //Assigne la pin #7 ("s" toutne à droite)
int MotControl3=2;                      //Assigne la pin #3 (voiture avance)
int MotControl4=3;                      //Assigne la pin #3 (voiture recule)

int LDR1=0;                             //LDR1 cathode to A0
int LDR2=1;                             //LDR1 cathode to A1
int LDR3=2;                             //LDR1 cathode to A2
int LDR4=3;                             //LDR1 cathode to A3

int LDR[]={LDR1, LDR2, LDR3, LDR4};     //Assigne value 0 to integer of variable val  
int space=32;                           //Space
int val=0;                              //Declare variable val as integer

float msr1=0;                           //Declare variable measure1 as decimal
float msr2=0;                           //Declare variable measure2 as decimal
float msr3=0;                           //Declare variable measure3 as decimal
float msr4=0;                           //Declare variable measure4 as decimal
float msr5=0;                           //Declare variable measure5 as decimal

void setup(){
Serial.begin(9600);
pinMode(LDR1,INPUT);
pinMode(LDR1,LOW);
}

void loop(){
msr1=analogRead(LDR[0]);//*5.00/1023;
Serial.print("Voltage (LDR1)=");
Serial.println(msr1);
Serial.write(space); 
delay(500);


switch (LDR[0]){
case 154:               // Lorsque la touche "w" est activée, la voiture tourne à gauche  
Serial.print("LDR=");
Serial.print(LDR[0]);
Serial.print("MotControl4=");
Serial.print("MotControl1=");
digitalWrite(MotControl4,HIGH);
digitalWrite(MotControl3,LOW);
Serial.write(space);
digitalWrite(MotControl1,HIGH);
digitalWrite(MotControl2,LOW);
Serial.write(space);
delay (500);
break;
}

//switch (LDR[0]){                   
//case 157:
//Serial.print("MotControl4=");
//Serial.print("MotControl1=");
//digitalWrite(MotControl4,HIGH);
//digitalWrite(MotControl3,LOW);
//Serial.print(byteout);
//Serial.write(space);
//digitalWrite(MotControl1,HIGH);
//digitalWrite(MotControl2,LOW);
//Serial.print(byteout);
//Serial.write(space);
//delay (400);
//break;
//}
}// your code here
}

[/quote]

So I need to use integer value, How do I do to read all 4 values, I guess I can do it sequently one by one, but not all 4 together?

So I need to use integer value, How do I do to read all 4 values, I guess I can do it sequently one by one, but not all 4 together?

The Arduino can never do two or more things at the same time because it has only one processor. So yes, it has to do it sequentially.

You can do the read like this:

uint8_t i;
for (i = 0; i < 4; i++) {
  msr = analogRead(LDR[i]);
  if (msr == 154) {
    // do anything
  }
}

I cannot give you more hints because I have no clue what your sketch is expected to do. Usually checking a specific value you got from an analogRead() doesn’t make sense, so in the above code we probably still have an error.

pylon:

So I need to use integer value, How do I do to read all 4 values, I guess I can do it sequently one by one, but not all 4 together?

The Arduino can never do two or more things at the same time because it has only one processor. So yes, it has to do it sequentially.

You can do the read like this:

uint8_t i;

for (i = 0; i < 4; i++) {
  msr = analogRead(LDR[i]);
  if (msr == 154) {
    // do anything
  }
}




I cannot give you more hints because I have no clue what your sketch is expected to do. Usually checking a specific value you got from an analogRead() doesn't make sense, so in the above code we probably still have an error.

I converted each value into voltage and I used to compare. It’s actually working:

void loop(){
msr1=analogRead(LDR[0])*5.00/1023;
Serial.print("Voltage(LDR1)=");
Serial.print(msr1,3);
Serial.write(space); 
val1=analogRead(LDR[0]);
Serial.print("range="); 
Serial.print(val1); 
Serial.write(space);

msr2=analogRead(LDR[1])*5.00/1023;
Serial.print("Voltage(LDR2)=");
Serial.print(msr2,3);
Serial.write(space); 
val2=analogRead(LDR[1]);
Serial.print("range="); 
Serial.print(val2); 
Serial.write(space);

msr3=analogRead(LDR[2])*5.00/1023;
Serial.print("Voltage(LDR3)=");
Serial.print(msr3,3);
Serial.write(space); 
val3=analogRead(LDR[2]);
Serial.print("range="); 
Serial.print(val3); 
Serial.write(space);

msr4=analogRead(LDR[3])*5.00/1023;
Serial.print("Voltage(LDR4)=");
Serial.print(msr4,3);
Serial.write(space);
val4=analogRead(LDR[3]);
Serial.print("range="); 
Serial.print(val4); 
Serial.write(space);


msr5=analogRead(LDR[4])*5.00/1023;
Serial.print("Voltage(LDR5)=");
Serial.print(msr5,3);
Serial.write(space);
val5=analogRead(LDR[4]);
Serial.print("range="); 
Serial.print(val5); 
Serial.write(space);
Serial.println("");
delay(500);

if(val3==151){       // Lorsque la touche "w" est activée, la voiture avance  
Serial.print("MotControl4=");
Serial.print(MotControl4);
do{digitalWrite(MotControl4,HIGH);
digitalWrite(MotControl3,LOW);

msr3=analogRead(LDR[2])*5.00/1023;
Serial.print("Voltage(LDR3)=");
Serial.print(msr3,3);
Serial.write(space); 
val3=analogRead(LDR[2]);
Serial.print("range="); 
Serial.print(val3); 
Serial.write(space);
Serial.println("");
delay(500);
}
while(val3==151);
}

Nick2012Future:
So I need to use integer value, How do I do to read all 4 values, I guess I can do it sequently one by one, but not all 4 together?

You can read 4 pins at once if they are on the same Port just by reading the Port register.

You can also use byte instead of int for values 0 to 255 and quit wasting RAM.

@PaulS: You're going soft on us:

You can't read from the pin in the 6th position of a 4 element array.

I would have expected something more entertaining, like:

You can read the 6th position of a 4 element array, it's just that it probably won't be anything useful.

Nick2012Future:

pylon:

So I need to use integer value, How do I do to read all 4 values, I guess I can do it sequently one by one, but not all 4 together?

The Arduino can never do two or more things at the same time because it has only one processor. So yes, it has to do it sequentially.

You can do the read like this:

uint8_t i;

for (i = 0; i < 4; i++) {
  msr = analogRead(LDR[i]);
  if (msr == 154) {
    // do anything
  }
}




I cannot give you more hints because I have no clue what your sketch is expected to do. Usually checking a specific value you got from an analogRead() doesn't make sense, so in the above code we probably still have an error.

I converted each value into voltage and I used to compare. It’s actually working:

void loop(){

msr1=analogRead(LDR[0])*5.00/1023;
Serial.print(“Voltage(LDR1)=”);
Serial.print(msr1,3);
Serial.write(space);
val1=analogRead(LDR[0]);
Serial.print(“range=”);
Serial.print(val1);
Serial.write(space);

msr2=analogRead(LDR[1])*5.00/1023;
Serial.print(“Voltage(LDR2)=”);
Serial.print(msr2,3);
Serial.write(space);
val2=analogRead(LDR[1]);
Serial.print(“range=”);
Serial.print(val2);
Serial.write(space);

msr3=analogRead(LDR[2])*5.00/1023;
Serial.print(“Voltage(LDR3)=”);
Serial.print(msr3,3);
Serial.write(space);
val3=analogRead(LDR[2]);
Serial.print(“range=”);
Serial.print(val3);
Serial.write(space);

msr4=analogRead(LDR[3])*5.00/1023;
Serial.print(“Voltage(LDR4)=”);
Serial.print(msr4,3);
Serial.write(space);
val4=analogRead(LDR[3]);
Serial.print(“range=”);
Serial.print(val4);
Serial.write(space);

msr5=analogRead(LDR[4])*5.00/1023;
Serial.print(“Voltage(LDR5)=”);
Serial.print(msr5,3);
Serial.write(space);
val5=analogRead(LDR[4]);
Serial.print(“range=”);
Serial.print(val5);
Serial.write(space);
Serial.println("");
delay(500);

if(val3==151){       // Lorsque la touche “w” est activée, la voiture avance 
Serial.print(“MotControl4=”);
Serial.print(MotControl4);
do{digitalWrite(MotControl4,HIGH);
digitalWrite(MotControl3,LOW);

msr3=analogRead(LDR[2])*5.00/1023;
Serial.print(“Voltage(LDR3)=”);
Serial.print(msr3,3);
Serial.write(space);
val3=analogRead(LDR[2]);
Serial.print(“range=”);
Serial.print(val3);
Serial.write(space);
Serial.println("");
delay(500);
}
while(val3==151);
}

Thank you, your code is much simple and as much useful.

Nick, you are reading whether an analog pin has bled down through a banana to a certain threshold voltage. And that is taking 16 bit values and 105 usec reads plus code you don't need.

A pin used as digital INPUT_PULLUP will do all that and simply present you with 1 bit per banana. You can read up to 6 bananas at once with an UNO for IIRC 16 bananas (maybe 18?). You can read whole sets of 8 with a MEGA. When you read the pins in a Port, all the bits are right there together in the same byte, the evaluation and organization all part of the wiring and code allowing you to use LESS of both to do the job.