Hy guys
my name is peter and i am buildung a sunrise/sunset lamp for my aquarium.
Im am using the sketch of the “simple arduino sunrise lamp” from instructables.
The sketch fades the led during one minute to high an stays on for another minute. Now i want archieve, that the led fades after the one minute high down to low. For that i have changed the lookup function backwards and renamed it lookdown. How can i achieve my goal.
The sketch can be found in the attachment.
Thanks for your help.
Yours Peter
int i=0;
const int pulsepin = 3; // the PWM pin the Sunrise-LED is attached to
int ledState;
unsigned long previousMicros=0;
unsigned long previousMillis=0;
int led = 1; // the PWM pin the Status-LED is attached to
int brightness = 20; // how bright the LED is
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};
int lookdown[64] = {992,992,992,961,930,900,870,841,812,
784,756,729,702,650,676,625,600,576,552,529,
506,484,462,441,420,400,380,361,342,324,306,
289,272,256,240,225,210,196,182,169,156,144,
132,121,110,100,90,81,72,64,56,42,
36,30,25,20,16,12,9,6,4,2,1};
unsigned long lampOnMinutes = 1; //length of time the lamp will be on since sunrise ended
unsigned long lampOnMilliSeconds = (lampOnMinutes*60*1000);
int duty; //high time in microseconds
long sunriseMin = 1; //length of sunrise in minutes
long stepMillis = sunriseMin*60*1000/64;
long pulsePeriodMicros = 5000; //pulse period in microseconds
void setup()
{
pinMode(pulsepin, OUTPUT);
// Serial.begin(9600);
pinMode(led, OUTPUT); // Status Led;
analogWrite(led, brightness);
}
void loop()
{
unsigned long currentMicros = micros();
unsigned long currentMillis = millis();
duty = lookup[i]*5;
if (i<64){
if(currentMicros - previousMicros >pulsePeriodMicros){
previousMicros=currentMicros;
}
if(currentMicros - previousMicros < duty){
ledState=HIGH;
}
if(currentMicros - previousMicros > duty){
ledState = LOW;
}
if(currentMillis - previousMillis >stepMillis){
previousMillis = currentMillis;
i++;
// Serial.print("i=");
// Serial.println(i);
}
}
if(i==64){
if (currentMillis - previousMillis < lampOnMilliSeconds){
ledState = HIGH;
// Serial.print("Time elapsed since sunrise=");
// Serial.println(currentMillis - previousMillis);
// delay(10000);
}
if (currentMillis - previousMillis > lampOnMilliSeconds){
ledState = LOW;
}
}
digitalWrite(pulsepin, ledState);
}
Sunrise.txt (2.3 KB)