Skip to content

Instantly share code, notes, and snippets.

@nicoandmee
Created April 21, 2022 23:47
Show Gist options
  • Save nicoandmee/1d7e157a8a870c3c609d3e17f4e56fce to your computer and use it in GitHub Desktop.
Save nicoandmee/1d7e157a8a870c3c609d3e17f4e56fce to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#include <SPI.h>
#include <String.h>
#include <Wire.h>
#pragma region PIN_DEFINITIONS
#define GAME_START 3
#define GAME_END 4
#define SLAVE_ADDR 9
#define GAME_IRQ 8
#pragma endregion
#pragma region GLOBALS
int GAME_STATE = 0; // 0 allows the loop to run, waiting for a card to be read
// in. Increments 100, 200, 400 1 creates a loop, adding
// in scores and waiting for an endgame signal from Mega
bool PLAYERS_INITD = false;
byte firstN[18];
byte lastN[18];
byte blankN[18];
byte firstN_2[18];
byte lastN_2[18];
#pragma endregion
void receiveEvent() {
int index = 0;
// Read while data received
while (0 < Wire.available()) {
byte x = Wire.read();
// First 18 bytes are first name [Player 1]
if (index < 18) {
firstN[index] = x;
}
// Next 18 bytes are last name [Player 1]
else if (index < 36 && index > 18) {
lastN[index - 18] = x;
}
// Next 18 bytes are first name [Player 2]
else if (index < 54 && index > 36) {
firstN_2[index - 36] = x;
}
// Next 18 bytes are last name [Player 2]
else if (index < 72 && index > 54) {
lastN_2[index - 54] = x;
}
index++;
}
// Print to Serial Monitor
Serial.println("Receive event");
// Set flag indicating we have names
PLAYERS_INITD = true;
}
void setup() {
// Initialize I2C as slave
Wire.begin(SLAVE_ADDR);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
Serial.begin(9600);
pinMode(GAME_START, OUTPUT);
pinMode(GAME_END, INPUT);
digitalWrite(GAME_START, LOW);
attachInterrupt(digitalPinToInterrupt(GAME_IRQ), gameStartHandler, RISING);
}
void loop() {
// We should enter this when RFID mega sends a signal to start the game
if (GAME_STATE == 1) {
Serial.println("GAME STARTED");
// Wait until we have the player infos
while (!PLAYERS_INITD) {
Serial.println("Waiting for player initialization...");
delay(50);
}
// Now we haz players?
String firstName = String((char*)firstN);
Serial.println("FIRST NAME [PLAYER 1]: ");
Serial.println(firstName);
String lastName = String((char*)lastN);
Serial.println("LAST NAME [PLAYER 1]: ");
Serial.println(lastName);
String firstName2 = String((char*)firstN_2);
Serial.println("FIRST NAME [PLAYER 2]: ");
Serial.println(firstName2);
String lastName2 = String((char*)lastN_2);
Serial.println("LAST NAME [PLAYER 2]: ");
Serial.println(lastName2);
delay(10000);
// Reset the names
for (int i = 0; i < 18; i++) {
firstN[i] = blankN[i];
lastN[i] = blankN[i];
firstN_2[i] = blankN[i];
lastN_2[i] = blankN[i];
}
}
}
/**
* This function triggers when master arduino says game should begin
*/
void gameStartHandler() {
GAME_STATE = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment