Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. String inputString = "";         // a string to hold incoming data
  4. boolean startGame = false;
  5. boolean stopGame = false;
  6. boolean readSensors = false;
  7.  
  8. boolean sensor_1_tripped = false;
  9. boolean sensor_2_tripped = false;
  10. boolean sensor_3_tripped = false;
  11. boolean sensor_4_tripped = false;
  12. boolean sensor_5_tripped = false;
  13. boolean sensor_6_tripped = false;
  14.  
  15. #define SENSOR_1 2
  16. #define SENSOR_2 3
  17. #define SENSOR_3 21
  18. #define SENSOR_4 20
  19. #define SENSOR_5 19
  20. #define SENSOR_6 18
  21.  
  22. void setup() {
  23.   // put your setup code here, to run once:
  24.   Serial.begin(9600);
  25.  
  26.   pinMode(SENSOR_1, INPUT_PULLUP);
  27.   pinMode(SENSOR_2, INPUT_PULLUP);
  28.   pinMode(SENSOR_3, INPUT_PULLUP);
  29.   pinMode(SENSOR_4, INPUT_PULLUP);
  30.   pinMode(SENSOR_5, INPUT_PULLUP);
  31.   pinMode(SENSOR_6, INPUT_PULLUP);
  32.  
  33.   // reserve 200 bytes for the inputString:
  34.   inputString.reserve(200);
  35. }
  36.  
  37. void loop() {
  38.   while (Serial.available()) {
  39.     // get the new byte:
  40.     char inChar = (char)Serial.read();
  41.     // add it to the inputString:
  42.     inputString += inChar;
  43.     // if the incoming character is a newline, set a flag
  44.     // so the main loop can do something about it:
  45.     if (inputString == "start") {
  46.       startGame = true;
  47.       stopGame = false;
  48.       readSensors = true;
  49.       Serial.write("STARTED");
  50.       inputString = "";
  51.       return;
  52.     }
  53.     if (inputString == "stop") {
  54.       stopGame = true;
  55.       startGame = false;
  56.       readSensors = false;
  57.       return;
  58.     }
  59.   }
  60.  
  61.   while (startGame) {
  62.     while(readSensors) {
  63.  
  64.       attachInterrupt(digitalPinToInterrupt(SENSOR_1), sensor_1_callback, FALLING);
  65.       attachInterrupt(digitalPinToInterrupt(SENSOR_2), sensor_2_callback, FALLING);
  66.       attachInterrupt(digitalPinToInterrupt(SENSOR_3), sensor_3_callback, FALLING);
  67.       attachInterrupt(digitalPinToInterrupt(SENSOR_4), sensor_4_callback, FALLING);
  68.       attachInterrupt(digitalPinToInterrupt(SENSOR_5), sensor_5_callback, FALLING);
  69.       attachInterrupt(digitalPinToInterrupt(SENSOR_6), sensor_6_callback, FALLING);
  70.  
  71.       if(Serial.available()) {
  72.         char inChar = (char)Serial.read();
  73.         inputString += inChar;
  74.         if (inputString == "stop") {
  75.           stopGame = true;
  76.           startGame = false;
  77.           readSensors = false;
  78.           inputString = "";
  79.           Serial.write("STOPPED");
  80.           break;
  81.         }
  82.       }
  83.  
  84.       if(sensor_1_tripped == true) {
  85.         sensor_1_tripped = false;
  86.         Serial.write("1");
  87.       }
  88.       if(sensor_2_tripped == true) {
  89.         sensor_2_tripped = false;
  90.         Serial.write("2");
  91.       }
  92.       if(sensor_3_tripped == true) {
  93.         sensor_3_tripped = false;
  94.         Serial.write("3");
  95.       }
  96.       if(sensor_4_tripped == true) {
  97.         sensor_4_tripped = false;
  98.         Serial.write("4");
  99.       }
  100.       if(sensor_5_tripped == true) {
  101.         sensor_5_tripped = false;
  102.         Serial.write("5");
  103.       }
  104.       if(sensor_6_tripped == true) {
  105.         sensor_6_tripped = false;
  106.         Serial.write("6");
  107.       }
  108.     }
  109.   }
  110. }
  111.  
  112. void sensor_1_callback() {
  113.   sensor_1_tripped = true;
  114. }
  115. void sensor_2_callback() {
  116.   sensor_2_tripped = true;
  117. }
  118. void sensor_3_callback() {
  119.   sensor_3_tripped = true;
  120. }
  121. void sensor_4_callback() {
  122.   sensor_4_tripped = true;
  123. }
  124. void sensor_5_callback() {
  125.   sensor_5_tripped = true;
  126. }
  127. void sensor_6_callback() {
  128.   sensor_6_tripped = true;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement