Go Down

Topic: [SOLVED] Is there any way to "delay()" (or equivalent) within an interrupt? (Read 1 time) previous topic - next topic

PaulS

Code: [Select]
     for (int x = 4; x >= 1; x--){
       delay(1000);
       Serial.print(x);
     }

Are you sure you don't want to use a long (or maybe a long long) for your index variable? After all, it does need to count all the way up to 4.

Nick Gammon

Steady, Paul. All the programming texts recommend int for integers.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

wballum


Code: [Select]
    STATE = digitalRead(2);
    if (STATE == 0){
      StopforHuman(); // Ouroboros ;)
    }


is the same as

Code: [Select]
while (digitalRead(2) == 0)
  ; // do nothing


with no recursion.

You do know that the processor will appear to just stop and actually do nothing while this loop is running?


This worked very well thanks.

wballum


Code: [Select]
     for (int x = 4; x >= 1; x--){
       delay(1000);
       Serial.print(x);
     }

Are you sure you don't want to use a long (or maybe a long long) for your index variable? After all, it does need to count all the way up to 4.


I assume you were trying to be funny or make fun of me to drive a point.   Either way is this your point?  ;)

Code: [Select]
for (byte x = 4; x >= 1; x--){
       delay(1000);
       Serial.print(x);
     }

Nick Gammon

PaulS likes to encourage people to minimize their use of resources, hence the jesting reference to using an even larger data type.

Using "byte" will use slightly less program memory and execution time, but be aware that it is an unsigned type, so if you had done this it wouldn't have worked:

Code: [Select]

for (byte x = 3; x >= 0; x--){
       delay(1000);
       Serial.print(x);
     }


That is because a byte type is always >= 0.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
Either way is this your point?

Yes it was. I'm glad to see you picked up the proper solution.

Go Up