The best thing about finishing a project early is that you have time for refinements.
I awoke at 3AM with inspiration on how to fix an annoying bug in the code that was stopping a not necessary, but desirable function.
Here is the new code...
// Punch Your Lights Out Vest Code
// on start: vest strike zones light up
// when a strike zone is activated (button press) the corresponding light goes out (and a buzzer sounds)
// when all lights are out a buzzer sounds
#include <Bounce2.h>
Bounce debouncerA = Bounce(); // Instantiate a Bounce object - ie. reads a button push
Bounce debouncerB = Bounce(); // Instantiate a Bounce object
Bounce debouncerC = Bounce(); // Instantiate a Bounce object
Bounce debouncerD = Bounce(); // Instantiate a Bounce object
Bounce debouncerE = Bounce(); // Instantiate a Bounce object
Bounce debouncerF = Bounce(); // Instantiate a Bounce object
const int ledA = 2; // BLACK these are the LED pins, constants won't change
const int ledB = 3; // WHITE
const int ledC = 4; // GREY
const int ledD = 5; // PURPLE
const int ledE = 6; // BLUE
const int ledF = 7; // GREEN
const int BUTTON_A = 8; // YELLOW these are the button pins, constants won't change
const int BUTTON_B = 9; // ORANGE
const int BUTTON_C = 10; // RED
const int BUTTON_D = 11; // BLACK
const int BUTTON_E = 12; // WHITE
const int BUTTON_F = 13; // GREY
const int buzzerPin = 14; // set the piezo pin number to analogue pin 0
bool ledAON = true;
bool ledBON = true;
bool ledCON = true;
bool ledDON = true;
bool ledEON = true;
bool ledFON = true;
bool ledAOFF = false;
bool ledBOFF = false;
bool ledCOFF = false;
bool ledDOFF = false;
bool ledEOFF = false;
bool ledFOFF = false;
#define frequency1 500 // sets the frequency #1
#define duration1 300 // sets the duration #1
#define frequency2 2000 // sets the frequency #2
#define duration2 800 // sets the duration #2
void setup()
{
debouncerA.attach(BUTTON_A, 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_B, 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_C, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerC.interval(25); // Use a debounce interval of 25 milliseconds
debouncerD.attach(BUTTON_D, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerD.interval(25); // Use a debounce interval of 25 milliseconds
debouncerE.attach(BUTTON_E, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerE.interval(25); // Use a debounce interval of 25 milliseconds
debouncerF.attach(BUTTON_F, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerF.interval(25); // Use a debounce interval of 25 milliseconds
pinMode (ledA, OUTPUT);
pinMode (ledB, OUTPUT);
pinMode (ledC, OUTPUT);
pinMode (ledD, OUTPUT);
pinMode (ledE, OUTPUT);
pinMode (ledF, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
debouncerA.update(); // Update the Bounce instance
debouncerB.update(); // Update the Bounce instance
debouncerC.update(); // Update the Bounce instance
debouncerD.update(); // Update the Bounce instance
debouncerE.update(); // Update the Bounce instance
debouncerF.update(); // Update the Bounce instance
digitalWrite (ledA, ledAON); // turn on all the LEDs
digitalWrite (ledB, ledBON);
digitalWrite (ledC, ledCON);
digitalWrite (ledD, ledDON);
digitalWrite (ledE, ledEON);
digitalWrite (ledF, ledFON);
if (debouncerA.fell()) // press button A
{
ledAON = false;
Serial.print("hitA"); // remembers the button press
digitalWrite (ledA, ledAOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerB.fell()) // press button B
{
ledBON = false;
Serial.print("hitB"); // remembers the button press
digitalWrite (ledB, ledBOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerC.fell()) // press button C
{
ledCON = false;
Serial.print("hitC"); // remembers the button press
digitalWrite (ledC, ledCOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerD.fell()) // press button D
{
ledDON = false;
Serial.print("hitD"); // remembers the button press
digitalWrite (ledD, ledDOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerE.fell()) // press button E
{
ledEON = false;
Serial.print("hitE"); // remembers the button press
digitalWrite (ledE, ledEOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerF.fell()) // press button F
{
ledFON = false;
Serial.print("hitF"); // remembers the button press
digitalWrite (ledF, ledFOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
// once all buttons have been hit once play an end of game tone then STOP all functions
if (!ledAON && !ledBON && !ledCON && !ledDON && !ledEON && !ledFON)
{ delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
tone(buzzerPin, frequency1, duration1); // game over tone
delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
delay (500);
tone(buzzerPin, frequency1, duration1); // game over tone
delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
delay (500);
tone(buzzerPin, frequency1, duration1); // game over tone
noTone(buzzerPin);
while (1);
}
}
I awoke at 3AM with inspiration on how to fix an annoying bug in the code that was stopping a not necessary, but desirable function.
Here is the new code...
// Punch Your Lights Out Vest Code
// on start: vest strike zones light up
// when a strike zone is activated (button press) the corresponding light goes out (and a buzzer sounds)
// when all lights are out a buzzer sounds
#include <Bounce2.h>
Bounce debouncerA = Bounce(); // Instantiate a Bounce object - ie. reads a button push
Bounce debouncerB = Bounce(); // Instantiate a Bounce object
Bounce debouncerC = Bounce(); // Instantiate a Bounce object
Bounce debouncerD = Bounce(); // Instantiate a Bounce object
Bounce debouncerE = Bounce(); // Instantiate a Bounce object
Bounce debouncerF = Bounce(); // Instantiate a Bounce object
const int ledA = 2; // BLACK these are the LED pins, constants won't change
const int ledB = 3; // WHITE
const int ledC = 4; // GREY
const int ledD = 5; // PURPLE
const int ledE = 6; // BLUE
const int ledF = 7; // GREEN
const int BUTTON_A = 8; // YELLOW these are the button pins, constants won't change
const int BUTTON_B = 9; // ORANGE
const int BUTTON_C = 10; // RED
const int BUTTON_D = 11; // BLACK
const int BUTTON_E = 12; // WHITE
const int BUTTON_F = 13; // GREY
const int buzzerPin = 14; // set the piezo pin number to analogue pin 0
bool ledAON = true;
bool ledBON = true;
bool ledCON = true;
bool ledDON = true;
bool ledEON = true;
bool ledFON = true;
bool ledAOFF = false;
bool ledBOFF = false;
bool ledCOFF = false;
bool ledDOFF = false;
bool ledEOFF = false;
bool ledFOFF = false;
#define frequency1 500 // sets the frequency #1
#define duration1 300 // sets the duration #1
#define frequency2 2000 // sets the frequency #2
#define duration2 800 // sets the duration #2
void setup()
{
debouncerA.attach(BUTTON_A, 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_B, 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_C, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerC.interval(25); // Use a debounce interval of 25 milliseconds
debouncerD.attach(BUTTON_D, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerD.interval(25); // Use a debounce interval of 25 milliseconds
debouncerE.attach(BUTTON_E, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerE.interval(25); // Use a debounce interval of 25 milliseconds
debouncerF.attach(BUTTON_F, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerF.interval(25); // Use a debounce interval of 25 milliseconds
pinMode (ledA, OUTPUT);
pinMode (ledB, OUTPUT);
pinMode (ledC, OUTPUT);
pinMode (ledD, OUTPUT);
pinMode (ledE, OUTPUT);
pinMode (ledF, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
debouncerA.update(); // Update the Bounce instance
debouncerB.update(); // Update the Bounce instance
debouncerC.update(); // Update the Bounce instance
debouncerD.update(); // Update the Bounce instance
debouncerE.update(); // Update the Bounce instance
debouncerF.update(); // Update the Bounce instance
digitalWrite (ledA, ledAON); // turn on all the LEDs
digitalWrite (ledB, ledBON);
digitalWrite (ledC, ledCON);
digitalWrite (ledD, ledDON);
digitalWrite (ledE, ledEON);
digitalWrite (ledF, ledFON);
if (debouncerA.fell()) // press button A
{
ledAON = false;
Serial.print("hitA"); // remembers the button press
digitalWrite (ledA, ledAOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerB.fell()) // press button B
{
ledBON = false;
Serial.print("hitB"); // remembers the button press
digitalWrite (ledB, ledBOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerC.fell()) // press button C
{
ledCON = false;
Serial.print("hitC"); // remembers the button press
digitalWrite (ledC, ledCOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerD.fell()) // press button D
{
ledDON = false;
Serial.print("hitD"); // remembers the button press
digitalWrite (ledD, ledDOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerE.fell()) // press button E
{
ledEON = false;
Serial.print("hitE"); // remembers the button press
digitalWrite (ledE, ledEOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
if (debouncerF.fell()) // press button F
{
ledFON = false;
Serial.print("hitF"); // remembers the button press
digitalWrite (ledF, ledFOFF);
tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
}
// once all buttons have been hit once play an end of game tone then STOP all functions
if (!ledAON && !ledBON && !ledCON && !ledDON && !ledEON && !ledFON)
{ delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
tone(buzzerPin, frequency1, duration1); // game over tone
delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
delay (500);
tone(buzzerPin, frequency1, duration1); // game over tone
delay (500);
tone(buzzerPin, frequency2, duration2); // game over tone
delay (500);
tone(buzzerPin, frequency1, duration1); // game over tone
noTone(buzzerPin);
while (1);
}
}
Comments
Post a Comment