Sports Timer - more improvements

Now that I am getting my head around writing code it is getting easier to make improvements.

The timer now remembers the previous set time and returns to this at the end of the count down sequence. This is essential for doing repetitions of exercises and multiple rounds of a game.

I have also fixed bugs that allowed the time to be set beyond the parameters of the light display

I am so happy!!!!!

Here is a video...


and here is the code...

// use buttons to set the timer
// Press button to activate the timer.
// All lights come on in a fast bottom to top sequence,
// then lights turn off one at a time, slowly in a top to bottom sequence
// Includes buzzers for audio feedback

#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>

#define BUTTON_PINA   4
#define BUTTON_PINB   5
#define BUTTON_PINC   3
#define PIXEL_PIN    6  // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 16 // Number of NeoPixels
#define TIME_WIPE 500 // Milliseconds of time between reverse wipe

const int buzzerPin = 8; // set the piezo pin number

int SCORE = 0;

// Declare the NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

Bounce debouncerA = Bounce(); // Instantiate a Bounce object - reads a button push
Bounce debouncerB = Bounce(); // Instantiate a Bounce object - reads a button push
Bounce debouncerC = Bounce(); // Instantiate a Bounce object - reads a button push




void setup() {
  Serial.begin (9600);

  debouncerA.attach(BUTTON_PINA, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerA.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerB.attach(BUTTON_PINB, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerB.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerC.attach(BUTTON_PINC, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerC.interval(25); // Use a debounce interval of 25 milliseconds

  pinMode(buzzerPin, OUTPUT); // set the buzzer pin as an output

  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'
}

void loop() {

  debouncerA.update(); // Update the Bounce instance
  debouncerB.update(); // Update the Bounce instance
  debouncerC.update(); // Update the Bounce instance

  uint32_t green = strip.Color(55, 0, 0); // Define LED Colours
  uint32_t blue = strip.Color(0, 0, 255);
  uint32_t red = strip.Color(0, 55, 0);
  uint32_t off = strip.Color(0, 0, 0);



  // SETTING THE TIMER
  // Get current button state
  if ((SCORE > 1) && (debouncerA.fell())) // press button A
  {
    Serial.println ("minusTime"); // write the button state
    SCORE = SCORE - 1; // minus a pixel count
    tone(buzzerPin, 7000, 30); //play tone ( pin number, frequency in hertz, duration in milliseconds);
    strip.clear();
    strip.fill(green, 0, SCORE); // turns on the LEDs(colour, starting LED, number of LEDs)
    strip.show(); // updates the LED bar;
  }

  if ((SCORE < PIXEL_COUNT) && (debouncerB.fell())) // press button B
  {
    Serial.println ("plusTime"); // write the button state
    SCORE = SCORE + 1; // plus a pixel count
    tone(buzzerPin, 10000, 30); //play tone ( pin number, frequency in hertz, duration in milliseconds)
    strip.fill(green, 0, SCORE); // turns on the LEDs(colour, starting LED, number of LEDs)
    strip.show(); // updates the LED bar;
  }


  // Countdown Function Here


  int newSCORE = SCORE;

  if ((SCORE > 0) && (debouncerC.fell())) // press button C
  {
    tone(buzzerPin, 7000, 30);
    for (int s = SCORE; s > 0; s --)
    {
      delay (TIME_WIPE);
      SCORE = SCORE - 1; // minus a pixel count
      strip.clear();
      strip.fill(green, 0, SCORE); // turns on the LEDs(colour, starting LED, number of LEDs)
      strip.show(); // updates the LED bar;
    }

    // Alert to end of time
    tone(buzzerPin, 500, 600); //( pin number, frequency in hertz, duration in milliseconds);
    strip.fill(red, 0, PIXEL_COUNT); // LIGHT IT UP
    strip.show();
    delay (TIME_WIPE);
    strip.clear();
    strip.show();
    delay (TIME_WIPE);
    strip.fill(green, 0, newSCORE); // RESET THE TIMER
    strip.show();
  }
  SCORE = newSCORE; // sets SCORE to the saved score value

}

Comments