Skip to content

Instantly share code, notes, and snippets.

@malceore
Created September 4, 2017 22:20
Show Gist options
  • Select an option

  • Save malceore/7dcfd1381bcf97c113e200be6859f694 to your computer and use it in GitHub Desktop.

Select an option

Save malceore/7dcfd1381bcf97c113e200be6859f694 to your computer and use it in GitHub Desktop.
A serial program that listens for input on teh serial and flips gpio/relays automatically.
// Globals
int ONE = 19;
int TWO = 20;
int THREE = 21;
int FOUR = 18;
void setup() {
// Put your setup code here, to run once:
pinMode(ONE, OUTPUT);
pinMode(TWO, OUTPUT);
pinMode(THREE, OUTPUT);
pinMode(FOUR, OUTPUT);
// Setup serial connection so we cna talk to board over USB
Serial.begin(9600); // baud rate
Serial.flush();
}
// MAIN LOOP
void loop() {
// Take input from serial and
String input = "";
// Read any serial input
while (Serial.available() > 0){
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
// Decode what message wants program to do.
if (input == "one:off"){
digitalWrite(ONE, LOW);
}else if(input =="one:on"){
digitalWrite(ONE, HIGH); // on
}else if (input == "two:off"){
digitalWrite(TWO, LOW);
}else if(input == "two:on"){
digitalWrite(TWO, HIGH); // on
}else if (input == "three:off"){
digitalWrite(THREE, LOW);
}else if(input == "three:on"){ //
digitalWrite(THREE, HIGH);
}else if (input == "four:off"){ //off
digitalWrite(FOUR, LOW);
}else if(input == "four:on"){ //on
digitalWrite(FOUR, HIGH);
}
}
//END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment