Last active
February 15, 2025 05:54
-
-
Save astrolitterbox/e1331f419e4c479e5a11 to your computer and use it in GitHub Desktop.
Arduino sunrise simulator
This file contains hidden or 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
int duty = 0; | |
int steps = 256; //no. of light levels | |
int sunrisespeed = 20;//no. of pulses, change to ~5400 to have 30min-long sunrise | |
int minutesUntilSunrise = 0; //change to time until sunrise in minutes | |
int i; | |
int j; | |
int pulsepin = 9; | |
unsigned long currentMillis = 0; | |
int previousMillis = 0; | |
int currentMinutes = 0; | |
const unsigned char cie[256] = { | |
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, | |
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, | |
3, 4, 4, 4, 4, 4, 4, 5, 5, 5, | |
5, 5, 6, 6, 6, 6, 6, 7, 7, 7, | |
7, 8, 8, 8, 8, 9, 9, 9, 10, 10, | |
10, 10, 11, 11, 11, 12, 12, 12, 13, 13, | |
13, 14, 14, 15, 15, 15, 16, 16, 17, 17, | |
17, 18, 18, 19, 19, 20, 20, 21, 21, 22, | |
22, 23, 23, 24, 24, 25, 25, 26, 26, 27, | |
28, 28, 29, 29, 30, 31, 31, 32, 32, 33, | |
34, 34, 35, 36, 37, 37, 38, 39, 39, 40, | |
41, 42, 43, 43, 44, 45, 46, 47, 47, 48, | |
49, 50, 51, 52, 53, 54, 54, 55, 56, 57, | |
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, | |
68, 70, 71, 72, 73, 74, 75, 76, 77, 79, | |
80, 81, 82, 83, 85, 86, 87, 88, 90, 91, | |
92, 94, 95, 96, 98, 99, 100, 102, 103, 105, | |
106, 108, 109, 110, 112, 113, 115, 116, 118, 120, | |
121, 123, 124, 126, 128, 129, 131, 132, 134, 136, | |
138, 139, 141, 143, 145, 146, 148, 150, 152, 154, | |
155, 157, 159, 161, 163, 165, 167, 169, 171, 173, | |
175, 177, 179, 181, 183, 185, 187, 189, 191, 193, | |
196, 198, 200, 202, 204, 207, 209, 211, 214, 216, | |
218, 220, 223, 225, 228, 230, 232, 235, 237, 240, | |
242, 245, 247, 250, 252, 255, | |
}; | |
void setup() | |
{ | |
pinMode(pulsepin, OUTPUT); | |
} | |
void loop(){ | |
if(millis() >= previousMillis+86400000){ | |
// a full day has elapsed, reset the clock; | |
previousMillis +=86400000; | |
} | |
currentMillis = millis() - previousMillis; // this keeps our currentMillis the same each day | |
currentMinutes = (currentMillis/1000)/60; | |
// turn this on for debugging | |
//digitalWrite(pulsepin, LOW); | |
//delay(2000); | |
Serial.begin(9600); | |
if(currentMinutes >= minutesUntilSunrise){ | |
for (i=0; i<steps; i++) | |
{ | |
duty = 20*cie[i]; | |
Serial.println(duty); | |
Serial.println(5120-duty); | |
for (j=0; j<sunrisespeed; j++) | |
{ | |
// one pulse of PWM | |
if(duty > 0){ | |
digitalWrite(pulsepin, HIGH); | |
delayMicroseconds(duty); | |
} | |
digitalWrite(pulsepin, LOW); | |
delayMicroseconds(5120-duty); | |
} | |
} | |
Serial.println("Sunrise ended"); | |
digitalWrite(pulsepin, HIGH); | |
delay(5000); //change value to how long you'd like the LEDs to shine at full brightness, in milliseconds | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment