/* Cassette RGB LED
Last edited by: Danielle Gonzalez, [email protected]
8/22/2023
Information about Setup
Top right bread board (From Left to Right): First Diagram, RFID Round 1, RFID Round 2, First Disk In, RGB LED
The right column from top to bottom is the First, Second, Third and Fourth RFID stations
The bread board directly under the Arduino is the fifth RFID station
The bread board top left (From Left to Right): Start Round 2, Upload, Success Pin, Failure Pin, 2 Disks In, 3 Disks In, Round 1 Success, Round 2 Success
Each round, when the white disk in button is pressed, it's respective red LED turns on.
All 4 (or 5 depending on the round) LEDs must be on in order for the upload to work
If all black RFID buttons are pressed (depending on the round) as well as the LEDs being on, then the round is a success
When the first round is a success, all Diagram LEDs are turned off as well as RGB LEDs and once the black Round 2 button is pressed, the next round can begin
The puzzle is ended once the Round 1 and Round 2 success lights are on
*/
// RGB LED Setup variables
const int FirstLEDRed = 6;
const int FirstLEDGreen = 7;
const int FirstLEDBlue = 8;
const int SecondLEDRed = 26;
const int SecondLEDGreen = 27;
const int SecondLEDBlue = 28;
const int ThirdLEDRed = 33;
const int ThirdLEDGreen = 34;
const int ThirdLEDBlue = 35;
const int FourthLEDRed = 47;
const int FourthLEDGreen = 48;
const int FourthLEDBlue = 49;
const int FifthLEDRed = 40;
const int FifthLEDGreen = 41;
const int FifthLEDBlue = 42;
//LED Diagram
const int FirstDiagram = 2;
const int SecondDiagram = 22;
const int ThirdDiagram = 29;
const int FourthDiagram = 43;
const int FifthDiagram = 36;
//Disk In Variables so that when they are pressed we know there is a cassette in that spot
const int FirstDiskIn = 5;
const int SecondDiskIn = 25;
const int ThirdDiskIn = 32;
const int FourthDiskIn = 46;
const int FifthDiskIn = 39;
//RFID Round One Variables
const int RFIDFirstRound1 = 3;
const int RFIDSecondRound1 = 23;
const int RFIDThirdRound1 = 30;
const int RFIDFourthRound1 = 44;
//const int RFIDFifthRound1 = 37;
//RFID Round Two Variables
const int RFIDFirstRound2 = 4;
const int RFIDSecondRound2 = 24;
const int RFIDThirdRound2 = 31;
const int RFIDFourthRound2 = 45;
const int RFIDFifthRound2 = 38;
//Game Controls
const int StartRound2 = 52;
//Player Variables
const int Upload = 53;
const int Success = 9;
const int Failure = 10;
//Output Variables
const int TwoDiscsIn = 11;
const int ThreeDiscsIn = 12;
const int Round1Success = 13;
const int Round2Success = 14;
//Button state for disk in
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
//Button state for first round button
int firstState1 = 0;
int firstState2 = 0;
int firstState3 = 0;
int firstState4 = 0;
//Previous state for first round button
int prevFirst1 = 0;
int prevFirst2 = 0;
int prevFirst3 = 0;
int prevFirst4 = 0;
//Button state for second round button
int secondState1 = 0;
int secondState2 = 0;
int secondState3 = 0;
int secondState4 = 0;
int secondState5 = 0;
//Previous state for second round button
int prevSecond1 = 0;
int prevSecond2 = 0;
int prevSecond3 = 0;
int prevSecond4 = 0;
int prevSecond5 = 0;
//Counting for Powerup
int Count1 = 0;
int Count2 = 0;
int Count3 = 0;
int Count4 = 0;
int Count5 = 0;
//Upload Button State
int uploadState = 0;
//Previous upload button state
int prevUpload = 0;
//Upload Button State round 2
int upload2State = 0;
//Previous upload button state round 2
int prev2Upload = 0;
//Round 2 button state
int round2State = 0;
//Previous round 2 button state
int prevRound2 = 0;
//Stay in Round 2
int Round2Stay = 0;
//Changing Variables
int Powerup = 0;
int rightDisc = 0;
int Round2Go = 0;
int turnOff = 0;
//Debounce
int debounceState1 = 0;
int debounceState2 = 0;
int debounceState3 = 0;
int debounceState4 = 0;
int debounceState5 = 0;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime4 = 0;
unsigned long lastDebounceTime5 = 0;
unsigned long debounceDelay = 50;
int ledState1 = HIGH;
int lastButtonState1 = LOW;
int ledState2 = HIGH;
int lastButtonState2 = LOW;
int ledState3 = HIGH;
int lastButtonState3 = LOW;
int ledState4 = HIGH;
int lastButtonState4 = LOW;
int ledState5 = HIGH;
int lastButtonState5 = LOW;
void setup() {
// LED Outputs
pinMode(FirstLEDRed, OUTPUT);
pinMode(FirstLEDGreen, OUTPUT);
pinMode(FirstLEDBlue, OUTPUT);
pinMode(SecondLEDRed, OUTPUT);
pinMode(SecondLEDGreen, OUTPUT);
pinMode(SecondLEDBlue, OUTPUT);
pinMode(ThirdLEDRed, OUTPUT);
pinMode(ThirdLEDGreen, OUTPUT);
pinMode(ThirdLEDBlue, OUTPUT);
pinMode(FourthLEDRed, OUTPUT);
pinMode(FourthLEDGreen, OUTPUT);
pinMode(FourthLEDBlue, OUTPUT);
pinMode(FifthLEDRed, OUTPUT);
pinMode(FifthLEDGreen, OUTPUT);
pinMode(FifthLEDBlue, OUTPUT);
//LED Diagram
pinMode(FirstDiagram, OUTPUT);
pinMode(SecondDiagram, OUTPUT);
pinMode(ThirdDiagram, OUTPUT);
pinMode(FourthDiagram, OUTPUT);
pinMode(FifthDiagram, OUTPUT);
// Disk Button Inputs
pinMode(FirstDiskIn, INPUT_PULLUP);
pinMode(SecondDiskIn, INPUT_PULLUP);
pinMode(ThirdDiskIn, INPUT_PULLUP);
pinMode(FourthDiskIn, INPUT_PULLUP);
pinMode(FifthDiskIn, INPUT_PULLUP);
//Round One Button Inputs
pinMode(RFIDFirstRound1, INPUT_PULLUP);
pinMode(RFIDSecondRound1, INPUT_PULLUP);
pinMode(RFIDThirdRound1, INPUT_PULLUP);
pinMode(RFIDFourthRound1, INPUT_PULLUP);
//Round Two Button Inputs
pinMode(RFIDFirstRound2, INPUT_PULLUP);
pinMode(RFIDSecondRound2, INPUT_PULLUP);
pinMode(RFIDThirdRound2, INPUT_PULLUP);
pinMode(RFIDFourthRound2, INPUT_PULLUP);
pinMode(RFIDFifthRound2, INPUT_PULLUP);
//Game Controls
pinMode(StartRound2, INPUT_PULLUP);
//Player Variables
pinMode(Upload, INPUT_PULLUP);
pinMode(Success, OUTPUT);
pinMode(Failure, OUTPUT);
//Output Variables
pinMode(TwoDiscsIn, OUTPUT);
pinMode(ThreeDiscsIn, OUTPUT);
pinMode(Round1Success, OUTPUT);
pinMode(Round2Success, OUTPUT);
}
void loop() {
if (Round2Go == 0) {
Round1(); //Starts Round 1 and stays in it until Round 2 is ready to go
}
//If the upload button is pressed in round 1 and 5 red LEDs are on, check Discs
uploadState = digitalRead(Upload); //Reads state of button
if (uploadState == LOW && Round2Stay == 0 && Powerup == 4) {
checkDiscs(); //Checks to see if the cassettes are in right place
}
//When Round 2 Button is pressed start round 2 if round 1 is over
round2State = digitalRead(StartRound2);
if (round2State == LOW && Round2Go == 1) {
Round2Stay = 1; //Variable to let us know we are currently in round 2
}
//If we are in round 2, it is round 2 and we will reset success LED
if (Round2Stay == 1)
{
Round2(); //initiates round 2
//turn unneccessary signalLED's off
digitalWrite(Success, LOW);
}
//If the upload button is pressed in round 2 and 5 red LEDs are on, check Discs
if (uploadState == LOW && Round2Stay == 1 && Powerup == 5) {
checkDiscs2();
}
}
void Round1()
{
//Makes sure that the fifth round RGB LED doesn't turn on
analogWrite(FifthLEDRed, 255);
analogWrite(FifthLEDGreen, 255);
analogWrite(FifthLEDBlue, 255);
digitalWrite(FifthDiagram, LOW);
//Turns first RGB LED Red
analogWrite(FirstLEDRed, 0);
analogWrite(FirstLEDGreen, 255);
analogWrite(FirstLEDBlue, 255);
//When the white disk in button is pressed, the diagram for round 1 turns on, signalling that something has been put it
//If pressed again the red button turns off
buttonState1 = digitalRead(FirstDiskIn);
if (buttonState1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
// if the button state has changed:
if (buttonState1 != debounceState1) {
debounceState1 = buttonState1;
// only toggle the LED if the new button state is HIGH
if (debounceState1 == HIGH) {
ledState1 = !ledState1;
}
}
}
// set the LED:
digitalWrite(FirstDiagram, ledState1);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = buttonState1;
firstState1 = digitalRead(RFIDFirstRound1);
if (firstState1 == LOW && prevFirst1 == 0) {
prevFirst1++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Tells the second LED to be blue
analogWrite(SecondLEDRed, 255);
analogWrite(SecondLEDGreen, 255);
analogWrite(SecondLEDBlue, 0);
buttonState2 = digitalRead(SecondDiskIn);
if (buttonState2 != lastButtonState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState2 != debounceState2) {
debounceState2 = buttonState2;
// only toggle the LED if the new button state is HIGH
if (debounceState2 == HIGH) {
ledState2 = !ledState2;
}
}
}
// set the LED:
digitalWrite(SecondDiagram, ledState2);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState2 = buttonState2;
firstState2 = digitalRead(RFIDSecondRound1);
if (firstState2 == LOW && prevFirst2 == 0) {
prevFirst2++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Tells the third LED to be yellow
analogWrite(ThirdLEDRed, 0);
analogWrite(ThirdLEDGreen, 0);
analogWrite(ThirdLEDBlue, 255);
buttonState3 = digitalRead(ThirdDiskIn);
if (buttonState3 != lastButtonState3) {
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState3 != debounceState3) {
debounceState3 = buttonState3;
// only toggle the LED if the new button state is HIGH
if (debounceState3 == HIGH) {
ledState3 = !ledState3;
}
}
}
// set the LED:
digitalWrite(ThirdDiagram, ledState3);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState3 = buttonState3;
firstState3 = digitalRead(RFIDThirdRound1);
if (firstState3 == LOW && prevFirst3 == 0) {
prevFirst3++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Tells the fourth LED to be green
analogWrite(FourthLEDRed, 255);
analogWrite(FourthLEDGreen, 0);
analogWrite(FourthLEDBlue, 255);
buttonState4 = digitalRead(FourthDiskIn);
if (buttonState4 != lastButtonState4) {
lastDebounceTime4 = millis();
}
if ((millis() - lastDebounceTime4) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState4 != debounceState4) {
debounceState4 = buttonState4;
// only toggle the LED if the new button state is HIGH
if (debounceState4 == HIGH) {
ledState4 = !ledState4;
}
}
}
// set the LED:
digitalWrite(FourthDiagram, ledState4);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState4 = buttonState4;
firstState4 = digitalRead(RFIDFourthRound1);
if (firstState4 == LOW && prevFirst4 == 0) {
prevFirst4++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//If a light is on, it adds a one to Powerup
if (digitalRead(FirstDiagram) == HIGH) {
Count1 = 1;
}
else {
Count1 = 0;
}
if (digitalRead(SecondDiagram) == HIGH) {
Count2 = 1;
}
else {
Count2 = 0;
}
if (digitalRead(ThirdDiagram) == HIGH) {
Count3 = 1;
}
else {
Count3 = 0;
}
if (digitalRead(FourthDiagram) == HIGH) {
Count4 = 1;
}
else {
Count4 = 0;
}
//Equation to have Powerup keep track of how many Diagram LEDs are on
Powerup = Count1 + Count2 + Count3 + Count4;
//If two red LEDs are on, turn on this green LED
if (Powerup > 1)
{
digitalWrite(TwoDiscsIn, HIGH);
}
else
{
digitalWrite(TwoDiscsIn, LOW);
}
//If three red LEDs are on, turn on this green LED
if (Powerup > 2)
{
digitalWrite(ThreeDiscsIn, HIGH);
}
else
{
digitalWrite(ThreeDiscsIn, LOW);
}
}
void Round2()
{
//Same as round 1 but green
analogWrite(FirstLEDRed, 255);
analogWrite(FirstLEDGreen, 0);
analogWrite(FirstLEDBlue, 255);
buttonState1 = digitalRead(FirstDiskIn);
if (buttonState1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState1 != debounceState1) {
debounceState1 = buttonState1;
// only toggle the LED if the new button state is HIGH
if (debounceState1 == HIGH) {
ledState1 = !ledState1;
}
}
}
// set the LED:
digitalWrite(FirstDiagram, ledState1);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = buttonState1;
secondState1 = digitalRead(RFIDFirstRound2);
if (secondState1 == LOW && prevSecond1 == 0) {
prevSecond1++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Same as round 1 but white
analogWrite(SecondLEDRed, 0);
analogWrite(SecondLEDGreen, 0);
analogWrite(SecondLEDBlue, 0);
buttonState2 = digitalRead(SecondDiskIn);
if (buttonState2 != lastButtonState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState2 != debounceState2) {
debounceState2 = buttonState2;
// only toggle the LED if the new button state is HIGH
if (debounceState2 == HIGH) {
ledState2 = !ledState2;
}
}
}
// set the LED:
digitalWrite(SecondDiagram, ledState2);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState2 = buttonState2;
secondState2 = digitalRead(RFIDSecondRound2);
if (secondState2 == LOW && prevSecond2 == 0) {
prevSecond2++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Same as round 1 but blue
analogWrite(ThirdLEDRed, 255);
analogWrite(ThirdLEDGreen, 255);
analogWrite(ThirdLEDBlue, 0);
buttonState3 = digitalRead(ThirdDiskIn);
if (buttonState3 != lastButtonState3) {
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState3 != debounceState3) {
debounceState3 = buttonState3;
// only toggle the LED if the new button state is HIGH
if (debounceState3 == HIGH) {
ledState3 = !ledState3;
}
}
}
// set the LED:
digitalWrite(ThirdDiagram, ledState3);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState3 = buttonState3;
secondState3 = digitalRead(RFIDThirdRound2);
if (secondState3 == LOW && prevSecond3 == 0) {
prevSecond3++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//Same as round 1 but red
analogWrite(FourthLEDRed, 0);
analogWrite(FourthLEDGreen, 255);
analogWrite(FourthLEDBlue, 255);
buttonState4 = digitalRead(FourthDiskIn);
if (buttonState4 != lastButtonState4) {
lastDebounceTime4 = millis();
}
if ((millis() - lastDebounceTime4) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState4 != debounceState4) {
debounceState4 = buttonState4;
// only toggle the LED if the new button state is HIGH
if (debounceState4 == HIGH) {
ledState4 = !ledState4;
}
}
}
// set the LED:
digitalWrite(FourthDiagram, ledState4);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState4 = buttonState4;
secondState4 = digitalRead(RFIDFourthRound2);
if (secondState4 == LOW && prevSecond4 == 0) {
prevSecond4++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//A new cassette is introduced to be yellow
analogWrite(FifthLEDRed, 0);
analogWrite(FifthLEDGreen, 0);
analogWrite(FifthLEDBlue, 255);
buttonState5 = digitalRead(FifthDiskIn);
if (buttonState5 != lastButtonState5) {
lastDebounceTime5 = millis();
}
if ((millis() - lastDebounceTime5) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (buttonState5 != debounceState5) {
debounceState5 = buttonState5;
// only toggle the LED if the new button state is HIGH
if (debounceState5 == HIGH) {
ledState5 = !ledState5;
}
}
}
// set the LED:
digitalWrite(FifthDiagram, ledState5);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState5 = buttonState5;
secondState5 = digitalRead(RFIDFifthRound2);
if (secondState5 == LOW && prevSecond5 == 0) {
prevSecond5++;
rightDisc++; //Keeps track of how many cassettes are in the right place
}
//If Diagram LED is on, Powerup goes up by one
if (digitalRead(FirstDiagram) == HIGH) {
Count1 = 1;
}
else {
Count1 = 0;
}
if (digitalRead(SecondDiagram) == HIGH) {
Count2 = 1;
}
else {
Count2 = 0;
}
if (digitalRead(ThirdDiagram) == HIGH) {
Count3 = 1;
}
else {
Count3 = 0;
}
if (digitalRead(FourthDiagram) == HIGH) {
Count4 = 1;
}
else {
Count4 = 0;
}
if (digitalRead(FifthDiagram) == HIGH) {
Count5 = 1;
}
else {
Count5 = 0;
}
//Equation to keep track of Powerup
Powerup = Count1 + Count2 + Count3 + Count4 + Count5;
//If two red LEDs are on, turn on this green LED
if (Powerup > 1)
{
digitalWrite(TwoDiscsIn, HIGH);
}
else
{
digitalWrite(TwoDiscsIn, LOW);
}
//If three red LEDs are on, turn on this green LED
if (Powerup > 2)
{
digitalWrite(ThreeDiscsIn, HIGH);
}
else
{
digitalWrite(ThreeDiscsIn, LOW);
}
}
//Turns off all of the LED's
void TurnOff()
{
analogWrite(FirstLEDRed, 255);
analogWrite(FirstLEDGreen, 255);
analogWrite(FirstLEDBlue, 255);
analogWrite(SecondLEDRed, 255);
analogWrite(SecondLEDGreen, 255);
analogWrite(SecondLEDBlue, 255);
analogWrite(ThirdLEDRed, 255);
analogWrite(ThirdLEDGreen, 255);
analogWrite(ThirdLEDBlue, 255);
analogWrite(FourthLEDRed, 255);
analogWrite(FourthLEDGreen, 255);
analogWrite(FourthLEDBlue, 255);
analogWrite(FifthLEDRed, 255);
analogWrite(FifthLEDGreen, 255);
analogWrite(FifthLEDBlue, 255);
}
void checkDiscs()
{
if (rightDisc == 4)
{
//Turns on/off LEDs
digitalWrite(Success, HIGH);
digitalWrite(Failure, LOW);
digitalWrite(Round1Success, HIGH);
digitalWrite(TwoDiscsIn, LOW);
digitalWrite(ThreeDiscsIn, LOW);
//Reset variables for next round
Powerup = 0;
rightDisc = 0;
Round2Go = 1; //Preps code for Round 2
//Resets variables for next round
debounceState1 = 0;
debounceState2 = 0;
debounceState3 = 0;
debounceState4 = 0;
debounceState5 = 0;
lastDebounceTime1 = 0;
lastDebounceTime2 = 0;
lastDebounceTime3 = 0;
lastDebounceTime4 = 0;
lastDebounceTime5 = 0;
//Keeps lights on for 10 seconds
delay(10000);
//Turns off lights
digitalWrite(FirstDiagram, LOW);
digitalWrite(SecondDiagram, LOW);
digitalWrite(ThirdDiagram, LOW);
digitalWrite(FourthDiagram, LOW);
digitalWrite(Success, LOW);
TurnOff();
}
else {
digitalWrite(Failure, HIGH); //Nothing else changes so that it can continue being uploaded
delay(1000); //Has failure light on for 1 second
digitalWrite(Failure, LOW);
}
}
void checkDiscs2()
{
if (rightDisc == 5)
{
//Turn on/off LEDs
digitalWrite(Success, HIGH);
digitalWrite(Round2Success, HIGH);
digitalWrite(Failure, LOW);
digitalWrite(TwoDiscsIn, LOW);
digitalWrite(ThreeDiscsIn, LOW);
delay(10000);
TurnOff();
digitalWrite(FirstDiagram, LOW);
digitalWrite(SecondDiagram, LOW);
digitalWrite(ThirdDiagram, LOW);
digitalWrite(FourthDiagram, LOW);
digitalWrite(FifthDiagram, LOW);
digitalWrite(Success, LOW);
Round2Stay = 2; //Round 2 is now over
}
else {
digitalWrite(Failure, HIGH); //Nothing else changes so that it can continue being uploaded
delay(1000); //Has failure light on for one second
digitalWrite(Failure, LOW);
}
}