stay in void setup until condition

Guys, I have question regarding void setup as in sample below. when i program an arduino, it directly executes void loop without completing the conditions below. anything wrong with my code?

void setup () {
while (spin >= 5) {
pbstate = digitalRead(7);
if (pbstate != lbstate) {
if (pbstate == LOW) {
spin++;}
else {
delay(100);}
lbstate = pbstate;
}
digitalWrite(9,HIGH);
}
}

You posted at the wrong site, think you meant to post it on http://snippets-r-us.com/

void setup () {
while (spin >= 5) {
    pbstate = digitalRead(7);
    if (pbstate != lbstate) {
      if (pbstate == LOW) {
        spin++;}
      else {
        delay(100);}
        lbstate = pbstate;
    }
  digitalWrite(9,HIGH);
}
}

probably spin is not >= 5 so you don't enter the while loop... but as you did not post the full code and how things are declared or wired (PULLUP ?), we have no clue..may be you meant

void setup () {
  while (spin < 5) { // **** changed to <
    pbstate = digitalRead(7);
    if (pbstate != lbstate) {
      if (pbstate == LOW)
        spin++;
      else
        delay(100);
      lbstate = pbstate;
    }
    digitalWrite(9, HIGH);
  }
}

thank you for your reply. code also does not execute digitalWrite (9,HIGH);. pbstate is = to a switch installed on PIN7. i have not pressed the switch still it proceeds to the void loop.

PROVIDE FULL DETAILS.

  • how things are wired (including grounds etc)
  • how things are powered
  • code demonstrating the issue that compiles.

without that, can't help.

PS: digitalWrite(9, HIGH); is in the while (notice your brackets?), so if you don't enter the while, you never get to execute it

condition is, if while is true then digitalWrite(9, HIGH);

changing while (spin < 5) runs the digitalWrite(9, HIGH) without pressing the button.

I'm not sure what you don't understand when being asked to provide full details.

May be you'll find someone with more patience than me. Bye.

Thank you. i managed to make the code work

Please post details of how you solved it

This may help others with a similar problem

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