Skip to content

Instantly share code, notes, and snippets.

@jasp
Last active August 22, 2017 04:44
Show Gist options
  • Save jasp/8e64b643e8640a278fda577ff212ebc8 to your computer and use it in GitHub Desktop.
Save jasp/8e64b643e8640a278fda577ff212ebc8 to your computer and use it in GitHub Desktop.
Simple serial controllable timer using relay
int timer;
String buffer;
int relay = 7;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect
}
pinMode(relay, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
switch (Serial.read()) {
case 'T':
buffer = Serial.readStringUntil('\n');
timer = buffer.toInt();
digitalWrite(relay, HIGH);
delay(timer);
digitalWrite(relay, LOW);
Serial.println("OK");
break;
case 'H':
digitalWrite(relay, HIGH);
Serial.println("OK");
break;
case 'L':
digitalWrite(relay, LOW);
Serial.println("OK");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment