//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 //Eg 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(); } }
var Arduino = function () { var self = this; // constans block self.LISTENING_MESSAGE = "LISTENING"; self.STOP_GAME_MESSAGE = "STOP"; ... var portIsReady = true; // init SerialPort var serialPort = new SerialPort(self.PORT_NUMBER, { parser: serialport.parsers.readline("\r\n"), baudrate: 9600, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }, true); // open connection and listening port serialPort.on(self.PORT_OPEN_COMMAND, function () { serialPort.on(self.PORT_RECEIVE_DATA_COMMAND, function (arduinoMessage) { if (arduinoMessage === self.LISTENING_MESSAGE) { // arduino is ready self.emit(self.ARDUINO_READY_COMMAND, arduinoMessage); } else if (arduinoMessage === self.STOP_GAME_MESSAGE) { // stop command or timeout stop self.emit(self.ARDUINO_IS_STOPPED, arduinoMessage); } else if (arduinoMessage === self.GOAL_WHITE_MESSAGE || arduinoMessage === self.GOAL_BLUE_MESSAGE) { // goal in white gate (point for blue team) self.emit(self.ARDUINO_GOAL, arduinoMessage); } }); }); serialPort.on(self.PORT_CLOSE_COMMAND, function () { portIsReady = false; }); serialPort.on(self.PORT_ERROR_COMMAND, function () { portIsReady = false; }); self.on(self.ARDUINO_READY_COMMAND, function () { portIsReady = true; }); self.on(self.ARDUINO_START_COMMAND, function () { if (portIsReady) { serialPort.write(self.START_GAME_COMMAND); } }); self.on(self.ARDUINO_STOP_COMMAND, function () { if (portIsReady) { serialPort.write(self.STOP_GAME_COMMAND); } }); self.start = function () { self.emit(self.ARDUINO_START_COMMAND); }; self.stop = function () { self.emit(self.ARDUINO_STOP_COMMAND); }; };
var GameController = function () { ... Arduino.on(Arduino.ARDUINO_GOAL, function (team) { goal(team); }); Arduino.on(Arduino.ARDUINO_IS_STOPPED, function () { _stop(currentGame, true); }); ... }
Source: https://habr.com/ru/post/245031/
All Articles