Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- String inputString = ""; // a string to hold incoming data
- boolean startGame = false;
- boolean stopGame = false;
- boolean readSensors = false;
- boolean sensor_1_tripped = false;
- boolean sensor_2_tripped = false;
- boolean sensor_3_tripped = false;
- boolean sensor_4_tripped = false;
- boolean sensor_5_tripped = false;
- boolean sensor_6_tripped = false;
- #define SENSOR_1 2
- #define SENSOR_2 3
- #define SENSOR_3 21
- #define SENSOR_4 20
- #define SENSOR_5 19
- #define SENSOR_6 18
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(SENSOR_1, INPUT_PULLUP);
- pinMode(SENSOR_2, INPUT_PULLUP);
- pinMode(SENSOR_3, INPUT_PULLUP);
- pinMode(SENSOR_4, INPUT_PULLUP);
- pinMode(SENSOR_5, INPUT_PULLUP);
- pinMode(SENSOR_6, INPUT_PULLUP);
- // reserve 200 bytes for the inputString:
- inputString.reserve(200);
- }
- void loop() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (inputString == "start") {
- startGame = true;
- stopGame = false;
- readSensors = true;
- Serial.write("STARTED");
- inputString = "";
- return;
- }
- if (inputString == "stop") {
- stopGame = true;
- startGame = false;
- readSensors = false;
- return;
- }
- }
- while (startGame) {
- while(readSensors) {
- attachInterrupt(digitalPinToInterrupt(SENSOR_1), sensor_1_callback, FALLING);
- attachInterrupt(digitalPinToInterrupt(SENSOR_2), sensor_2_callback, FALLING);
- attachInterrupt(digitalPinToInterrupt(SENSOR_3), sensor_3_callback, FALLING);
- attachInterrupt(digitalPinToInterrupt(SENSOR_4), sensor_4_callback, FALLING);
- attachInterrupt(digitalPinToInterrupt(SENSOR_5), sensor_5_callback, FALLING);
- attachInterrupt(digitalPinToInterrupt(SENSOR_6), sensor_6_callback, FALLING);
- if(Serial.available()) {
- char inChar = (char)Serial.read();
- inputString += inChar;
- if (inputString == "stop") {
- stopGame = true;
- startGame = false;
- readSensors = false;
- inputString = "";
- Serial.write("STOPPED");
- break;
- }
- }
- if(sensor_1_tripped == true) {
- sensor_1_tripped = false;
- Serial.write("1");
- }
- if(sensor_2_tripped == true) {
- sensor_2_tripped = false;
- Serial.write("2");
- }
- if(sensor_3_tripped == true) {
- sensor_3_tripped = false;
- Serial.write("3");
- }
- if(sensor_4_tripped == true) {
- sensor_4_tripped = false;
- Serial.write("4");
- }
- if(sensor_5_tripped == true) {
- sensor_5_tripped = false;
- Serial.write("5");
- }
- if(sensor_6_tripped == true) {
- sensor_6_tripped = false;
- Serial.write("6");
- }
- }
- }
- }
- void sensor_1_callback() {
- sensor_1_tripped = true;
- }
- void sensor_2_callback() {
- sensor_2_tripped = true;
- }
- void sensor_3_callback() {
- sensor_3_tripped = true;
- }
- void sensor_4_callback() {
- sensor_4_tripped = true;
- }
- void sensor_5_callback() {
- sensor_5_tripped = true;
- }
- void sensor_6_callback() {
- sensor_6_tripped = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement