-
-
Save anonymous/fb7a63367e7a722dc272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Pin for white gate laser | |
#define WHITE_LED_PIN 13 | |
//Pin for blue gate laser | |
#define BLUE_LED_PIN 8 | |
//Pin for white gate photoresistor | |
#define WHITE_LDR_PIN A0 | |
//Pin for blue gate photoresistor | |
#define BLUE_LDR_PIN A1 | |
//Commands to arduino: | |
// 1 - start game | |
const int START_COMMAND = 1; | |
// 2 - stop game | |
const int STOP_COMMAND = 2; | |
//Commands from arduino: | |
// 'GOAL:WHITE' - goal to white gate | |
// 'GOAL:BLUE' - goal to blue gate | |
const char GOAL_PREFIX[] = "GOAL:"; | |
const char WHITE_TEAM_NAME[] = "WHITE"; | |
const char BLUE_TEAM_NAME[] = "BLUE"; | |
// 'LISTENING' - waiting for commands | |
const char LISTENING_MESSAGE[] = "LISTENING"; | |
// 'START' - game is started | |
const char START_GAME_MESSAGE[] = "START"; | |
// 'CALIBRATION' - sensors are in calibration | |
const char CALIBRATION_MESSAGE[] = "CALIBRATION"; | |
// 'STOP' - game is stopped | |
const char STOP_GAME_MESSAGE[] = "STOP"; | |
//Timeout after which game will be automatically stopped | |
//15 minutes - 900000 ms | |
const long INACTIVITY_TIMEOUT = 900000; | |
//Minumum deviation that is necessary to count a goal | |
//E.g if deviation is 1.1 it means that when value on photoresistor exceeds calibrated maximum at least on 10%, goal will be counted | |
const int MINIMUM_SENSOR_DEVIATION = 1.1; | |
//Time in milliseconds for photoresistors calibration | |
const long CALIBRATION_TIME = 1000; | |
//Delay in milliseconds after goals to avoid multiple counting of the same goal | |
const long DELAY_AFTER_GOALS = 1000; | |
//Delay in milliseconds to avoid interference on LDR right after lasers are 'ON' | |
const long DELAY_BEFORE_CALIBRATION = 200; | |
//Maximum values on photoresistors after calibration. | |
int maxWhiteSensorValue = 0; | |
int maxBlueSensorValue = 0; | |
//Is game currently running | |
boolean running = false; | |
//Milliseconds from last activity (from game start or from last goal) | |
long lastActivityTime = 0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(WHITE_LED_PIN, OUTPUT); | |
pinMode(BLUE_LED_PIN, OUTPUT); | |
Serial.println(LISTENING_MESSAGE); | |
} | |
void loop() | |
{ | |
if (running) | |
{ | |
if (millis() - lastActivityTime >= INACTIVITY_TIMEOUT) | |
{ | |
//Stop the game because of inactivity | |
stopTheGame(); | |
} | |
else | |
{ | |
checkFootballGate(WHITE_LDR_PIN, maxWhiteSensorValue, WHITE_TEAM_NAME); | |
checkFootballGate(BLUE_LDR_PIN, maxBlueSensorValue, BLUE_TEAM_NAME); | |
} | |
} | |
else | |
{ | |
//If game isn't running, check serial port for incoming commands | |
int serialValue = Serial.parseInt(); | |
if (serialValue == START_COMMAND) | |
{ | |
startTheGame(); | |
} | |
} | |
} | |
//Turn on lasers and calibrate the photoresistors | |
void startTheGame() | |
{ | |
digitalWrite(WHITE_LED_PIN, HIGH); | |
digitalWrite(BLUE_LED_PIN, HIGH); | |
//Delay to avoid interference on LDR right after lasers are 'ON' | |
delay(DELAY_BEFORE_CALIBRATION); | |
Serial.println(CALIBRATION_MESSAGE); | |
calibrateSensors(); | |
running = true; | |
lastActivityTime = millis(); | |
Serial.println(START_GAME_MESSAGE); | |
} | |
void stopTheGame() | |
{ | |
Serial.println(STOP_GAME_MESSAGE); | |
digitalWrite(WHITE_LED_PIN, LOW); | |
digitalWrite(BLUE_LED_PIN, LOW); | |
running = false; | |
} | |
//Measuring of maximum values on photoresistors during calibrationTime period | |
void calibrateSensors() | |
{ | |
maxWhiteSensorValue = 0; | |
maxBlueSensorValue = 0; | |
long startMillis = millis(); | |
while (millis() - startMillis < CALIBRATION_TIME) | |
{ | |
int whiteSensorValue = analogRead(WHITE_LDR_PIN); | |
int blueSensorValue = analogRead(BLUE_LDR_PIN); | |
// record the maximum sensor value | |
if (whiteSensorValue > maxWhiteSensorValue) | |
{ | |
maxWhiteSensorValue = whiteSensorValue; | |
} | |
if (blueSensorValue > maxBlueSensorValue) | |
{ | |
maxBlueSensorValue = blueSensorValue; | |
} | |
} | |
} | |
void checkFootballGate(int ldrPin, int maxSensorValue, const char *teamName) | |
{ | |
int sensorValue = analogRead(ldrPin); | |
//If sensorValue is exceeds maxValue at least on configured minimum deviation (which means that light flow is interrupted) | |
if (sensorValue >= (maxSensorValue * MINIMUM_SENSOR_DEVIATION)) | |
{ | |
Serial.print(GOAL_PREFIX); | |
Serial.println(teamName); | |
lastActivityTime = millis(); | |
checkForStopCommand(); | |
delay(DELAY_AFTER_GOALS); | |
} | |
} | |
//Check serial port for stop game command | |
void checkForStopCommand() | |
{ | |
int serialValue = Serial.parseInt(); | |
if (serialValue == STOP_COMMAND) | |
{ | |
stopTheGame(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment