If condition scope issue

Well here is my code

 if(sensor3==LOW && sensor5==LOW){ ++s5;} 

So basically I want it to turnright first and then follow the line but bot conditions are true at the start do it just follows the line.... is there any way I can do that ?

The above code is much easier to interpret when formatted differently

if (sensor3 == LOW && sensor5 == LOW)
if (s5 == 0)
  if (sensor3 == LOW)
    followline(sensor1, sensor2, sensor3, sensor4, sensor5);

turnRight() will only be called if s5 is 0 and followline() will only be called if s5 is 0 and sensor3 is LOW

Please explain what you want to do in those terms. Using more descriptive variable names would be helpful too

I assume that the value of sensor3 is the result of a digitalRead() and not the actual pin number

For a more complete answer please post your complete sketch

Don’t post snippets (Snippets R Us!)

Well so basically what i want is say we have this code :

if (sensor3==LOW){turnRight();}  //the first time sensor3 becomes low it turnsright and the when it becomes
                                 // low again it follows the line
if (sensor3==LOW){followline();}

but when i try to do it using a variable and incrementing it.... the variable gets incremented random numbers or time ... so that's not viable

Basically what I want to say is Don’t post snippets (Snippets R Us!)

Post full code. We have no idea what sensor3 is, nor how things are wired, powered etc...

Provide details, that’s where the devil always hide.

And if you have code that only tracks pin state and not pin state change, then remember the arduino is running at 16mhz... the loop will execute likely thousands times per second...

but when i try to do it using a variable and incrementing it.... the variable gets incremented random numbers or time

Which variable ?
Where is it declared ?
What type of variable is it ?
What is it initial value ?
Where is it incremented ?
How is it incremented ?
By how much is it incremented ?
Is it ever decremented ?
If so, where ?
If so, by how much ?
You mention incrementing a variable but the code tests for equality with LOW. That does not make any sense

If only we could see your complete sketch ...

Here is the full code :

#include <Servo.h>                           // Include servo library
Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
int button=2;
int led=11; 
int d3=3; 
int d4=4;
int d5=5;
int d6=6;
int d7=7;
int s1=0;
int s13=0;
int s53=0;
int s2=0;
int s3=0;
int s4=0;
int s5=0;

void setup()                                 // Built-in initialization block
   pinMode( button, INPUT_PULLUP);
   pinMode(led, OUTPUT);
   pinMode( d7, INPUT);                         // Set right whisker pin to input
   pinMode( d6, INPUT);  
   pinMode( d5, INPUT);                         // Set right whisker pin to input
   pinMode( d4, INPUT);  
   pinMode( d3, INPUT); 
   servoLeft.writeMicroseconds(1480);         // Left wheel counterclockwise
  servoLeft.attach(12);                      // Attach left signal to P12 
  servoRight.attach(13);                     // Attach right signal to P13
  //disableServos();                           // Stay still indefinitely
void loop()                                  // Main loop auto-repeats
  int sensor1 = digitalRead(d7);          //Rightmost
  int sensor2 = digitalRead(d6);
  int sensor3 = digitalRead(d5);          //middle
  int sensor4 = digitalRead(d4);
  int sensor5 = digitalRead(d3);         //Lefttmost


    if(sensor1==LOW && sensor3==LOW){ ++s13;}
    //if(sensor2==LOW){ ++s2;} 
    if(sensor3==LOW && sensor5==LOW){ ++s53;} 
    //if(sensor2==LOW){ ++s2;}
    if(sensor3==LOW){ ++s3;} 
    if(sensor5==LOW){ ++s5;}  
  //  if(s53==0){turnRight();}
 //{ followline(sensor1,sensor2,sensor3,sensor4,sensor5);} 
    //if(s5!=0 && s3!=0){followline(sensor1,sensor2,sensor3,sensor4,sensor5);}
   int buttonState = digitalRead(button);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(led, HIGH);
  } else {
    // turn LED off:
    digitalWrite(led, LOW);

 // delay(100);

void followline(int ss1,int ss2,int ss3,int ss4,int ss5)
  if(ss2==LOW){turnRight();}                               //--->line follower segment begins 
  if(ss4==LOW) {turnLeft();}
  if(ss2==LOW && ss3==LOW ){turnRight();}
  if(ss4==LOW && ss3==LOW ){turnLeft();}
  if(ss3==LOW) {forward();}

void forward()                       // forward function
     servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
     servoRight.writeMicroseconds(1300); }
    // Right wheel clockwise
  //delay(time);                               // Maneuver for time ms

void turnLeft()                      // Left turn function
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  //delay(time);                               // Maneuver for time ms

void turnRight()                     // Right turn function
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
 // delay(time);                               // Maneuver for time ms

void backward()                      // backward function
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
void formatPrint( char *leftStr, int MyVar1){
     Serial.println (leftStr);
     Serial.println (MyVar1);

void stay()                     // Right turn function
  servoLeft.writeMicroseconds(1480);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1472);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
void disableServos()                         // Halt servo signals
  servoLeft.detach();                      // Stop sending servo signals

So what I understood from pin state change is that it increments only when the state of pin is changed from high to low or low to high....So that might work....could you please tell me how i can code that and use it with my five sensors? So that the counter only increments when the pin state of the sensors changes.

To track a change you memorize the previous value and compare it with the current value. If they are not the same, you got a change

Thank for posting the code, but it is almost impossible to understand due to the cryptic and anonymous variable names used and I don't have the patience to decipher it at the moment.

For instance, what is the point of having a variable named d3 when it holds a value of 3. It would be better named something like leftSensorPin. To compound things, when you read its state you put it in another anonymous variable named sensor5

Please do us and yourself a favour by giving variables meaningful names so that reading the code is more like reading a sentence


Isn’t that the same code you said was all working fine in that discussion?

Yes that code was working fine for the line following task ... Now I am using that line follower to implement grid navigation.

So I changed the variable name(or at least commented to describe it) for better understanding.

I implemented the state change counter and it works fine (i checked on serial monitor).

Now everything works as it should. Thanks for suggesting the pin state change.....Also if someone knows any grid navigation algorithms and could share a link to them , I will really appreciate it.

Please post your revised code as there may be other changes that it would benefit from, either in its operation, making it easier to understand or both

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.