Advertisement
lucasmcg

Interrupt Test Ver 1 (Flashing with delay)

Nov 22nd, 2019
151
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 = 500; // Delay time for blinking LED
  4. const byte interruptPin = 2; // Button connected between here and GND
  5.  
  6. // Set to 'volatile' to allow the ISR to change the variable
  7. volatile byte state = LOW; // State of interrupt LED.
  8.  
  9. void setup()
  10. {
  11.   pinMode(blinkLed, OUTPUT);
  12.   pinMode(interruptLed, OUTPUT);
  13.   pinMode(interruptPin, INPUT_PULLUP);
  14.   // Mode: CHANGE -> on when button pushed. RISING -> Latches On
  15.   attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
  16. }
  17.  
  18. void loop()
  19.  {
  20.   flash(blinkLed);
  21.   digitalWrite(interruptLed, state);
  22.  }
  23.  
  24. //-------------------------------------------------------------------------
  25.  
  26. // LED flash function
  27. void flash(byte pin)
  28. {
  29.  digitalWrite(pin, HIGH);
  30.  delay(delayTime);
  31.  digitalWrite(pin, LOW);
  32.  delay(delayTime);  
  33. }
  34.  
  35. //-------------------------------------------------------------------------
  36.  
  37. // Interrupt Function (ISR)
  38. void blink()
  39.   {
  40.   state = !state;
  41.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement