As a first project with an Arduino board I am tinkering with a Duemilanove and some LEDS.
I am trying to make a RGB 'mood' light and white 'reading' light for in the bedroom. I programmed the board and tested, USB powered, and it works like a charm!
Then I wanted to try it in he bedroom, so I took an adapter (3v-12v, 1000mA) , set it to 9v and plugged it the Arduino board.
Now when I press the button the light turns on, but doesn't stay on like it should (and did USB powered) so it looks like the adruino doesnt remember the variable or something.
Here is my code:
// constant variables won't change.
const int btnPinRGB = 2; // the number of the 1st pushbutton pin
const int btnPinWhite = 3; // the number of the 2nd pushbutton pin
const int btnPinOff = 4; // the number of the 3rd pushbutton pin
const int ledPinWhite = 8; // the number of the White LED pin Esther side of the bed
const int ledPinRed = 9; // the number of the Blue LED pin
const int ledPinGreen = 10; // the number of the Red LED pin
const int ledPinBlue = 11; // the number of the Green LED pin
// variables will change:
int buttonStateRGB = 0; //variabelen on de btnstate in af te vangen
int buttonStateWhite = 0;
int buttonStateOff = 0;
int colorCode = 0; // RGB led aan/uit indicator + nummer voor kleurcode.
int colorStateWhite = 0; // White led aan/uit indicator
//int lastButtonStateRGB = 0;
//int lastButtonStateWhite = 0;
void setup()
{
pinMode(btnPinRGB, INPUT); // initializing the Btn pins as input
pinMode(btnPinWhite, INPUT);
pinMode(btnPinOff, INPUT);
pinMode(ledPinWhite,OUTPUT); // initializing the White LED pin as output
pinMode(ledPinRed, OUTPUT); // initializing the RGB LED pins as output
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
}
void loop()
{
// read buttonstate and put in var.
buttonStateRGB = digitalRead(btnPinRGB);
buttonStateWhite = digitalRead(btnPinWhite);
buttonStateOff = digitalRead(btnPinOff);
if (buttonStateRGB == HIGH)
{
colorCode = colorCode + 1;
delay(500);
colorHandler(colorCode);
}
if (buttonStateWhite == HIGH)
{
colorStateWhite = 1;
}
if (buttonStateOff == HIGH)
{
colorCode = 0;
colorHandler(colorCode);
colorStateWhite = 0;
}
// loop the colorcodes
if (colorCode == 8)
{
colorCode = 1;
}
if (colorStateWhite == 1)
{
digitalWrite(ledPinWhite ,HIGH);
}
else if (colorStateWhite == 0)
{
digitalWrite(ledPinWhite ,LOW);
}
}
void colorHandler(int colorCode)
{
if (colorCode == 0) // UIT
{
digitalWrite(ledPinRed ,LOW);
digitalWrite(ledPinGreen ,LOW);
digitalWrite(ledPinBlue ,LOW);
}
else if (colorCode == 1) // ROOD
{
digitalWrite(ledPinRed ,HIGH);
digitalWrite(ledPinGreen ,LOW);
digitalWrite(ledPinBlue ,LOW);
}
else if (colorCode == 2) // GROEN
{
digitalWrite(ledPinRed ,LOW);
digitalWrite(ledPinGreen ,HIGH);
digitalWrite(ledPinBlue ,LOW);
}
else if (colorCode == 3) // BLAUW
{
digitalWrite(ledPinRed ,LOW);
digitalWrite(ledPinGreen ,LOW);
digitalWrite(ledPinBlue ,HIGH);
}
else if (colorCode == 4) // PAARS
{
analogWrite(ledPinRed ,150);
analogWrite(ledPinGreen ,20);
analogWrite(ledPinBlue ,240);
}
else if (colorCode == 5) // PAARS 2
{
analogWrite(ledPinRed ,180);
analogWrite(ledPinGreen ,40);
analogWrite(ledPinBlue ,200);
}
else if (colorCode == 6) // PAARS 3
{
analogWrite(ledPinRed ,180);
analogWrite(ledPinGreen ,60);
analogWrite(ledPinBlue ,200);
}
}
(sorry for the dutch words in the code, but those are not important parts)
I know there is a lot to be improved in the code, but i wanted to test this code a bit first
Anyone knows why the Arduino does not remember the variable setting while adapter powered?