Första koden :D Lite frågor om den.

Jag har nu börjat med min kodning av Arduino och har börjat med ett enkelt program som skall läsa av en knapp och tända en lysdiod, dioden skall sedan köras på timer efter att knappen har släppts.

Tanken är att om jag trycker in knappen igen under tiden som timern körs så skall den resetta timern och börja om igen efter jag släpper knappen.

Frågor:
1: Jag får inte timer funktionen att resettas om jag trycker in knappen under tiden den körs, kan någon tipsa om vad jag bör ändra.

2: När jag trycker in knappen då får jag upp att timer har startats 2 gånger i följd men timer tiden stämmer med den sista Timer on, vad är fel?

3: Nu till den största frågan av alla, jag älskar att få tips och kritik om hur jag bör förenkla saker så även denna kod, tipsa gärna hur jag förbättra och korta ner koden.

/* This is a program to check and run a interior light*/
// Include external libraries
#include <Bounce2.h>

// Define ports to be used with Bounce class
#define BtnPin 2                              // The number of the button pin

// Create class objects
Bounce debo = Bounce();                       // Create a Bounce object called debo

// Set variables used in the program
const int LedPin = 9;                         // The number of the LED pin
const long OnTimer = 5000;                    // The interval that the LED will be on
int LedState = LOW;                           // Set LedState to low
int CurrBtnState = HIGH;                      // This will store the current state of the button
int PrevBtnState = HIGH;                      // This will store the previous state of the button
unsigned long CurrMillis = 0;                 // This holds the current Millis time value
unsigned long PrevMillis = 0;                 // will store last time LED was updated

// Initialize setup
void setup() {
  pinMode(LedPin, OUTPUT);                    // Set the LedPin pin as output:
  pinMode(BtnPin, INPUT);                     // Set the BtnPin as input
  debo.attach(BtnPin);                        // Setup the debounce instance
  debo.interval(5);                           // Set the debounce time interval to 5 ms
  Serial.begin(9600);                         // Setup serial communication
}

// Start main code, here you will run all your code for the program
void loop() {
  debo.update();                              // Update the bounce instance
  CurrBtnState = debo.read();                 // Read the current debounced button state and store it
  CurrMillis = millis();                      // Set the current value of clock to CurrMillis
  
  // Check if the button has changed and start the timer if so
  if (CurrBtnState != PrevBtnState){
    if (PrevMillis == 0){
      Serial.println("Timer on...");          // Send a serial message that the timer has started
      PrevMillis = CurrMillis;                // Check of PrevMillis has been used if not set it to start the timer
      LedState = HIGH;                        // Light up LED
    } else if (CurrMillis - PrevMillis >= OnTimer){
      Serial.println("Timer off...");         // Send a serial message that the timer has stopped
      LedState = LOW;                         // Set the LedState to LOW to turn of the LED after the timer has expired
      PrevBtnState = CurrBtnState;            // Set the button state varibles to same to reset the timer
      PrevMillis = 0;                         // Set the PrevMillis to 0 to reset the timer
    }

  // Check if button state has has become active
  if (CurrBtnState == LOW){
    LedState = HIGH;                          // Set the LedState to HIGH to turn on the LED    
    PrevMillis = 0;                           // Set the PrevMillis if the Btn state changes during the Timer is running
    PrevBtnState = CurrBtnState;              // Set the Previous button state to the current one, this is used for starting the timer
  }
  }
  digitalWrite(LedPin, LedState);             // Set the Output pin to its desired state
}