Pages: [1]   Go Down
Author Topic: While loops not working completly  (Read 641 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);            
    }
  }
}
« Last Edit: October 30, 2012, 01:57:08 pm by 89ls1 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27069
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26278
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do your debug prints tell you is happening?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27069
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
while(analogRead(ulimit) < threshold)
You could put some
Code:
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
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess I am not understanding the while parameter fully.

If
Code:
while(analogRead(dlimit) < threshold)
works properly (as I expect it to), by setting downRelay HIGH untill dlimit reads => 500

then why would the ALMOST exact piece of code not do the same thing on the next while loop?
Code:
while(analogRead(ulimit) < threshold)
That is saying while my ulimit sensor (upper limit switch) is reading less than 500 set the upRelay HIGH, when ulimit is => 500 set upRelay LOW.....correct?

ulimit only reads greater than 500 when the sled is at home.
dlimit only reads greater than 500 when the sled is at full extension.


while(expression) {...} 
expression - a (boolean) C statement which evaluates to true OR false.
My statements start as false and evaluate to true.
« Last Edit: October 30, 2012, 03:04:13 pm by 89ls1 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27069
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So put in some serial prints and see what is actually happening.
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Added a bunch of debug info.. Seems once it gets to the 2nd while loop it does not do anything. When changing the < to >= just as a test I fried both of those relays.

 
Code:

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() {
  Serial.print("ulimit = ");
  Serial.println(ulimit);
  Serial.print("dlimit = ");
  Serial.println(dlimit);
    if (Serial.available()) {
//      int dvalue = analogRead(dlimit);
//      int uvalue = analogRead(ulimit);
    start = Serial.read();
    Serial.println(start);
    if (start == 49) //Press 1 to start sequence
    {
      while(analogRead(dlimit) < threshold) {
        Serial.print("In first while - dlimit ");
        Serial.println(dlimit);
      digitalWrite(downRelay, HIGH);
      }
        Serial.print("After first while dlimit ");
        Serial.println(dlimit);
      digitalWrite(downRelay,LOW);
        Serial.print("dlimit after dRelay is LOW ");
        Serial.println(dlimit);
        Serial.print("ulimit before delay ");
        Serial.println(ulimit);
      delay(10000);
        Serial.print("ulimit before 2nd while ");
        Serial.println(ulimit);
      while(analogRead(ulimit) < threshold) {
        Serial.print("ulimit in Second while ");
        Serial.println(ulimit);
      digitalWrite(upRelay,HIGH);
      }
      digitalWrite(upRelay,LOW);
        Serial.print("ulimit after uRelay LOW ");
        Serial.println(ulimit);
      Serial.flush();
    }
    else
    {
      digitalWrite(upRelay, LOW);
      digitalWrite(downRelay, LOW);            
    }
  }
}


Also if I completely comment out the down loop it and just run it with the up loop it goes to the ulimit and stops as it should. But they will not work together.... What am I missing?
« Last Edit: October 30, 2012, 05:18:06 pm by 89ls1 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27069
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

change these to
Code:
Serial.println(analogRead(dlimit));
&
Serial.println(analogRead(ulimit));

otherwise dlimit & ulimit are just whatever value they are after these:
const int dlimit = A4;
const int ulimit = A2;
Logged

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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      while(analogRead(dlimit) < threshold) {
        Serial.print("In first while - dlimit ");
        Serial.println(dlimit);
      digitalWrite(downRelay, HIGH);
      }
Aside from you atrocious indenting (use Tools + Auto Format!), it is not necessary to keep banging on the pin. Do it once, and do nothing in the body of the loop, except, if desired print the value read from the pin (not, as CrossRoads points out, the pin number).

Code:
    if (start == 49) //Press 1 to start sequence
would be so much simpler to maintain as:
Code:
    if (start == '1') //Press 1 to start sequence
Then, you wouldn't even need the useless comment.

Code:
void loop() {
  Serial.print("ulimit = ");
  Serial.println(ulimit);
  Serial.print("dlimit = ");
  Serial.println(dlimit);
Since ulimit and dlimit are const, is it really necessary to print them every pass through loop?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help. I have since changed the sensor pins from "const int" to "int". They are not supposed to be constant as the value should change when switched high. Also I changed what the while is looking for, instead of a value < and eventually > 500 I just went ahead and am doing a digitalRead looking for high or low. As when I started looking at the values of the pins throughout the loop they seemed to float around 500-600 and when tied low with a 10k it did not even recognize the sensor going high and passed it. Have not tested my changes but hopefully that solves my issue.

Whats wrong with my indenting? I think indenting helps alot.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have since changed the sensor pins from "const int" to "int". They are not supposed to be constant as the value should change when switched high.
You have wires that jump around on the board? The pin that something is connected to is not supposed to change. Pin numbers are supposed to be constant. The state of the pin and the number of the pin are two different things, and are NOT interchangeable.

Quote
Whats wrong with my indenting? I think indenting helps alot.
       Only
if
    it
                         is
           consistent.
Logged

Pages: [1]   Go Up
Jump to: