Pages: [1]   Go Down
Author Topic: need a longer rgb fade  (Read 474 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is my code:
i cannot figure out for the life of me make it fade over all the colors slowly over an hour. than repeat and so on.
Code:
// attiny85 RGB led rainbow fade

const int redPin = 1; 
const int grnPin = 2; 
const int bluPin = 0;


void setup()
{
  pinMode(redPin, OUTPUT);     
  pinMode(grnPin, OUTPUT);     
  pinMode(bluPin, OUTPUT);     
}

void loop()
{
redtoyellow();
yellowtogreen();
delay(0);
greentocyan();
cyantoblue();
delay(0);
bluetomajenta();
majenatored();
delay(0);
}


void redtoyellow()
{
   digitalWrite(redPin, HIGH);
   digitalWrite(bluPin, LOW);

  // fade up green
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void yellowtogreen()
{
   digitalWrite(grnPin, HIGH);
   digitalWrite(bluPin, LOW);

  // fade down red
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  }
}


void greentocyan()
{
   digitalWrite(grnPin, HIGH);
   digitalWrite(redPin, LOW);

  // fade up blue
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void cyantoblue()
{
   digitalWrite(bluPin, HIGH);
   digitalWrite(redPin, LOW);

  // fade down green
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  }
}


void bluetomajenta()
{
   digitalWrite(bluPin, HIGH);
   digitalWrite(grnPin, LOW);

  // fade up red
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void majenatored()
{
   digitalWrite(redPin, HIGH);
   digitalWrite(grnPin, LOW);

  // fade down blue
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why are you using such a complicated pwm method? why not just use the built in analogwrite function?
EDIT: are you using an attiny85?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
delay(0);
delay(0);
delay(0);
Why?

What does your code do now? How long does it take to cycle through the color range?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yah its for an attiny85.. also about the delay(0) I left it there because I did have a delay before but I took it away. And If i wanted it back then I would like to add it quickly instead of right the couple of lines of code again lol
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i cannot figure out for the life of me make it fade over all the colors slowly over an hour.

Rather than repeat it 100x, repeat it 1000x, or 10000x, etc. until you are happy.
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a sketch I wrote to create a colour cycling mood lamp using an ATmega85. Just alter mainDelay to adjust the loop time. The result is here http://arduino.cc/forum/index.php/topic,134827.0.html with a fast cycle time to reduce video length  smiley

* ATtiny_10W_LED_Moodlamp_v01.ino (3.46 KB - downloaded 5 times.)
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Pages: [1]   Go Up
Jump to: