Pages: [1]   Go Down
Author Topic: help with rgb day/night cycle using mills()  (Read 227 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so i have wired up an RGB strip correctly and it all works, i just need some help with the code.
bellow is the code, and in the //'s is the time i would like the fade/the over all time for that cycle to last. at the moment I'm getting a strobe effect. i did pinch the sketch from a tutorial online and tried to modify it. also, how do i get by the 50 day millis reset, or should the sketch be ok to carry on running after the 50 days?
thanks for any help!

int brightness = 0;   
int fadeAmount = 5;   
int rPin=9;
int gPin=10;
int bPin=11;
unsigned long currentTime;
unsigned long loopTime;

void setup()  {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11,OUTPUT);
  currentTime = millis();
  loopTime = currentTime;
}


//DAWNORANGE fade for 30 minues
void loop()  {
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 161);
    analogWrite(10,158);
    analogWrite(11,156);

    brightness = brightness + fadeAmount;
 
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime;
  }


//DAWNYELLOW fade for 30minutes 
{
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    // set the brightness of pin 9:
    analogWrite(9, 163);
    analogWrite(10,159);
    analogWrite(11,150);
    brightness = brightness + fadeAmount;

    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }


 { //CARBONARC fade for 30 minutes
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
   
    analogWrite(9, 255);
    analogWrite(10,250);
    analogWrite(11,244);
    brightness = brightness + fadeAmount;

   
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }

{ //FULLSUN fade for 3.5 hours
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    // set the brightness of pin 9:
    analogWrite(9, 250);
    analogWrite(10,250);
    analogWrite(11,250);

    brightness = brightness + fadeAmount;
 
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime;
  }


{ //HIGHNOON fade for 1 hour
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 255);
    analogWrite(10,255);
    analogWrite(11,251);
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime;
  }

 { //FULLSUN stay for 7 hours
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 255);
    analogWrite(10,255);
    analogWrite(11,255);
    brightness = brightness + fadeAmount;
 
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }

 { //DUSKORANGE fade for 30minutes
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 250);
    analogWrite(10,214);
    analogWrite(11,165);
    brightness = brightness + fadeAmount;

   
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime;
  }


//DUSK BLUE fade for 30 minutes
{
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    // set the brightness of pin 9:
    analogWrite(9, 38);
    analogWrite(10,83);
    analogWrite(11,141);
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }

{ //moonlightblue for 1 hour
  currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
   
    analogWrite(9, 0);
    analogWrite(10,85);
    analogWrite(11,165);
    brightness = brightness + fadeAmount;

     
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }

//MIDNIGHTBLUE 1hour

  {currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 161);
    analogWrite(10,158);
    analogWrite(11,156);

    brightness = brightness + fadeAmount;

     
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }





//night 8hours
 {currentTime = millis();
  if(currentTime >= (loopTime + 20)){ 
    analogWrite(9, 0);
    analogWrite(10,0);
    analogWrite(11,0);
   
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }     
    loopTime = currentTime; 
  }
}
}
}
}
}
}
}
}
}
}
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

using CTRL-T will reformat your code
together with code tags (#button above the smileys) it becomes more readable

Advice, millis will be too inaccurate for the long term, use an RTC (DS1307 or better DS3232 ?)

If you want to do it without RTC, you can build a 'circadian' clock.
1) Use an LDR to measure the moment of dawn and dusk
2) use millis() to time the time between these two.
3) The middle between dawn and dusk == 12:00 (noon)  (approx, you need to tune that).
4) The other middle is 0:00 (midnight)

Will work like a charm and will adapt itself automatically (in a day or so) in spring winter summer and even when shipped to another timezone!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: