Pages: 1 [2]   Go Down
Author Topic: [SOLVED] Is there any way to "delay()" (or equivalent) within an interrupt?  (Read 662 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46084
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
     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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Steady, Paul. All the programming texts recommend int for integers.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

is the same as

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
     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?  smiley-wink

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for (byte x = 3; x >= 0; x--){
       delay(1000);
       Serial.print(x);
     }

That is because a byte type is always >= 0.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46084
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Either way is this your point?
Yes it was. I'm glad to see you picked up the proper solution.
Logged

Pages: 1 [2]   Go Up
Jump to: