/*
Arduino | coding-help
2 Leds and 2 Buttons
snaikinha — 12/7/24 at 2:08 PM
what i want, i want 2 led that operate at different freq
and blink and the 2 buttons control each one of the leds on/off
see https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all
*/
#include "flasher.h"
const int NUM_BTNS = 2;
const int BTN_PINS[] = {3, 2};
const int LED_PINS[] = {5, 4};
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
int flashState[NUM_BTNS];
// construct the flasher objects
Flasher flash1(LED_PINS[0], 250, 1000); // pin, on time, off time
Flasher flash2(LED_PINS[1], 300, 300);
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnPressed = i + 1;
Serial.print("Button ");
Serial.print(btnPressed);
Serial.println(" pressed");
} else { // was just released
// do release things
}
delay(20); // debounce
}
}
return btnPressed;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber != 0) {
flashState[btnNumber - 1] = !flashState[btnNumber - 1];
}
if (flashState[0]) {
flash1.Update();
} else {
digitalWrite(LED_PINS[0], LOW);
}
if (flashState[1]) {
flash2.Update();
} else {
digitalWrite(LED_PINS[1], LOW);
}
}