Skip to content

Instantly share code, notes, and snippets.

@Rushilwiz
Created January 6, 2023 02:09
Show Gist options
  • Save Rushilwiz/134476df1e1760a642a0e7b1072a75db to your computer and use it in GitHub Desktop.
Save Rushilwiz/134476df1e1760a642a0e7b1072a75db to your computer and use it in GitHub Desktop.
arduino code for the time scheduled desk lamp I have
// include the Time library
#include <Time.h>
// define the pin that the light is connected to
const int LIGHT_PIN = 9;
// set the time that the light should be turned off
const int TURN_OFF_HOUR = 22; // 22:00 (10:00 PM)
const int TURN_OFF_MINUTE = 0;
void setup() {
// set the light pin to be an output
pinMode(LIGHT_PIN, OUTPUT);
}
void loop() {
// get the current time
time_t currentTime;
getLocalTime(&currentTime);
// if it is time to turn off the light, do so
if (hour(currentTime) == TURN_OFF_HOUR && minute(currentTime) == TURN_OFF_MINUTE) {
digitalWrite(LIGHT_PIN, LOW);
}
// otherwise, turn the light on
else {
digitalWrite(LIGHT_PIN, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment