Advertisement
lucasmcg

Interrupt Test Ver 2 (Flashing without delay)

Nov 22nd, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const byte interruptLed = 13; // LED controlled by interrupt pin
  2. const byte blinkLed = 4; // LED to flash for interrupt testing
  3. int delayTime = 1000; // Delay time for blinking LED
  4. const byte interruptPin = 2; // Button connected between here and GND
  5.  
  6. //long unsigned int previousMillis = 0;
  7. const long int interval = 5000;
  8.  
  9. // Set to 'volatile' to allow the ISR to change the variable
  10. volatile byte state = LOW; // State of interrupt LED.
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.   pinMode(blinkLed, OUTPUT);
  16.   pinMode(interruptLed, OUTPUT);
  17.   pinMode(interruptPin, INPUT_PULLUP);
  18.   // Mode: CHANGE -> on when button pushed. RISING -> Latches On
  19.   attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
  20. }
  21.  
  22. void loop()
  23.  {
  24.   flash(blinkLed);
  25.   digitalWrite(interruptLed, state);
  26.  }
  27.  
  28. //-------------------------------------------------------------------------
  29.  
  30. // LED flash function
  31. void flash(byte pin)
  32. {
  33.   //static variables will only be created and initialized the first time a function is called.
  34.   static boolean ledState = LOW;
  35.   static long previousMillis = 0;
  36.   unsigned long currentMillis = millis();
  37.   if (currentMillis - previousMillis >= interval)
  38.   {
  39.     previousMillis = currentMillis;
  40.     ledState = !ledState;
  41.     digitalWrite(pin,ledState);
  42.   }
  43. }
  44.  
  45. //-------------------------------------------------------------------------
  46.  
  47. // Interrupt Function (ISR)
  48. void blink()
  49.   {
  50.   state = !state;
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement