// File: LedRGB.cpp // Control of an RGB ledstrip // For Arduino Ethernet // Version 2014-10-05 #include "globals.h" byte PwmR, PwmG, PwmB, PwmW, PwmY, PwmP, PwmC; enum Colors {red = 0, green, blue, white, yellow, purple, cyan}; // values 0..6 //******************************************************************************************************** // PWM modulator called by DoRGB() void DoPWM (void) { static byte PwmCounter, PwmPause; if (PwmCounter == 0) { PORTC &= ~0x07; // all off if (--PwmPause == 0) { PwmPause = 200; PwmCounter = 255; // again } } else { PwmCounter --; if (PwmR > PwmCounter) PORTC |= 0x01; // red if (PwmG > PwmCounter) PORTC |= 0x02; // green if (PwmB > PwmCounter) PORTC |= 0x04; // blue if (PwmW > PwmCounter) PORTC |= 0x07; // all 3 if (PwmY > PwmCounter) PORTC |= 0x03; // green + red = yellow if (PwmP > PwmCounter) PORTC |= 0x05; // blue + red = purple if (PwmC > PwmCounter) PORTC |= 0x06; // blue + green = cyan } } //******************************************************************************************************** byte MLS8 (void) // Maximum Length Sequence pseudo random generator { static byte mls = 0xFF; // startcondition != 0 byte x; // 8 bit MLS feedback is exor of bits 0001 1101 x = ((mls & 0x10) > 0) ^ ((mls & 0x08) > 0) ^ ((mls & 0x04) > 0) ^ ((mls & 0x01) > 0); mls = mls >> 1; // shift 1 right if (x > 0) mls += 0x80; // feedback as MSB return mls; } //******************************************************************************************************** // color sequencer called at 66 kHz by ISR(TIMER2_COMPA_vect) in OneWire.cpp void DoRGB (void) { static byte Pwm; static byte WaitTime; static Colors UpgoingColor = red, DowngoingColor = green; static int RGBCounter; DoPWM(); if(RGBCounter > 0) {RGBCounter--; return;} else RGBCounter = 100; if (WaitTime > 0) {WaitTime --; return;} // wait some time at maximum if (Pwm < 255) Pwm++; else { Pwm = 0; // on peak select new color for upgoing DowngoingColor = UpgoingColor; do UpgoingColor = Colors(MLS8() & 0x07); // values 0..6 // at same color as before or color 7 chose a new random color while ((UpgoingColor == DowngoingColor) | (UpgoingColor == 7)); WaitTime = 100; // new pause } PwmR = PwmG = PwmB = PwmW = PwmY = PwmP = PwmC = 0; // all off switch (UpgoingColor) { case red: PwmR = Pwm; break; case green: PwmG = Pwm; break; case blue: PwmB = Pwm; break; case white: PwmW = Pwm; break; case yellow: PwmY = Pwm; break; case purple: PwmP = Pwm; break; case cyan: PwmC = Pwm; break; } switch (DowngoingColor) { case red: PwmR = 255 - Pwm; break; case green: PwmG = 255 - Pwm; break; case blue: PwmB = 255 - Pwm; break; case white: PwmW = 255 - Pwm; break; case yellow: PwmY = 255 - Pwm; break; case purple: PwmP = 255 - Pwm; break; case cyan: PwmC = 255 - Pwm; break; } }