LED Fading Back-to-Back

Hi all,

I’m working on a project and I’m a bit stuck.

I’m designing a fish tank light using a two strips of LEDs- one white, one red. At roughly 8AM, I’d like the white strip to turn on slowly and increase to full brightness within a half-hour. At 8PM I’d like the white bar to begin to fade until it reaches an analog brightness of 15 or so, at which point the red bar will turn on slowly and increase in brightness as the white bar fades to 0 at its original rate. The red bar will continue to grow in brightness for a half-hour and then stay at full brightness for the rest of the night, until 8AM, when the cycle repeats itself.

The trouble I’m having occurs when I want the red LED strip to begin to increase in brightness while the white one fades to 0 and stays there.

I’m uploading the file I currently have- don’t mock, I know its not great. :stuck_out_tongue:

Any help would be greatly appreciated!

Thanks!

FishTank.ino (890 Bytes)

I should also add that I am going to be using an ATtiny85 with two TIP120 transistors to power the LED strips, but I am currently just trying to figure this out on a breadboard using single LEDs.

Thanks!

You will get more help if you post your code in the post's body not a file.
On my iPad I can't download an arduino file and then look at it.
See the how to use this forum sticky for how to post code correctly.

Thanks for the reply! Sorry that took forever- was at the Detroit Motor Show all day! Here is my code:

int ledred = 9;
int redbrightness = 1;
int yellowincrease = 1;

int ledyellow = 10;
int yellowbrightness = 1;
int redincrease = 1;



void setup(){
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
}



void loop(){
  
//Morning
  analogWrite(ledyellow, yellowbrightness);
  if (yellowbrightness == 0){
    yellowincrease=1;
  }
  if (yellowbrightness == 255){
    yellowincrease=-1;
  }
  delay(10);
  yellowbrightness=yellowbrightness+yellowincrease;
  

//Night
  
    
  if (yellowbrightness == 19 && yellowincrease == -1){
    analogWrite(ledred, redbrightness);
    delay(20);
  }
  
  if (yellowbrightness == 0){
    analogWrite(ledred, redbrightness);
  }
  redbrightness=redbrightness+redincrease;
  
  
  //while (yellowbrightness = LOW){
    //analogWrite(ledred, redbrightness);
    //redbrightness=redbrightness+1;
    //if (redbrightness == 0){
      //increase = 1;
    //}
    
    //if (redbrightness == 255){
      //increase = -1;
    //}
  //}

I can manage to turn the red LED on at a specific point within the yellow cycle, but am having trouble making it start a new cycle that is separate from the yellow one.

I have worked on my code and have figured out how to make the LEDs fade at the correct times, but am having trouble repeating the loop! Essentially, I want to reset the millis() function every day to begin the loop over again using the same increments. I’ve looked at the “Blink without Delay” example to try and get inspiration but I can’t seem to figure out how to place it in my code. If anyone has an idea of how to go about effectively “resetting” the millis() function with code, I’d be grateful.

FYI, the times I’ve used obviously aren’t useful for the final project, but they are useful for understanding how the code works practically on a breadboard.

Thanks!

int blue = 9;
int red = 10;
unsigned long previous = 0;
int brightness = 0;
int increase = 1;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop(){
  
  //Day
  
  unsigned long current = millis();
  if (current > 0 && current < 5100){
    analogWrite(blue, brightness);
    brightness = brightness + increase;
    delay(20);
  }
  
  if (current > 5100 && current < 10000){
    analogWrite(blue, 255);
  }
  
  if (current > 10000 && current < 15100){
    analogWrite(blue, brightness);
    brightness = brightness - increase;
    delay(20);
  }
  
  if (current > 16000){
    analogWrite(blue, 0);
  }
  
  
  // Night 
  
  if (current > 16000 && current < 21100){
    analogWrite(red, brightness);
    brightness = brightness + increase;
    delay(20);
  }
  
  if (current > 21100 && current < 26000){
    analogWrite(red, 255);
  }
  
  if (current > 26000 && current < 31100){
    analogWrite(red, brightness);
    brightness = brightness - increase;
    delay(20);
  }
  
  if (current >= 31100){
    analogWrite(red, 0);
  }
  

  
}

I want to reset the millis() function every day

Classic beginners error, you NEVER need to reset the millis timer.

Do not use absolute numbers in your code they need to be relative. That is they should all be differences from the time now to the time you want anything to happen.
So to see if it is time to do something subtract the time now given by millis() from your reference time and see if it is bigger than the interval you want that thing to happen. Then while it is happening reset your reference time to your current time and it will do it again after that interval.

I did my homework, and I understand that millis() does not need to be reset and can be worked around using the code you lined out- that is, until millis() becomes too large after 60 days or whatever the interval is. Is there any way you can quickly sketch out how to actually code what you have written here? It seems like a simple fix but I can't quite get a grasp on it until I see it.

Thanks!

that is, until millis() becomes too large after 60 days or whatever the interval is.

No, using that technique there is no problem with wrap round it will work forever.

Is there any way you can quickly sketch out how to actually code what you have written here?

It is the blink without delay example.

Predictably, it was ONE character that was screwing it up. I changed the last if statement to
if (current - previous >= 31100)

Where I had == instead of >= in the original code.

Thanks for your help!

Finished code:

int blue = 9;
int red = 10;
unsigned long previous = 0;
int brightness = 0;
int increase = 1;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop(){
  
  //Day
  
  unsigned long current = millis();
  
  if (current - previous > 0 && current - previous < 5100){
    analogWrite(blue, brightness);
    brightness = brightness + increase;
    delay(20);
    
  }
  
  if (current - previous > 5100 && current - previous < 10000){
    analogWrite(blue, 255);
  }
  
  if (current - previous > 10000 && current - previous < 15100){
    analogWrite(blue, brightness);
    brightness = brightness - increase;
    delay(20);
  }
  
  if (current - previous > 16000){
    analogWrite(blue, 0);
  }
  
  
  // Night 
  
  if (current - previous > 16000 && current - previous < 21100){
    analogWrite(red, brightness);
    brightness = brightness + increase;
    delay(20);
  }
  
  if (current - previous > 21100 && current - previous < 26000){
    analogWrite(red, 255);
  }
  
  if (current - previous > 26000 && current - previous < 31100){
    analogWrite(red, brightness);
    brightness = brightness - increase;
    delay(20);
  }
  
  if (current - previous >= 31100){
    analogWrite(red, 0);
    previous = current;
  }
  

  
}