#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#define NUM_BUTTONS 4
#define DEBOUNCE_TIME 80000 // Debounce time in microseconds (adjust as needed)
const uint BUTTON_PINS[NUM_BUTTONS] = {2, 3, 4, 5}; // Replace with the actual GPIO pin numbers for the buttons
const uint LED_PINS[NUM_BUTTONS] = {21, 20, 19, 18}; // Replace with the actual GPIO pin numbers for the LEDs
const uint LED_PINS_unlock = 27;
const uint LED_PINS_ALARM =26;
void init_gpio() {
gpio_init(LED_PINS_unlock);
gpio_set_dir(LED_PINS_unlock, GPIO_OUT);
gpio_init(LED_PINS_ALARM);
gpio_set_dir(LED_PINS_ALARM, GPIO_OUT);
for (int i = 0; i < NUM_BUTTONS; i++) {
gpio_init(BUTTON_PINS[i]);
gpio_init(LED_PINS[i]);
gpio_set_dir(BUTTON_PINS[i], GPIO_IN);
gpio_set_dir(LED_PINS[i], GPIO_OUT);
gpio_pull_up(BUTTON_PINS[i]);
}
}
void handle_button(int button_index) {
static bool last_state[NUM_BUTTONS] = {true};
bool current_state = gpio_get(BUTTON_PINS[button_index]);
if (current_state != last_state[button_index]) {
// Button state has changed, handle the event
if (current_state) {
// Button is released
printf("Button %d released\n", button_index + 1);
gpio_put(LED_PINS[button_index], false); // Turn off the LED
} else {
// Button is pressed
printf("Button %d pressed\n", button_index + 1);
gpio_put(LED_PINS[button_index], true); // Turn on the LED
}
}
last_state[button_index] = current_state;
}
void activate_alarm() {
// Code to activate the alarm, turn on the red alarm LED
gpio_put(ALARM_LED_RED_PIN, true);
gpio_put(ALARM_LED_OTHER_PIN, true); // You can adjust this line if the other alarm LED is different
}
int main() {
stdio_init_all();
init_gpio();
while (1) {
bool any_button_pressed = false;
for (int i = 0; i < NUM_BUTTONS; i++) {
handle_button(i);
sleep_us(DEBOUNCE_TIME);
// Check if any button is pressed
any_button_pressed |= !gpio_get(BUTTON_PINS[i]);
}
// If any button is pressed, activate the alarm
if (any_button_pressed) {
activate_alarm();
}
}
return 0;
}
/*
#define NUM_BUTTONS 4
#define DEBOUNCE_TIME 2000 // Debounce time in microseconds (adjust as needed)
const uint BUTTON_PINS[NUM_BUTTONS] = {2, 3, 4, 5}; // Replace with the actual GPIO pin numbers for the buttons
const uint LED_PINS[NUM_BUTTONS] = {21, 20, 19, 18}; // Replace with the actual GPIO pin numbers for the LEDs
void init_gpio() {
for (int i = 0; i < NUM_BUTTONS; i++) {
gpio_init(BUTTON_PINS[i]);
gpio_init(LED_PINS[i]);
gpio_set_dir(BUTTON_PINS[i], GPIO_IN);
gpio_set_dir(LED_PINS[i], GPIO_OUT);
gpio_pull_up(BUTTON_PINS[i]);
}
}
void debounce_timer_callback(struct repeating_timer *t, void *user_data) {
int button_index = (int)user_data;
static bool last_state[NUM_BUTTONS] = {true};
bool current_state = gpio_get(BUTTON_PINS[button_index]);
if (current_state != last_state[button_index]) {
// Button state has changed, handle the event
if (current_state) {
// Button is released
gpio_put(LED_PINS[button_index], false);
} else {
// Button is pressed
gpio_put(LED_PINS[button_index], true);
}
}
last_state[button_index] = current_state;
}
int main() {
stdio_init_all();
init_gpio();
struct repeating_timer debounce_timers[NUM_BUTTONS];
for (int i = 0; i < NUM_BUTTONS; i++) {
add_repeating_timer_us(DEBOUNCE_TIME, debounce_timer_callback, (void*)i, &debounce_timers[i]);
}
while (1) {
tight_loop_contents();
}
return 0;
}
*/