/* Sunrise 2 * Detects the alarm noise (but doesn't let it play), then initiates sunrise sequence. */ const int LED_1 = 5; const int LED_2 = 10; const int LED_3 = 11; const int SPEAK = 6; const int buttonPin = 2; int sensorValue = 0; boolean SOUND_ON=false; boolean SYSTEM_ON=false; int fade = 0; int lastReading=LOW; int reading = LOW; int duty = 0; int steps = 64; int sunrisespeed = 1800; //use value of 1800 when done testing, number of seconds for complete sunrise int i,j; int lookup[64] = { 1,2,4,6,9,12,16,20,25,30,36, 42,49,56,64,72,81,90,100,110,121,132, 144,156,169,182,196,210,225,240,256,272,289, 306,324,342,361,380,400,420,441,462,484,506, 529,552,576,600,625,650,676,702,729,756,784, 812,841,870,900,930,961,992,992,992 }; void setup() { pinMode(LED_1, OUTPUT); pinMode(SPEAK, OUTPUT); Serial.begin(9600); digitalWrite(LED_1, LOW); } void loop() { reading = digitalRead(buttonPin); if((reading == HIGH) && (lastReading == LOW)) { if(SYSTEM_ON) { digitalWrite(LED_1, LOW); digitalWrite(LED_2, LOW); digitalWrite(LED_3, LOW); digitalWrite(SPEAK, LOW); SYSTEM_ON = false; SOUND_ON = false; } else { digitalWrite(LED_1, HIGH); digitalWrite(LED_2, HIGH); digitalWrite(LED_3, HIGH); SYSTEM_ON = true; sunset(); } } lastReading = reading; if (!SOUND_ON) { //sum values picked up on the speaker (doesn't handle negative very well). Could rectify it, but this works fine. sensorValue = 0; for (i=0; i<100; i++) { sensorValue = sensorValue + analogRead(A0); } if (sensorValue > 500) { SOUND_ON=true; SYSTEM_ON=true; sunrise(); } } } void sunrise() { //64 steps. Time on each step is 5ms * sunrise speed. Sunrise speed=60 means a 1minute sunrise. Sunrise speed=1800 means a 30 minute sunrise. //Table taken from www.instructables.com/id/Simple-Arduino-Sunrise-Lamp/ //LED_2 = PWM if(SOUND_ON) { for (i=0; i