I am stuck with some weird timing issues. I am trying to blink an LED, when triggered with an interrupt. I am trying to do this with the millis() function, and not the delay() function, outside of the ISR. The issue is, my LED blinks very fast (greater than once a second). I can increase the delay period in my millis() function, but the program stops. Interesting to note, is that if I do use the delay() function, the timing is still way too fast. Thoughts?Preformatted text
int period = 1000;
unsigned long time_now = 0;
int LED = LED_BUILTIN;
int startPin = 2;
volatile byte ledState = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(startPin), blinker, CHANGE);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
}
void blinker() {
blinking();
}
void blinking() {
int loopControl = 0;
while (loopControl != 1) {
time_now = millis();
if (time_now > period) {
ledState != ledState;
digitalWrite(LED_BUILTIN, ledState);
Serial.println("Blinking");
}
}
}
This creates an endless loop as loopControl is always 0.
You're basically calling this from inside the interrupt... so everything in blinking is running inside the interrupt. The interrupt should be as quick as possible, and avoid anything that uses interrupts. It is better to just set a variable (volatile) and evaluate that in the rest of your code.
Once your program has been running for 1 second this will always be true, hence the rapid flashing.