Go Down

Topic: While loops not working completly (Read 680 times) previous topic - next topic

89ls1

Oct 30, 2012, 06:35 pm Last Edit: Oct 30, 2012, 07:57 pm by 89ls1 Reason: 1
My sketch has two while loops polling a single analog port each. The sketch gets through the first while loop no problem but does nothing after the delay. Can't seem to solve the problem, as all looks well to me. Please help.. The down sequence works fine. It just does not go back up.

Code: [Select]

const int upRelay = 9;
const int downRelay = 8;
const int dlimit = A4;
const int ulimit = A2;
int start;
int threshold = 500;

void  setup()
{
 Serial.begin(9600);
     pinMode(upRelay, OUTPUT);
     pinMode(downRelay, OUTPUT);
     pinMode(ulimit, INPUT);
     pinMode(dlimit, INPUT);
}
void loop() {
   if (Serial.available()) {
//      int dvalue = analogRead(dlimit);   //No longer in use as value is read in while loop
//      int uvalue = analogRead(ulimit);   //No longer in use as value is read in while loop
   start = Serial.read();
   if (start == 49) //Press 1 to start sequence
   {
     while(analogRead(dlimit) < threshold) {
     digitalWrite(downRelay, HIGH);
     }
     digitalWrite(downRelay,LOW);
     delay(10000);
     while(analogRead(ulimit) < threshold) {
     digitalWrite(upRelay,HIGH);
     }
     digitalWrite(upRelay,LOW);
     Serial.flush();
   }
   else
   {
     digitalWrite(upRelay, LOW);
     digitalWrite(downRelay, LOW);            
   }
 }
}

CrossRoads

Is it possible that ulimit is < threshold (so ~2.5V) and thus never leaves the while test?
Code: [Select]

while(analogRead(ulimit) < threshold)
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

AWOL

What do your debug prints tell you is happening?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

89ls1

CrossRoads :: The ulimit is < threshold therefore it should set the upRelay HIGH but it does not.

AWOL :: When I was watching to see what ulimit is doing the value reads 16 which is less than the 500 threshold.
If I comment out the down stuff and the delay, then run the sketch again, the serial data is delayed a bit then finally shows the start value and the ulimit value which shows 16 but still nothing happens.

Any suggestions on what I should be looking at for debugging? Or is just watching the ulimit enough seeing as that is the part not seeming to work.

CrossRoads

I think you'll find that this will sit there & keep testing until the condition is false, and then move on.
http://arduino.cc/en/Reference/While
Code: [Select]

while(analogRead(ulimit) < threshold)

You could put some
Code: [Select]

Serial.println("code is here now");
kind of statements in,
or
Serial.print ("data is now  ");
Serial.println (data);

to see what is going on
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up