#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "i2c-display-lib.hpp"
#define BUTTON_PIN 26
#define LED_PIN 12
#define I2C_SDA 6
#define I2C_SCL 7
bool counting = false;
uint32_t seconds = 0;
void printIntToDisplay(int num) {
// Temporary buffer to hold the integer as a string
char buffer[20];
snprintf(buffer, sizeof(buffer), "%d", num); // Convert integer to string
i2cdisplay::print(buffer); // Print the string
}
void button_callback(uint gpio, uint32_t events) {
counting = !counting; // Toggle counting on button press
}
int main() {
i2cdisplay::init(I2C_SDA, I2C_SCL);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &button_callback);
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
i2cdisplay::home();
i2cdisplay::clear();
i2cdisplay::print("Press button to start");
sleep_ms(2000); // Display for 2 seconds
while (true) {
if (counting) {
seconds++;
gpio_put(LED_PIN, 1); // Turn on LED
sleep_ms(250); // Blink at 1 Hz
gpio_put(LED_PIN, 0); // Turn off LED
sleep_ms(250);
} else {
// Perform actions opposite to the if block
seconds--;
gpio_put(LED_PIN, 1); // Turn off LED
sleep_ms(250); // Delay to maintain the same timing as the if block
gpio_put(LED_PIN, 0); // Turn on LED
sleep_ms(250);
}
i2cdisplay::clear();
i2cdisplay::home();
i2cdisplay::print("Seconds: ");
printIntToDisplay(seconds); // Display the counter value
sleep_ms(1000);
}
}