Created
February 26, 2017 20:34
-
-
Save tokolist/7203a5b6044c620062aa6cd69f830e5d to your computer and use it in GitHub Desktop.
Doll House Arduino
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
struct LedSwitch { | |
int buttonPin; | |
int ledPin; | |
int buttonState; | |
}; | |
LedSwitch ledSwitches[] = { | |
{6,2,LOW}, | |
{7,3,LOW}, | |
{8,4,LOW}, | |
{9,5,LOW}, | |
}; | |
int ledSwitchesNum = sizeof(ledSwitches)/sizeof(LedSwitch); | |
void setup() { | |
for(int i=0; i<ledSwitchesNum; i++){ | |
pinMode(ledSwitches[i].buttonPin, INPUT); | |
pinMode(ledSwitches[i].ledPin, OUTPUT); | |
digitalWrite(ledSwitches[i].ledPin, LOW); | |
ledSwitches[i].buttonState = digitalRead(ledSwitches[i].buttonPin); | |
} | |
//Initialize serial and wait for port to open: | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB | |
} | |
} | |
void loop() { | |
for(int i=0; i<ledSwitchesNum; i++){ | |
int currBtnState = digitalRead(ledSwitches[i].buttonPin); | |
if(currBtnState == HIGH && ledSwitches[i].buttonState == LOW){ | |
int newState = !digitalRead(ledSwitches[i].ledPin); | |
digitalWrite(ledSwitches[i].ledPin, newState); | |
Serial.print("LSC" + String(i) + "S" + String(newState) + "\n"); | |
} | |
ledSwitches[i].buttonState = currBtnState; | |
} | |
} | |
void serialEvent(){ | |
String input = Serial.readStringUntil('\n'); | |
if (input.length() >= 3){ | |
String cmd = input.substring(0, 3); | |
if (cmd == "LSS"){ | |
int ledNum = input.substring(3,4).toInt(); | |
int state = input.substring(5,6).toInt(); | |
digitalWrite(ledSwitches[ledNum].ledPin, state ? HIGH : LOW); | |
} else if (cmd == "LQS") { | |
int ledNum = input.substring(3,4).toInt(); | |
Serial.print("LSR" + String(ledNum) + "S" + digitalRead(ledSwitches[ledNum].ledPin) + "\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment