Pages: 1 [2]   Go Down
Author Topic: [SOLVED] Is there any way to "delay()" (or equivalent) within an interrupt?  (Read 1051 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
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: 495
Posts: 19036
Lua rocks!
View Profile
WWW
 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: 495
Posts: 19036
Lua rocks!
View Profile
WWW
 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
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
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: