Go Down

Topic: PWM Styring af RGB LED (Read 2 times) previous topic - next topic

Brian Pedersen

ups.. 3 fader.. 5 og 6 er slukket når man lige vender dioderne rigtigt ;)

//Brian

Brian Pedersen

Fandt lige denne her kode, som ser ud til at virke ganske glimrende, nu skal jeg bare have en RGB Led på, så jeg bedre kan se effekten..

Code: [Select]
// 2009 Kenneth Finnegan
// kennethfinnegan.blogspot.com

// Pins. Must be PWM pins.
#define REDLED  3
#define BLUELED  5
#define GREENLED  6
// Can't be higher than 255
#define MAXBRIGHT  200

// s[tate] t[arget] colors
byte s[3] = {0}, t[3] = {0};

void setup() {
 // Make the random MORE RANDOM!
 randomSeed(analogRead(0));
 
 // Initialization wiring check
 // You should see RED, BLUE, then GREEN.
 // If you don't, then you ballsed something up.
 analogWrite(REDLED, MAXBRIGHT);
 delay(1000);
 analogWrite(REDLED, 0);
 analogWrite(BLUELED, MAXBRIGHT);
 delay(1000);
 analogWrite(BLUELED, 0);
 analogWrite(GREENLED, MAXBRIGHT);
 delay(1000);
 analogWrite(GREENLED, 0);
}

void loop() {
 byte i, offset;
 
 // Select the next target color
 // Start from a random one of the three colors to prevent
 // the cycle from being red biased.
 offset = random(3);
 t[offset] = random(MAXBRIGHT);
 t[(offset+1)%3] = random(MAXBRIGHT - t[offset]);
 t[(offset+2)%3] = MAXBRIGHT - t[offset] - t[(offset+1)%3];
 
 // Slowly drift to the new target color 1 at a time until
 // it has been reached
 while(s[0]!=t[0] || s[1]!=t[1] || s[2] != t[2]) {
   for (i = 0; i<3; i++) {
     if (s[i] > t[i]) {
       s[i] = s[i] - 1;
     } else if (s[i] < t[i]) {
       s[i] = s[i] + 1;
     }
     analogWrite(REDLED, s[0]);
     analogWrite(BLUELED, s[1]);
     analogWrite(GREENLED, s[2]);
     delay(10);
   }
 }
 analogWrite(REDLED, s[0]);
 analogWrite(BLUELED, s[1]);
 analogWrite(GREENLED, s[2]);
 // Let the viewer enjoy the new color before
 // selecting the next target color.
 delay(2000);
}

Thomas Jespersen

Mystisk at min kode ikke virker... Nå, men godt at du har fundet et eksempel der virker :)
Need any help with your electronics projects?
TKJ Electronics, a consultancy company located in Denmark (Europe), has the required ex

Brian Pedersen

har sat og leget lidt med den kode jeg har fundet, ville det være muligt at skydt et potmeter ind, til at justere hastigheden den skifter farve med ?


Thomas Jespersen

#9
May 02, 2010, 10:34 pm Last Edit: May 02, 2010, 10:35 pm by tkj Reason: 1
Ja sagtens...
Smæk et potentiometer på Analog Pin 0 og erstat den sidste kommando "delay(2000);" med denne
Code: [Select]
delay(analogRead(0));
Need any help with your electronics projects?
TKJ Electronics, a consultancy company located in Denmark (Europe), has the required ex

Go Up