The puzzle is solved by correct RFID being solved. (which powers game up) Then 2 beam breakers need to be broken 4 each (1 for each lock). In the control room where we have access to a cheater button when someone cheats our room. We have this connected to Arduino then audio for the puzzle plays off adafruit fx board. The problem with my code is that The cheater function is getting hit when we don't hit the button. It happens right after we solve 1 beam breaker, but only 33% of the time.
#include <Wire.h>
//#include <TimerOne.h>
#include <FastLED.h>
//#include <SD.h> // Library for reading from SD card
#include "Trigger.h" // Contains data and controls for each Basketball Hoop
#define NUM_LEDS 150
#define PIN 10 // Library for controlling LED's
CRGB leds[NUM_LEDS];
Trigger firstTrigger, secondTrigger;
bool firstTriggerState = true;
bool secondTriggerState = true;
bool finishedGame = false;
const byte T_PIN1 = 4; // Basketball trigger pin
const byte T_PIN2 = 6; // Basketball trigger pin
const byte CHEATER_PIN = 3; // Pin to interupt and reset game
const byte MAG_PIN = 8;
const byte MAG_PIN2 = 7; // Pin to control Maglock
const byte MAX_PRESSES = 4; // Maximum number of times ...not Basketballs... should be triggered
const byte Reset = 11; // Pin for Reseting Arduino
const byte Finish_Sound = A0;
const byte Cheater_Sound = A1;
const byte Loop_Sound = A2;
const byte PowerUp_Sound = A3;
int x = 0;
int Slave1;
int Slave = 2;
const int TRIGGER_INTERVAL = 500;
unsigned long previousMillis = 0;
unsigned long previousMilli = 0;
bool gameOn, // Check if game has been activated by RFID
gameState,
lastGameState;
// Function to reset game to all default values
void resetGame(){
finishedGame = false;
gameOn = false;
gameState = true;
lastGameState = !gameState;
firstTrigger.init(T_PIN1, MAX_PRESSES);
secondTrigger.init(T_PIN2, MAX_PRESSES);
firstTriggerState = true;
secondTriggerState = true;
pinMode(MAG_PIN, OUTPUT);
digitalWrite(MAG_PIN, LOW);
pinMode(MAG_PIN2, OUTPUT);
digitalWrite(MAG_PIN2, LOW);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(CHEATER_PIN, INPUT);
digitalWrite(CHEATER_PIN,HIGH);
pinMode(Reset, OUTPUT);
digitalWrite(Reset, HIGH);
pinMode(Finish_Sound, OUTPUT);
digitalWrite(Finish_Sound, HIGH);
pinMode(Loop_Sound, OUTPUT);
digitalWrite(Loop_Sound, HIGH);
pinMode(Cheater_Sound, OUTPUT);
digitalWrite(Cheater_Sound, HIGH);
pinMode(PowerUp_Sound, OUTPUT);
digitalWrite(PowerUp_Sound, HIGH);
//Timer1.initialize(1000000); // 1 million microseconds is 2 seconds
//Timer1.attachInterrupt(DisplayRFID);
setAll(0,0,0);
}
void setup() {
Serial.begin(9600); // Set up serial communication at 9600bps
resetGame();
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
// Serial.println("BRETT");
}
void FinishedGame(){
digitalWrite(Loop_Sound, HIGH);
delay(250);
digitalWrite(Finish_Sound, LOW);
Serial.println("Finish Sound");
delay(3000);
digitalWrite(Finish_Sound, HIGH);
while(true){
setAll(0xff,0,0); //set all LEDs to SOLID COLOR
gameOn = false;
}
}
void cheater(){
if(digitalRead(CHEATER_PIN) != HIGH){
Serial.println("CHEATER!!!");
digitalWrite(Loop_Sound, HIGH);
delay(250);
digitalWrite(Cheater_Sound, LOW);
delay(3000);
digitalWrite(Cheater_Sound, HIGH);
delay(10000);
resetGame();
}
}
void loop(){
//Serial.print(F("LoopStart time:"));
//Serial.print(millis());
if(finishedGame){
gameOn = false;
FinishedGame();
} else {
DisplayRFID();
cheater();
if(gameOn){
if(gameState != lastGameState){
Serial.println("Game Ready...\nMusic and Lights start");
digitalWrite(PowerUp_Sound, LOW);
Serial.println("PowerUp");
delay(250);
digitalWrite(PowerUp_Sound, HIGH);
delay(1000);
digitalWrite(Loop_Sound, LOW);
Serial.println("Loop");
lastGameState = gameState;
}
buttonP();
LED();
} else{
resetGame();
}
}
//Serial.print(F("Loop Endtime:"));
//Serial.print(millis());
}
void buttonP(){
//Serial.println("GameOff");
if (gameOn){
//Serial.println("GameOn");
firstTrigger.isTriggered();
secondTrigger.isTriggered();
//Serial.println("This also better loop.");
if(firstTrigger.completed && firstTriggerState) {
firstTriggerState = false;
Serial.println("Completed game 1");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(MAG_PIN, HIGH); // open maglock
delay(100);
digitalWrite(MAG_PIN, LOW); // close maglock
//delay(1000);
}
if (secondTrigger.completed && secondTriggerState) {
secondTriggerState = false;
Serial.println("Completed Basket 2");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(MAG_PIN2, HIGH); // open maglock
delay(100);
digitalWrite(MAG_PIN2, LOW); // c maglock
//delay(1000);
}
if (firstTrigger.completed && secondTrigger.completed) {
Serial.println("Full Game completed");
finishedGame = true;
FinishedGame();
resetGame();
}
} else {
resetGame();
}
}
void DisplayRFID(){
Slave1 = digitalRead(Slave);
if (Slave1 == HIGH) {
gameOn= true;
} else if (digitalRead(Slave1) == LOW && (unsigned long)(millis() - previousMillis) >= TRIGGER_INTERVAL){
gameOn= false;
}
}
void LED(){
theaterChaseRainbow(50);
}
}
}
TRIGGER TAB
#pragma once
class Trigger {
public:
bool completed;
int sensorState;
int lastState;
protected:
const int TRIGGER_INTERVAL = 1000;
byte switchPin; // switch is connected to pin
byte buttonPresses; // how many times the button has been pressed
byte maxPresses;
byte lastPressCount; // to keep track of last press count
unsigned long previousMillis = 0;
public:
Trigger();
virtual ~Trigger();
void init(byte, byte);
void isTriggered();
};
Trigger::Trigger(){
this->sensorState = 0;
this->lastState = 0;
this->buttonPresses = 0;
this->lastPressCount = 0;
this->completed = false;
}
Trigger::~Trigger(){}
void Trigger::init(byte s, byte m){
this->sensorState = 0;
this->lastState = 0;
this->maxPresses = m;
this->buttonPresses = 0;
this->switchPin = s;
this->completed = false;
pinMode(switchPin, INPUT); // Set the switch pin as input
digitalWrite(switchPin, HIGH); // set pullup resistor
}
void Trigger::isTriggered(){
// check if button was pressed
//if (digitalRead(switchPin) == LOW && (unsigned long)(millis() - previousMillis) >= TRIGGER_INTERVAL){
// buttonPresses++; // increment buttonPresses count
// previousMillis = millis();
//}
// read the state of the pushbutton value:
sensorState = digitalRead(switchPin);
// check if the sensor beam is broken
if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
buttonPresses++;
}
lastState = sensorState;
// only do output if the count has changed
if (lastPressCount != buttonPresses){
Serial.print("[Pin: ");
Serial.print(switchPin, DEC);
Serial.print ("] Button press count = ");
Serial.println(buttonPresses, DEC);
if(buttonPresses >= maxPresses){
this->completed = true;
//Serial.println("Completed");
} else {
this->completed = false;
}
lastPressCount = buttonPresses; // track last press count
}
// Serial.println("This better be looping...");
}