Skip to content

Instantly share code, notes, and snippets.

@AlexanderBrevig
Last active August 29, 2015 14:04
Show Gist options
  • Save AlexanderBrevig/9b4fa8b42b2733ab9075 to your computer and use it in GitHub Desktop.
Save AlexanderBrevig/9b4fa8b42b2733ab9075 to your computer and use it in GitHub Desktop.
Office project, light guiding to door
const int NUMBER_OF_ZONES = 19;
const int DOOR_OPEN_MS = 5 * 1000; // 5 seconds
const int ZONE_ON_MS = 10 * 60 * 1000; // 10 minutes
unsigned int zoneLastOn[NUMBER_OF_ZONES] = {};
void loop() {
//this is the multitasking part
//we just check the time that has passed since
//we last wanted it on and act accordingly
for (int i=0; i<NUMBER_OF_ZONES; i++) {
//if the last time we set zone on
//plus the amount of time we want it on
//is greater than the the actual time
//it's time to shut it off
if (zoneLastOn[i] + ZONE_ON_MS > millis()) {
digitalWrite(i + 2, LOW); //shift zone to pin
}else{ //else we want it on
digitalWrite(i + 2, HIGH);
}
}
}
//replace digitalWrite in the original sketch with this
//not sure you really ever want to tell the zone to go off?
//what would happen in that case?
//I will assume onOrOFf to be true always
void enableZone(int zone, int onOrOff)
{
//since this method is called
//based on a message from the app
//we want the zone to be on from now until ZONE_ON_MS
//has passed,
//and all the preceeding zones
int middleZone = NUMBER_OF_ZONES / 2;
if (zone < middleZone) {
for (int i=0; i<=zone; i++) {
zoneLastOn[i] = millis();
}
} else {
for (int i=zone; i>=0; i--) {
zoneLastOn[i] = millis();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment