'Knock Your Lights Out' Code

After a long day and with the help of a friend I have managed to make the code work for one of the stand alone games of the Interactive Armour.

The game works like this...

On start, 5 sections of each players armour lights up.

If you hit your opponents vest, the section that you hit registers the hit by turning off the light.

The first player to knock out all of the opponents lights is the winner.

This video shows 3 out of the 5 lights turning off as I ran out of push buttons.




The code should have been simple, but as I am still very new to Arduino it wasn't.

Here it is...

// 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

int piezoPin = 11; // set the piezo pin number

const int ledA = 3; // these are the LED pins, constants won't change
const int ledB = 4;
const int ledC = 5;
const int ledD = 6;
const int ledE = 7;

const int BUTTON_A = 8; // these are the button pins, constants won't change
const int BUTTON_B = 9;
const int BUTTON_C = 10;
const int BUTTON_D = 12;
const int BUTTON_E = 13;

bool ledAON = true;
bool ledBON = true;
bool ledCON = true;
bool ledDON = true;
bool ledEON = true;

bool ledAOFF = false;
bool ledBOFF = false;
bool ledCOFF = false;
bool ledDOFF = false;
bool ledEOFF = false;


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


  pinMode (ledA, OUTPUT);
  pinMode (ledB, OUTPUT);
  pinMode (ledC, OUTPUT);
  pinMode (ledD, OUTPUT);
  pinMode (ledE, 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

  digitalWrite (ledA, ledAON);
  digitalWrite (ledB, ledBON);
  digitalWrite (ledC, ledCON);
  digitalWrite (ledD, ledDON);
  digitalWrite (ledE, ledEON);

noTone(piezoPin); // turns off piezo

  if (debouncerA.fell()) // press button A
  {
    ledAON = false;
    Serial.print("hitA"); // remembers the button press
    digitalWrite (ledA, ledAOFF);
  }

  if (debouncerB.fell())    // press button B
  {
    ledBON = false;
    Serial.print("hitB"); // remembers the button press
    digitalWrite (ledB, ledBOFF);
  }

  if (debouncerC.fell())    // press button C
  {
    ledCON = false;
    Serial.print("hitC"); // remembers the button press
    digitalWrite (ledC, ledCOFF);
  }

  if (debouncerD.fell())    // press button D
  {
    ledDON = false;
    Serial.print("hitD"); // remembers the button press
    digitalWrite (ledD, ledDOFF);
  }

  if (debouncerE.fell())    // press button E
  {
    ledEON = false;
    Serial.print("hitE"); // remembers the button press
    digitalWrite (ledE, ledEOFF);
  }


  if (!(ledAON && ledBON && ledCON && ledDON && ledEON)); {
    tone(piezoPin, 294, 50); //( pin number, frequency in hertz, duration in milliseconds);
    noTone(piezoPin); // turns off piezo
 
    // use this to make it play a tone
    // tone(piezoPin, 294, 50); //( pin number, frequency in hertz, duration in milliseconds);
    // noTone(piezoPin);
  }
}

Comments