#include "segment.pio.h"

uint8_t digits[] = {
  0b11000000, // 0
  0b11111001, // 1
  0b10100100, // 2 
  0b10110000, // 3
  0b10011001, // 4
  0b10010010, // 5
  0b10000010, // 6
  0b11111000, // 7
  0b10000000, // 8
  0b10011000, // 9
};

const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 10;
const uint8_t button_pin = 15; // Change this to the actual GPIO pin you are using for the button

bool buttonPressed = false;
uint32_t lastButtonPressTime = 300;
const uint32_t debounceDelay = 500; // Adjust the debounce delay as needed

void setup() {
  Serial1.begin(115200);
  Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");

  // Load the PIO program and initialize the machine
  auto offset = pio_add_program(pio0, &segment_program);
  segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);

  pinMode(button_pin, INPUT);
}

void displayNumber(uint value) {
  pio_sm_put(pio0, 0, 
    digits[value / 1000 % 10] << 24 |
    digits[value / 100 % 10] << 16 |
    digits[value / 10 % 10] << 8 |
    digits[value % 10]
  );
}

int i = 0;
void loop() {
  // Check if the button is pressed
  if (digitalRead(button_pin) == HIGH) {
    // Check for debounce
    if (millis() - lastButtonPressTime >= debounceDelay) {
      buttonPressed = true;
      lastButtonPressTime = millis();
    }
  } else {
    buttonPressed = false;
  }

  if (buttonPressed) {
    // Increment the count when the button is pressed
    i++;
    displayNumber(i);
    delay(100); // Adjust the delay as needed to control counting speed
  }
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT