Go Down

Topic: I need to execute a block of code several number of times. (Read 160 times) previous topic - next topic

flash_os

I need to execute a block of code several number of times.
How to do that without goto?

In general:
Button==1 execute the loop code.
Button==0 stop and wait for Button to be again Button==1.

So basically, I'm trying to switch between 2 loops with button. 

Thank you.
ARDUINO UNO R3
I just recently retired & I want to take up electronics as a hobby. I'm a newbie.

larryd

Show us your attempt so far.

Nice cat.
.
No technical PMs.
The last thing you did is where you should start looking.

INTP

use while loops with a condition within each loop to check if button state changes from what started the block. If button changes, break;

flash_os

Code: [Select]
void loop(void)
{

 int  value = client.getValue("57d164ef762542127772a44");
 delay(1000);
  
 Serial.println("Got this before while:");
 Serial.println( value);

 
 while( value > 0 ){      // Button on
    int   value = client.getValue("57d164ef762542127772a44");
    delay(1000);
  
    Serial.println("Got this inside while:");
    Serial.println( value);
 }
}


But even after value changed back to 0 its seems to stuck inside while loop:

Code: [Select]
Got this inside while:
0


What I'm doing wrong?
ARDUINO UNO R3
I just recently retired & I want to take up electronics as a hobby. I'm a newbie.

Delta_G

You're creating a new variable named value inside the while loop. It's not the same variable you're checking as the condition. The one you're using for the condition never gets updated.  It's not the same one you see printed.  The one you're printing is the new one.

Lise the "int" in front of value in the while loop. Putting the type creates a new variable. Two variables with the same name is almost never a good idea.
If at first you don't succeed, up - home - sudo - enter.

Robin2

I need to execute a block of code several number of times.
How to do that without goto?
Put the code that needs to be repeated into a function. Then you can call the function whenever needed.

Have a look at Planning and Implementing a Program to see an example of the use of functions.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

flash_os

Thanks guys.
It's worked!
ARDUINO UNO R3
I just recently retired & I want to take up electronics as a hobby. I'm a newbie.

Go Up