//Petra Rutanen final project code, Fab academy 2023 #include #include //for I2C bus as peripheral #include //for OLED #include #include #include //install from a zip library included #define rxPin 27 // recieves signal from bridge #define txPin 28 // transmit signal to the bridge #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels const int BUTTON_PIN = 2; // the number of the pushbutton pin GPIO Arduino int score = 0; // variable to store score long gameDuration = 30000; //game duration 30s int nodeTimeout = 5000; //node timeout 5s int numberOfNodes = 2; // number of nodes unsigned long gameStartTime; //variable to store game start time SoftwareSerial mySerial(rxPin, txPin); EncButton2 btn(INPUT_PULLUP, BUTTON_PIN); //Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); char nodes[] = { '1', '2', '3', '4', '5', '6' }; //nodes[0] = 1 void setup() { Serial.begin(115200); while (!Serial) { delay(10); } mySerial.begin(9600); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 //Serial.println(F("SSD1306 allocation failed")); for (;;) ; } delay(2000); startMessage(); } void loop() { btn.tick(); // check button for clicks if (btn.click()) { // if button is clicked score = 0; // zero score gameMessage(); // display message during the game gameStartTime = millis(); // save start time Serial.println("GameStarted"); while (millis() - gameStartTime < gameDuration) { // while the time is not out int callNode = random(numberOfNodes); //random 0..numberOfNodes mySerial.println(nodes[callNode]); //send random node number to serial long callTime = millis(); //save time when te node was triggered Serial.print("Node "); Serial.print(nodes[callNode]); Serial.println(" is triggered"); while (millis() - callTime < nodeTimeout) { // check serial before nodeTimeout gameMessage2(callNode); } } gameOver(); } } void startMessage() { display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0, 10); display.println("Press Button to START!"); display.display(); } void gameOver() { display.clearDisplay(); display.setCursor(0, 10); display.println("Game Ended"); display.print("Score: "); display.println(score); display.display(); } void gameMessage() { display.clearDisplay(); display.setCursor(0, 10); display.println("Targets Hit: "); display.println(score); display.display(); } void gameMessage2(int node) { display.clearDisplay(); display.setCursor(0, 10); display.println("Node "); display.println(node + 1); display.display(); }