Help with While loop issue

Hi,

I'm new to coding in ardunio and need some help, so please excuse the coding.

I'm trying to write a program to drive s servo motor, while checking the status of 2 IR sensors. when i active the sensor the code stops looping, and I cant understand why, can any one help?

code as follows:

[#include <Servo.h>

// setup inputs
int irPin1 = 2;                     // Setup pin 2 as IR effect sensor 1
int irPin2 = 3;                     // Setup pin 3 as IR effect sensor 2

int irval1 = 0;                           // Setup intial IR sesnor 1 value as low.
int irval2 = 0;                           // Setup intial IR sesnor 2 value as low.
int induct1 = 0;

// Setup outputs 

int servoirPin=10;
Servo servoir;                            // Define servo IR sensor
int irpos = 0;                           // Define servo start postion
int ledPin1 = 8;                     // Setup pin 8 as LED
int ledPin2 = 9;                     // Setup pin 8 as LED
void setup() 
{
pinMode(ledPin1, OUTPUT);                       // set the ledPin as an output
pinMode(ledPin2, OUTPUT);                       // set the ledPin as an output
pinMode(irPin1, INPUT);                      // set the IR sensor1 as an input
pinMode(irPin2, INPUT);                      // set the IR sensor 2 as an input
Serial.begin(9600);                            // attach PC monitor for testing
servoir.attach(servoirPin);                             // attach IR servo      
}


void loop() {
irval1  = digitalRead(irPin1);          // read the current state of IR sensor 1
irval2 = digitalRead(irPin2);           // read the current state of IR sensor 2
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
    while (irval1 == HIGH && irval2 == HIGH) { //While Sensors Clear servo runs
            digitalWrite(ledPin1, HIGH);       // trun on LED to show senspors clear
for(irpos = 90; irpos < 180; irpos++)           // set servo psotion
{
 servoir.write(irpos);                         //write psotion to servo
            delay(15)           
}
digitalWrite(ledPin1, LOW);                     // turn off LED 1
digitalWrite(ledPin2,HIGH);                     // Show sensors activiated 
}
]

You're not updating the control variables inside the while loop, so once it is in, it stays in.

At first glance, your code looks incomplete.

Please remember to use code tags when posting code

Could you clarify what you call an "IR Sensor"? is that a PIR ?


Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Hi,

Sorry I've updated post (newbie here).

The IR sensors are obstacle avoidance IR sensors.

I do plan to do more with the code after, but trying to get this section to work first. I'm trying to cycle the servo if the sensors are both clear. if not turn on a warning light until the sensors are cleared. Hope that helps.

OK (your while issue was captured by AWOL)

Can anyone advise on how to change it to get it to work?

You could skip putting the results of the digitalReads into variables, and put the digitalReads directly into the while.

Thanks that got me up and running :slight_smile: