Dummy while loop question

I thought this part would be quite straightforward, but having quite a time with the while statement. been at it a while, and out of head scratches.

The way I see this is that line 9 should go to the button loop, which forever calls the ready loop until pin 3 goes low, at which point it should return to he void loop at line 11. In action it just keeps running the ready loop.

I swapped while for if with expected results .. it only tests once and does return to the void loop. What am I missing?

EDIT .. pin 3 is on a momentary pushbutton to ground with a 10K pullup on the pin - it's not a hardware issue.

int x = 0;
int a = 0;
void setup() {
  pinMode(3, INPUT);  //
  Serial.begin(9600, SERIAL_8N1);
}

void loop() {
  button();

  for (int x = 0; x < 60; x = x + 1) {
    Serial.write(0x12);
    Serial.write(" ELAPSED SECONDS IN  SHOP: ");
    if (x < 10) { Serial.write("0"); }
    Serial.print(x);
    delay(1000);
    Serial.write(0x12);
  }
}

void ready() {
  Serial.write(0x12); 
  Serial.write("?Ready_");
  delay(100);
  Serial.write(0x12);
  Serial.write("?Ready ");
  delay(100);
}

void button() {
  a = digitalRead(3);
  while (a = HIGH)   
  { ready(); }
}
void button() {
  a = digitalRead(3);
  while (a == HIGH)     //oops
  { ready(); 
  a = digitalRead(3);
  }
}

Ah, heck. I had actually tried the double equal because I understood that from the language tutorial, but it didn't work until I also added the second instance of read as per DrDiettrich.

Appreciate the help making it work, but why does the state need to be tested again?

You can use
while (digitalRead(3))
to test the button before each iteration.

That's how I though it should initially work, but kept getting errors .. too many - darn i forget. I'll have to back and re write that and come back.

AH! had the bracket in the wrong place.

void button2() {while (digitalRead(3 == HIGH)){ready();} // instead of

void button2() {while (digitalRead(3) == HIGH){ready();}

I'm slowly getting it. I try not to bug everyone too much, but thanks for the quick help.

The goal of this 'project' is simply to use all of the different commands and see how they work.

1 Like

Don't overlook the fact that it's all properly documented online.

Thanks, I take that as a compliment.

Some may say I like to bang my head against the wall, but I like to know WHY something works, not that it just does.

Learned the 6502 the 'hard' way when Reagan was cracking jokes .. and I was pretty decent - although so far this has been a bit harder, (mostly no time anymore) but it's coming.

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