0

program which won't time out properly
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

program which won't time out properly

by LLavigne on Fri May 30, 2008 2:21 am

I have a piece of code which is supposed to veer to blinking the blue leds on pin 12 rapidly after 1000*12*12*12 microseconds (28'48")
And it doesn't work.
I tested it with 1000 and it works 1000*12 and 1000*24 it works but 1000*36 doesn't work, it never stops (move out of the // blink every 12 seconds loop)
WHY ?

Code: Select all | TOGGLE FULL SIZE
/* samadhi
*/

int ledPin = 13;             
int ledPinEnd = 12;
int ledPinBeat = 0;
unsigned long endTimer = 1000*36;
unsigned long startTimer;

void setup()                 
{
  startTimer = millis();
  pinMode(ledPin, OUTPUT);     
  pinMode(ledPinEnd, OUTPUT);     
  pinMode(ledPinBeat, OUTPUT);     
  digitalWrite(ledPinBeat, LOW);
}

void loop()
{
  if (millis()-startTimer < endTimer)
  { // blink every 12 seconds
    for (int k = 0; k<11;k++)
    {
      digitalWrite(ledPinBeat, HIGH);
      delay(100);
      digitalWrite(ledPinBeat, LOW);
      delay(900);
    }
    digitalWrite(ledPin, HIGH);   
    delay(900);                 
    digitalWrite(ledPin, LOW);   
    delay(100);
  }
  else
  { // finish
    digitalWrite(ledPinEnd, HIGH); 
    delay(100);
    digitalWrite(ledPinEnd, LOW); 
    delay(100);
    digitalWrite(ledPinEnd, HIGH); 
    delay(100);
    digitalWrite(ledPinEnd, LOW);   
    delay(100);
  }
}
_____________
www.previs.org
LLavigne
 
Posts: 6
Joined: Thu May 08, 2008 4:52 pm

by mtbf0 on Fri May 30, 2008 7:41 am

my guess is that 1000 * 16 is being performed as a 16 bit integer calculation that gives a negative result. if this is sign extended and stuck into your unsigned long it's going to be a very large number indeed. you might try leaving your gizmo running for a really long time and see if it finishes eventually or you could initialize endtimer to 1000UL * 16UL.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.