Last active
May 11, 2020 21:38
-
-
Save vspruyt-sol/a16bf2378a99a286990f5999a7ffeb44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
EthernetClient client; | |
unsigned long byteCount = 0; | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
char server[] = "collabyrinth.be"; | |
// Set the static IP address to use if the DHCP fails to assign | |
IPAddress ip(192, 168, 1, 39); | |
IPAddress myDns(192, 168, 1, 1); | |
//Inputs | |
//Camera pins: 22 - 25 | |
int cameraSelectSwich1Pin = 22; | |
int cameraSelectSwich2Pin = 23; | |
int cameraSelectSwich3Pin = 24; | |
int cameraSelectSwich4Pin = 25; | |
int cameraSelectSwich1PinState = LOW; | |
int cameraSelectSwich2PinState = LOW; | |
int cameraSelectSwich3PinState = LOW; | |
int cameraSelectSwich4PinState = LOW; | |
//First row: 26 - 29 | |
int floorLightFirstRow1Pin = 26; | |
int floorLightFirstRow2Pin = 27; | |
int floorLightFirstRow3Pin = 28; | |
int floorLightFirstRow4Pin = 29; | |
int floorLightFirstRow1PinState = LOW; | |
int floorLightFirstRow2PinState = LOW; | |
int floorLightFirstRow3PinState = LOW; | |
int floorLightFirstRow4PinState = LOW; | |
//Second row: 30 - 33 | |
int floorLightSecondRow1Pin = 30; | |
int floorLightSecondRow2Pin = 31; | |
int floorLightSecondRow3Pin = 32; | |
int floorLightSecondRow4Pin = 33; | |
int floorLightSecondRow1PinState = LOW; | |
int floorLightSecondRow2PinState = LOW; | |
int floorLightSecondRow3PinState = LOW; | |
int floorLightSecondRow4PinState = LOW; | |
//Third row: 33 - 36 | |
int floorLightThirdRow1Pin = 33; | |
int floorLightThirdRow2Pin = 34; | |
int floorLightThirdRow3Pin = 35; | |
int floorLightThirdRow4Pin = 36; | |
int floorLightThirdRow1PinState = LOW; | |
int floorLightThirdRow2PinState = LOW; | |
int floorLightThirdRow3PinState = LOW; | |
int floorLightThirdRow4PinState = LOW; | |
//Fourth row: 37 - 40 | |
int floorLightFourthRow1Pin = 37; | |
int floorLightFourthRow2Pin = 38; | |
int floorLightFourthRow3Pin = 39; | |
int floorLightFourthRow4Pin = 40; | |
int floorLightFourthRow1PinState = LOW; | |
int floorLightFourthRow2PinState = LOW; | |
int floorLightFourthRow3PinState = LOW; | |
int floorLightFourthRow4PinState = LOW; | |
//Game state | |
bool correctFloor1Combination = false; | |
bool correctFloor2Combination = false; | |
bool correctFloor3Combination = false; | |
bool correctFloor4Combination = false; | |
int floorPins[] = { | |
floorLightFirstRow1Pin, floorLightFirstRow2Pin, floorLightFirstRow3Pin, floorLightFirstRow4Pin, | |
floorLightSecondRow1Pin, floorLightSecondRow2Pin, floorLightSecondRow3Pin, floorLightSecondRow4Pin, | |
floorLightThirdRow1Pin, floorLightThirdRow2Pin, floorLightThirdRow3Pin, floorLightThirdRow4Pin, | |
floorLightFourthRow1Pin, floorLightFourthRow2Pin, floorLightFourthRow3Pin, floorLightFourthRow4Pin | |
}; | |
const int NUM_FLOOR_PINS = 12; | |
//1, 5, 9 | |
int floor1Combination[] = { | |
HIGH, LOW, LOW, LOW, | |
HIGH, LOW, LOW, LOW, | |
HIGH, LOW, LOW, LOW | |
}; | |
//2, 11, 12 | |
int floor2Combination[] = { | |
LOW, HIGH, LOW, LOW, | |
LOW, LOW, LOW, LOW, | |
LOW, LOW, HIGH, HIGH | |
}; | |
//7, 8, 10 | |
int floor3Combination[] = { | |
LOW, LOW, LOW, LOW, | |
LOW, LOW, HIGH, HIGH, | |
LOW, HIGH, LOW, LOW | |
}; | |
//4, 5, 6 | |
int floor4Combination[] = { | |
LOW, LOW, LOW, HIGH, | |
HIGH, HIGH, LOW, LOW, | |
LOW, LOW, LOW, LOW | |
}; | |
void setup() | |
{ | |
pinMode(cameraSelectSwich1Pin, INPUT); | |
pinMode(cameraSelectSwich2Pin, INPUT); | |
pinMode(cameraSelectSwich3Pin, INPUT); | |
pinMode(cameraSelectSwich4Pin, INPUT); | |
pinMode(floorLightFirstRow1Pin, INPUT); | |
pinMode(floorLightFirstRow2Pin, INPUT); | |
pinMode(floorLightFirstRow3Pin, INPUT); | |
pinMode(floorLightFirstRow4Pin, INPUT); | |
pinMode(floorLightSecondRow1Pin, INPUT); | |
pinMode(floorLightSecondRow2Pin, INPUT); | |
pinMode(floorLightSecondRow3Pin, INPUT); | |
pinMode(floorLightSecondRow4Pin, INPUT); | |
pinMode(floorLightThirdRow1Pin, INPUT); | |
pinMode(floorLightThirdRow2Pin, INPUT); | |
pinMode(floorLightThirdRow3Pin, INPUT); | |
pinMode(floorLightThirdRow4Pin, INPUT); | |
pinMode(floorLightFourthRow1Pin, INPUT); | |
pinMode(floorLightFourthRow2Pin, INPUT); | |
pinMode(floorLightFourthRow3Pin, INPUT); | |
pinMode(floorLightFourthRow4Pin, INPUT); | |
Serial.begin(9600); | |
while (!Serial) {} | |
initializeEthernet(); | |
keepConnectionUp(); | |
} | |
void loop() | |
{ | |
//Do logic | |
readAndSendChangedInputs(); | |
if(client.available() > 0){ | |
printEthernetResponse(); | |
} | |
//Reconnect if disconnected | |
keepConnectionUp(); | |
} | |
void keepConnectionUp() | |
{ | |
if (!client.connected()) { | |
client.connect(server, 80); | |
} | |
} | |
void readAndSendChangedInputs() | |
{ | |
//Compare channel pins | |
int cameraSelectSwich1PinStateNew = digitalRead(cameraSelectSwich1Pin); | |
if(cameraSelectSwich1PinStateNew != cameraSelectSwich1PinState) | |
{ | |
sendCameraUpdate(cameraSelectSwich1PinStateNew, "1"); | |
cameraSelectSwich1PinState = cameraSelectSwich1PinStateNew; | |
} | |
int cameraSelectSwich2PinStateNew = digitalRead(cameraSelectSwich2Pin); | |
if(cameraSelectSwich2PinStateNew != cameraSelectSwich2PinState) | |
{ | |
sendCameraUpdate(cameraSelectSwich2PinStateNew, "2"); | |
cameraSelectSwich2PinState = cameraSelectSwich2PinStateNew; | |
} | |
int cameraSelectSwich3PinStateNew = digitalRead(cameraSelectSwich3Pin); | |
if(cameraSelectSwich3PinStateNew != cameraSelectSwich3PinState) | |
{ | |
sendCameraUpdate(cameraSelectSwich3PinStateNew, "3"); | |
cameraSelectSwich3PinState = cameraSelectSwich3PinStateNew; | |
} | |
int cameraSelectSwich4PinStateNew = digitalRead(cameraSelectSwich4Pin); | |
if(cameraSelectSwich4PinStateNew != cameraSelectSwich4PinState) | |
{ | |
sendCameraUpdate(cameraSelectSwich4PinStateNew, "4"); | |
cameraSelectSwich4PinState = cameraSelectSwich4PinStateNew; | |
} | |
//Compare floor pins | |
bool floor1CombinationCheck = compareCombinations(floorPins, floor1Combination, NUM_FLOOR_PINS); | |
if(floor1CombinationCheck != correctFloor1Combination){ | |
sendLightsUpdate(floor1CombinationCheck, "1"); | |
correctFloor1Combination = floor1CombinationCheck; | |
} | |
bool floor2CombinationCheck = compareCombinations(floorPins, floor2Combination, NUM_FLOOR_PINS); | |
if(floor2CombinationCheck != correctFloor2Combination){ | |
sendLightsUpdate(floor2CombinationCheck, "2"); | |
correctFloor2Combination = floor2CombinationCheck; | |
} | |
bool floor3CombinationCheck = compareCombinations(floorPins, floor3Combination, NUM_FLOOR_PINS); | |
if(floor3CombinationCheck != correctFloor3Combination){ | |
sendLightsUpdate(floor3CombinationCheck, "3"); | |
correctFloor3Combination = floor3CombinationCheck; | |
} | |
bool floor4CombinationCheck = compareCombinations(floorPins, floor4Combination, NUM_FLOOR_PINS); | |
if(floor4CombinationCheck != correctFloor4Combination){ | |
sendLightsUpdate(floor4CombinationCheck, "4"); | |
correctFloor4Combination = floor4CombinationCheck; | |
} | |
} | |
boolean compareCombinations(int array1[], int array2[], int arrayLength) | |
{ | |
for(int i = 0; i < arrayLength; i++) | |
{ | |
if(digitalRead(array1[i]) != digitalRead(array2[i])){ | |
return false; | |
} | |
} | |
return true; | |
} | |
void sendCameraUpdate(boolean state, String channel) | |
{ | |
String stateString = state ? "true" : "false"; | |
String endpoint = buildEndpointString("GET", "/api/arduino/channels/", channel, stateString); | |
sendcall(endpoint); | |
} | |
void sendLightsUpdate(bool state, String channel) | |
{ | |
String stateString = state ? "true" : "false"; | |
String endpoint = buildEndpointString("GET", "/api/arduino/lights/", channel, stateString); | |
sendcall(endpoint); | |
} | |
/* | |
* ETHERNET | |
*/ | |
void printEthernetResponse() | |
{ | |
int responseLength = client.available(); | |
if (responseLength > 0) { | |
byte buffer[80]; | |
if (responseLength > 80) responseLength = 80; | |
client.read(buffer, responseLength); | |
Serial.write(buffer, responseLength); | |
byteCount = byteCount + responseLength; | |
} | |
} | |
void initializeEthernet(){ | |
Ethernet.init(10); | |
int dhcpConnectionResult = Ethernet.begin(mac); | |
if (dhcpConnectionResult == 0) { | |
Ethernet.begin(mac, ip, myDns); | |
} | |
delay(1000); | |
} | |
/* | |
* Usage: | |
* String b = SW1State ? "true" : "false"; | |
* String endpoint = buildEndpointString("GET", "/api/arduino/lights/", String(channel), b); | |
* sendcall(endpoint); | |
*/ | |
void sendcall(String s){ | |
int str_len = s.length() + 1; | |
char char_array[str_len]; | |
s.toCharArray(char_array, str_len); | |
sendCall(char_array); | |
} | |
void sendCall(char endpoint[]){ | |
client.println(endpoint); | |
client.println("Host: www.collabyrinth.be"); | |
client.println(); | |
} | |
String buildEndpointString(String protocol, String apiString, String channel, String value){ | |
String endpoint = protocol; | |
endpoint += " "; | |
endpoint += apiString; | |
endpoint += channel; | |
endpoint += "/"; | |
endpoint += value; | |
endpoint += " HTTP/1.1"; | |
return endpoint; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment