Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const byte interruptLed = 13; // LED controlled by interrupt pin
- const byte blinkLed = 4; // LED to flash for interrupt testing
- int delayTime = 1000; // Delay time for blinking LED
- const byte interruptPin = 2; // Button connected between here and GND
- //long unsigned int previousMillis = 0;
- const long int interval = 5000;
- // Set to 'volatile' to allow the ISR to change the variable
- volatile byte state = LOW; // State of interrupt LED.
- void setup()
- {
- Serial.begin(9600);
- pinMode(blinkLed, OUTPUT);
- pinMode(interruptLed, OUTPUT);
- pinMode(interruptPin, INPUT_PULLUP);
- // Mode: CHANGE -> on when button pushed. RISING -> Latches On
- attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
- }
- void loop()
- {
- flash(blinkLed);
- digitalWrite(interruptLed, state);
- }
- //-------------------------------------------------------------------------
- // LED flash function
- void flash(byte pin)
- {
- //static variables will only be created and initialized the first time a function is called.
- static boolean ledState = LOW;
- static long previousMillis = 0;
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval)
- {
- previousMillis = currentMillis;
- ledState = !ledState;
- digitalWrite(pin,ledState);
- }
- }
- //-------------------------------------------------------------------------
- // Interrupt Function (ISR)
- void blink()
- {
- state = !state;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement