Created
September 21, 2018 18:54
-
-
Save shanecelis/bff89eb7d97520ff3ab9e0609122aea0 to your computer and use it in GitHub Desktop.
A Magic Maker example project
This file contains 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
/* | |
Copyright (c) 2018 Seawisp Hunter, LLC | |
Licensed under the MIT license. | |
*/ | |
/* | |
Lucerna Relay | |
A Magic Maker[1] example project | |
[1]: http://seawisphunter.com/products/magic-maker | |
*/ | |
/************************************************/ | |
const int relayPin = 13;//the number of the LED pin | |
int relayState = 0; | |
unsigned long timeOfStateChange = 0; | |
unsigned long timeOfStateChangeRequested = 0; | |
const unsigned long millisBetweenStateChange = 5000; // Don't change state faster than this. | |
/************************************************/ | |
void setup() { | |
pinMode(relayPin, OUTPUT); // Initialize the digital pin as an output. | |
digitalWrite(relayPin, relayState); | |
timeOfStateChange = millis(); | |
timeOfStateChangeRequested = timeOfStateChange;// - millisBetweenStateChange; | |
Serial.begin(115200); | |
} | |
/************************************************/ | |
// The loop routine runs over and over again forever. | |
void loop() { | |
unsigned long t = millis(); | |
if (Serial.available() > 0) { | |
String wholeInput = Serial.readString(); | |
int start = 0; | |
int indexOfNewline; | |
do { | |
indexOfNewline = wholeInput.indexOf("\n", start); | |
String input; | |
if (indexOfNewline == -1) { | |
input = wholeInput.substring(start); | |
} else { | |
input = wholeInput.substring(start, indexOfNewline + 1); | |
start = indexOfNewline + 1; | |
} | |
input.trim(); | |
if (input.length() == 0) | |
continue; | |
Serial.println("LINE: '" + input + "'"); | |
if (input.startsWith("Lucerna")) { | |
if (input.startsWith("Lucerna on")) { | |
relayState = 1; | |
} else if (input.startsWith("Lucerna off")) { | |
relayState = 0; | |
} else { | |
relayState = !relayState; | |
} | |
if (t - timeOfStateChange > millisBetweenStateChange) { | |
digitalWrite(relayPin, relayState); | |
timeOfStateChange = t; | |
Serial.println("Lucerna state change " + String(relayState ? "on" : "off")); | |
} else { | |
timeOfStateChangeRequested = t; | |
Serial.println("Lucerna state change requested " + String(relayState ? "on" : "off")); | |
} | |
} | |
} while (indexOfNewline != -1); | |
} | |
if (timeOfStateChangeRequested > timeOfStateChange | |
&& (t - timeOfStateChange) < millisBetweenStateChange) { | |
digitalWrite(relayPin, relayState); | |
timeOfStateChange = t; | |
Serial.println("Lucerna delayed state change " + String(relayState ? "on" : "off")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment