RGB strings always have some disbalance in colors, so we never have pure white light. That's why we need adjustable dimmer with memory, for set nedeed light temperature for photo or work.
Code
/*
LED's with common anode
BUTTONENT=Select Green
BUTTONUP=Select Blue
BUTTONDOWN=Select Red
On enter selected color light for 1 sec
Up/Down = +/- brightness selected color
Enter again save settings and light off for 0.5sec
*/
#include <EEPROM.h>
const int BLED = 3; //output for blue led, also address for coincident PWM value
const int GLED = 5; //output for green led, also address for coincident PWM value
const int RLED=6; //output for red led, also address for coincident PWM value
const int BUTTONENT=7;
const int BUTTONUP=8;
const int BUTTONDOWN=2;
byte PWMB;
byte PWMG;
byte PWMR;
void setup()//
{
pinMode(BLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
pinMode(BUTTONENT, INPUT_PULLUP);
pinMode(BUTTONDOWN, INPUT_PULLUP);
pinMode(BUTTONUP, INPUT_PULLUP);
PWMB = EEPROM.read(BLED);
PWMG = EEPROM.read(GLED);
PWMR = EEPROM.read(RLED);
analogWrite(BLED, PWMB);
analogWrite(RLED, PWMR);
analogWrite(GLED, PWMG);
}
void adjust (int color) //function for adjusment
{
analogWrite(BLED, PWMB);
analogWrite(RLED, PWMR);
analogWrite(GLED, PWMG);
if (color==BLED)
{
while (digitalRead(BUTTONENT)==HIGH)
{
if (digitalRead(BUTTONUP)==LOW)
{
analogWrite(BLED, PWMB=PWMB-10);
EEPROM.write(BLED, PWMB);
delay(300);
}
if (digitalRead(BUTTONDOWN)==LOW)
{
analogWrite(BLED, PWMB=PWMB+10);
EEPROM.write(BLED, PWMB);
delay(300);
}
}
}
if (color==GLED)
{
while (digitalRead(BUTTONENT)==HIGH)
{
if (digitalRead(BUTTONUP)==LOW)
{
analogWrite(GLED, PWMG=PWMG-10);
EEPROM.write(GLED, PWMG);
delay(300);
}
if (digitalRead(BUTTONDOWN)==LOW)
{
analogWrite(GLED, PWMG=PWMG+10);
EEPROM.write(GLED, PWMG);
delay(300);
}
}
}
if (color==RLED)
{
while (digitalRead(BUTTONENT)==HIGH)
{
if (digitalRead(BUTTONUP)==LOW)
{
analogWrite(RLED, PWMR=PWMR-10);
EEPROM.write(RLED, PWMR);
delay(300);
}
if (digitalRead(BUTTONDOWN)==LOW)
{
analogWrite(RLED, PWMR=PWMR+10);
EEPROM.write(RLED, PWMR);
delay(300);
}
}
}
digitalWrite(BLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, HIGH);
delay (500);
analogWrite(BLED, PWMB);
analogWrite(RLED, PWMR);
analogWrite(GLED, PWMG);
return;
}
void loop() // the loop function runs over and over again forever
{
if (digitalRead(BUTTONUP)==LOW)
{
digitalWrite(BLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, HIGH);
delay (1000);
adjust (BLED);
}
if (digitalRead (BUTTONENT)==LOW) //кнопка enter (средняя)
{
digitalWrite(BLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(RLED, HIGH);
delay (1000);
adjust (GLED);
}
if (digitalRead (BUTTONDOWN)==LOW)
{
digitalWrite(BLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
delay (1000);
adjust (RLED);
}
}
Comments