-1

not sure what is going wrong in the code below, but it's a simplified code of a larger piece I'm trying to debug. I notice that the code seems to skip to the else statement even though the if statement is valid. Potential is set to analogRead(A0)

while (potential < 1023) {
Serial.println(analogRead(A0));
if (potential < 1023) {
  if (potential > 200) {
    Serial.println("cool");
  } else {
    Serial.println("low");
    lcd.print("hello");
    delay(1000);
  }
}

Results in serial monitor show

235
low
324
low

I've also tried if (potential < 1023 && potential > 200) and if ((potential < 1023) && (potential >200)) to no avail.

Can anyone help, let me know what's going wrong or suggest a fix to this?

3
  • 2
    Did you forget to read into the variable? – Ignacio Vazquez-Abrams May 31 '16 at 2:14
  • Indeed, the comparison appears to be based on an old value (or maybe an initial 0), not the new value you are printing but never assigning to "potential". You should probably do the analog read to potential and print that, rather than directly print the return value. – Chris Stratton May 31 '16 at 2:16
  • @ChrisStratton and Ignacio Thanks guys, I added the variable into the while loop and it worked – Sarahhyy Jun 1 '16 at 8:51
1

I'm not sure what you're trying to achieve with that while. But this will result in none of your other code being executed until potential is above 1022, which sound rather weird. (Unless it's for some kind of safety mechanism).

The problem is that you do send the new value over serial. But you do not assign that new value to the variable 'potential'.

Basically, you see something completely different as what is actually happening. You should do Serial.println(potential); to be sure that you print the value of 'potential'.

And you should assign the analogRead(A0); to your variable potential.

I've also taken away the while since I think it would be misplaced. And I worked around it, so it doesn't block your complete sketch.

void loop() {
  potential = analogRead(A0));
  Serial.println(potential);

  if (potential < 1023) {
    if (potential > 200) {
      Serial.println("cool");
    } else {
      Serial.println("low");
      lcd.print("hello");
      delay(1000);
    }
  }else{
    //Code that won't run if potential < 1023
  }

  //Other code that can run "simultaniously".
}
3
  • Thanks for the answer, I'd like to explain a bit more about what I'm trying to achieve. I want to make sure it doesn't go on to the next part of the code till the potentiometer has been turned all the way to the end (1023). So the whole chunk in my question was part of void setup actually, which also explains why I needed the while for the loop. I set potential as a global variable, do I need to define it again in the setup scope? – Sarahhyy Jun 1 '16 at 4:21
  • Hi Paul, I reintroduced the variable into the while loop and it worked. – Sarahhyy Jun 1 '16 at 8:51
  • @Sarahhyy Ah, okay, that other part of the code wasn't shown before ;) Please consider accepting this as an answer, if it solved your problem. – Paul Jun 1 '16 at 9:57
0

If in while loop, analogRead needs to be reassigned so it is constantly read.

int potential = analogRead(A0);

void setup() {
  while (potential < 1023) {
    potential = analogRead(A0);
    ...
    }
}

Thanks guys for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.