int sunriseDuration; #define SUN_PIN 3 // true if the sun is currently rising (light going on), it is set in the alarm() function bool sunrise = false; // the time in millis() the time started rising unsigned long sunriseTime; // This table is used to let the LED get brighter steady. This is needed because the eye is // seeing brightness logarithmical. More on this here (german) https://www.mikrocontroller.net/articles/LED-Fading const int16_t pwmtable[1000] PROGMEM = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38, 38, 39, 39, 40, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 54, 54, 55, 55, 56, 56, 57, 58, 58, 59, 60, 60, 61, 62, 62, 63, 64, 64, 65, 66, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 114, 115, 116, 117, 119, 120, 121, 122, 124, 125, 126, 128, 129, 131, 132, 133, 135, 136, 138, 139, 141, 142, 144, 145, 147, 149, 150, 152, 153, 155, 157, 158, 160, 162, 164, 165, 167, 169, 171, 173, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 201, 203, 205, 207, 209, 212, 214, 216, 219, 221, 223, 226, 228, 231, 233, 236, 238, 241, 243, 246, 249, 251, 254, 257, 260, 262, 265, 268, 271, 274, 277, 280, 283, 286, 289, 292, 295, 299, 302, 305, 308, 312, 315, 318, 322, 325, 329, 332, 336, 340, 343, 347, 351, 355, 358, 362, 366, 370, 374, 378, 382, 386, 390, 395, 399, 403, 408, 412, 416, 421, 426, 430, 435, 439, 444, 449, 454, 459, 464, 469, 474, 479, 484, 489, 495, 500, 505, 511, 516, 522, 527, 533, 539, 545, 551, 557, 563, 569, 575, 581, 587, 594, 600, 607, 613, 620, 626, 633, 640, 647, 654, 661, 668, 675, 683, 690, 697, 705, 713, 720, 728, 736, 744, 752, 760, 768, 776, 785, 793, 802, 811, 819, 828, 837, 846, 855, 865, 874, 883, 893, 902, 912, 922, 932, 942, 952, 963, 973, 983, 994, 1005, 1016, 1027, 1038, 1049, 1060, 1072, 1083, 1095, 1107, 1119, 1131, 1143, 1155, 1168, 1180, 1193, 1206, 1219, 1232, 1246, 1259, 1273, 1286, 1300, 1314, 1328, 1343, 1357, 1372, 1387, 1402, 1417, 1432, 1448, 1463, 1479, 1495, 1511, 1527, 1544, 1561, 1577, 1595, 1612, 1629, 1647, 1665, 1682, 1701, 1719, 1738, 1756, 1775, 1794, 1814, 1833, 1853, 1873, 1893, 1914, 1935, 1955, 1977, 1998, 2020, 2041, 2063, 2086, 2108, 2131, 2154, 2177, 2201, 2224, 2248, 2273, 2297, 2322, 2347, 2373, 2398, 2424, 2450, 2477, 2503, 2530, 2558, 2585, 2613, 2642, 2670, 2699, 2728, 2757, 2787, 2817, 2848, 2878, 2910, 2941, 2973, 3005, 3037, 3070, 3103, 3137, 3171, 3205, 3239, 3274, 3310, 3346, 3382, 3418, 3455, 3492, 3530, 3568, 3607, 3646, 3685, 3725, 3765, 3806, 3847, 3888, 3930, 3973, 4016, 4059, 4103, 4147, 4192, 4237, 4283, 4329, 4376, 4423, 4471, 4519, 4568, 4617, 4667, 4718, 4769, 4820, 4872, 4925, 4978, 5032, 5086, 5141, 5196, 5253, 5309, 5367, 5424, 5483, 5542, 5602, 5663, 5724, 5786, 5848, 5911, 5975, 6040, 6105, 6171, 6237, 6305, 6373, 6442, 6511, 6581, 6652, 6724, 6797, 6870, 6944, 7019, 7095, 7172, 7249, 7328, 7407, 7487, 7567, 7649, 7732, 7815, 7900, 7985, 8071, 8158, 8246, 8335, 8425, 8516, 8608, 8701, 8795, 8890, 8986, 9083, 9181, 9281, 9381, 9482, 9584, 9688, 9793, 9898, 10005, 10113, 10222, 10333, 10444, 10557, 10671, 10786, 10903, 11021, 11140, 11260, 11381, 11504, 11628, 11754, 11881, 12009, 12139, 12270, 12402, 12536, 12672, 12809, 12947, 13087, 13228, 13371, 13515, 13661, 13809, 13958, 14108, 14261, 14415, 14570, 14728, 14887, 15048, 15210, 15374, 15540, 15708, 15878, 16049, 16222, 16398, 16575, 16754, 16935, 17117, 17302, 17489, 17678, 17869, 18062, 18257, 18454, 18653, 18855, 19058, 19264, 19472, 19682, 19895, 20110, 20327, 20546, 20768, 20992, 21219, 21448, 21680, 21914, 22150, 22390, 22631, 22876, 23123, 23372, 23625, 23880, 24138, 24398, 24662, 24928, 25197, 25469, 25744, 26022, 26303, 26587, 26874, 27165, 27458, 27754, 28054, 28357, 28663, 28973, 29285, 29602, 29921, 30244, 30571, 30901, 31235, 31572, 31913, 32257, 32606, 32958, 33314, 33673, 34037, 34405, 34776, 35152, 35531, 35915, 36303, 36695, 37091, 37491, 37896, 38305, 38719, 39137, 39560, 39987, 40419, 40855, 41296, 41742, 42193, 42648, 43109, 43574, 44045, 44520, 45001, 45487, 45978, 46475, 46976, 47484, 47996, 48515, 49039, 49568, 50103, 50644, 51191, 51744, 52303, 52867, 53438, 54015, 54598, 55188, 55784, 56386, 56995, 57611, 58233, 58861, 59497, 60139, 60789, 61445, 62109, 62779, 63457, 64142, 64835, 65535 }; // Set the timer for the PWM void initTimer() { // Fast PWM on Pin 10, ICR1 Top TCCR1A = (1 << COM1B1) | (1 << WGM11); // no prescaler TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10); ICR1 = 0xFFFF; OCR1B = 0; pinMode(10, OUTPUT); } // This function is called on every loop() void doSunrise() { // If the sun is currently not rising do nothing if(!sunrise) return; // If the up button was long pressed turn the light of if(buttonLongPressed[BTN_UP]) { sunrise = false; OCR1B = 0; return; } // Calculate the progress of the sunrise. The result is a value between 0 and 1 float progress = (float) (millis() - sunriseTime) / (sunriseDuration*60.f*1000); if(progress < 1) { // Set the timer compare value according to the pwmtable OCR1B = pgm_read_word(&pwmtable[(int) (999 * progress)]); } }