Project works USB powered, fails adapter powered

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 :wink:

Anyone knows why the Arduino does not remember the variable setting while adapter powered?

Anyone knows why the Arduino does not remember the variable setting while adapter powered?

Yes. It most certainly does.

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.

Which button?

but doesn't stay on like it should (and did USB powered)

Does the light come on for some time, then go off? Does it not come on at all? Does it just blink and go off? Does anything on the board get warm?

Are you using the proper external current-limiting resistors with the LEDs? What kind of LEDs are the?

I know there is a lot to be improved in the code, but i wanted to test this code a bit first

Actually, there isn't. The code is well written, and easily understandable.

The only "improvement" I'd recommend is a switch statement in place of the if/else if statements in colorHandler().

Thank you for your reply PaulS,

The button I mentioned is one of the three buttons on the breadboard. These buttons are wired as shown on the official Arduino tutorials site.

  • If I push the button on the breadboard for the white LED's the lights stay on. As soon as I release the button the lights go out. It doesnt matter how long I keep the button 'on'. The white light, when turned on, flickers a little bit. They never go totally off but seem to illumnate a little less. That doesnt happen when it is USB powered.
  • If I push the RGB button on the breadboard the RGB LED goes on, if I release it turns off. As you have seen in my code there are 6 colors programmed, but when I push the button it always starts with the first color.
  • The third button is an OFF button, but thats not relevant at the moment.

The LED's are:

  • White = 5mm, 3.4V, 20mA connected with 100 Ohm resistor.
  • RGB = 5mm, 2.0V, 3.2V, 3.2V, (cant find the)mA connected with 2x100 ohm and 180 Ohm resistor.

In the time that I am writing this reply (approx 10 min) nothing on the board became hot, so that should be fine I guess.

Code improvements I want to do is get rid of the delay() thing by making the buttons a onRelease button. I found a tutorial for that, but didnt implement it yet. :sunglasses:

I hope you can think of something I did wrong here :slight_smile:

The RGB switch is being debounced (the delay() does this). The White switch is not.

The flickering and lower intensity implies that the power supply you have is under-powered.

When you have the arduino powered from the external power supply, What is the voltage reading between the arduino 5v and ground pins? Does this drop when you push your buttons and the program runs?

It turns out to be a harware fault.

The wire bridge to the ground of the 'OFF' button was wired incorrect. This made the off button turned on all the time... ::slight_smile:

Thank you for helping me guys! :slight_smile: