Arduino sunrise and sunset lamp Help

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)

Hello Peter. Can I suggest that you post your code here rather than attaching it. Paste it into a forum post, select it, then click on the code button (</>) far left above the editor window. This will put it in code tags which makes it easier to copy/paste into an editor to provide help.

For that i have changed the lookup function backwards and renamed it lookdown.

As opposed to simply accessing the lookup array in the reverse order?

Where do you use lookdown?

UKHeliBob:
Hello Peter. Can I suggest that you post your code here rather than attaching it. Paste it into a forum post, select it, then click on the code button (</>) far left above the editor window. This will put it in code tags which makes it easier to copy/paste into an editor to provide help.

Can you also link to the instructables article so we can see why the code is so complex for fading an LED.

Thanks for your answers:

here is the instructables link http://www.instructables.com/id/Simple-Arduino-Sunrise-Lamp/.

I had the lookdown function inserted in the the second led state=low area but it didnt worked so i deleted it

Riva:
Can you also link to the instructables article so we can see why the code is so complex for fading an LED.

The fading is so complex because in the final scetch the fading time is about 30 minutes and with the normal fade from 0 to 255 it wouldnt be smooth enough.

tigerblood:
The fading is so complex because in the final scetch the fading time is about 30 minutes and with the normal fade from 0 to 255 it wouldnt be smooth enough.

The attached sketch will fade a pin over about 65500 steps so should be smoother still. It’s possible advantage is the loop() code is not time critical as all the work is done in interrupts. All the main loop needs to do is set the level.

sketch_nov29a_Fader.ino (1.74 KB)