Untitled

 avatar
unknown
python
3 years ago
1.2 kB
3
Indexable
#include <Arduino.h>
volatile int button_count; // Trigger
int totalLoops; // counts the number of triggering of the alarm
#define LED_PIN LED_BUILTIN
#define PIN_BOTON 2
enum system_state {
 LED_OFF,
 LED_ON
 };
system_state state = LED_OFF;
void switch_OFF(void) {
 digitalWrite(LED_PIN, LOW);
 state = LED_OFF;
 Serial.print("\n[LED_OFF]\n");
}
void switch_ON(void) {
 digitalWrite(LED_PIN, HIGH);
 state = LED_ON;
 Serial.print("\n[LED_ON]\n");
}
void button_isr (void) {
 button_count++;
 Serial.print("\nButton has been pressed (%d times)!!!!\n", button_count);
 if(state == LED_OFF) {
 switch_ON(); // Turn off LED
 }
 else { // state == LED_ON
 switch_OFF(); // Turn on LED
 }
}
void setup() {
 Serial.begin(9600);
 while (!Serial);
 delay(100);
 Serial.println("[SYSTEM START]");
 

 // Configure LED output
 pinMode(LED_PIN, OUTPUT);
 digitalWrite(LED_PIN, LOW);

 // Configure button pin input
 pinMode(PIN_BOTON, INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(PIN_BOTON), button_isr, RISING);
}
void loop() {
 totalLoops++;
 delay(100);
 // Do whatever...
 Serial.print("..%d\n", totalLoops);
 }
Editor is loading...