Help with programming an RGB LED and 4 pushbuttons at an analog input

As a beginner I am playing at my arduino leonardo trying to use 4 pushbuttons at an analog input and make the whole thing operate an RGB LED. The functions i want to perform are the following:

Button 1: Turn ON red LED and after a delay turn it off Button 2: Turn ON green LED and after a delay turn it off Button 3: Turn ON blue LED and after a delay turn it off Button 4: Turn ON all LEDs and after a delay turn them off

I use a common anode RGB LED which means that the longest wire at the LED goes to +5 Volts and the other 3 pins via a resistor go to digital pins at the arduino. The setup for the pushbuttons is the following

I use four pushbuttons with a 10k pull down resistor and at the pushbuttons i use 5K, 4K, 3K and 2K resistors.

My problem is that the LED is either flickering on running the code or the Blue color is ON. when i press the pushbuttons i see the operations i programmed but the colors are mixed.

Any help is appreciated since i have a steep learning curve.

const int analogPin = A0; const int Red = 8; const int Green = 9; const int Blue = 10; int LED;

void setup(){ pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(Blue, OUTPUT); } void loop() { digitalWrite(Red, LOW); digitalWrite(Green,LOW); digitalWrite(Blue,LOW);

LED = map(analogRead(analogPin), 0, 1023, 0, 3);

switch (LED){

case 0: digitalWrite(Red, HIGH); delay (100); digitalWrite(Red,LOW); break;

case 1: digitalWrite(Green, HIGH); delay (100); digitalWrite(Green,LOW); break;

case 2: digitalWrite(Blue, HIGH); delay (100); digitalWrite(Blue,LOW); break;

case 3: digitalWrite(Red, HIGH); digitalWrite(Green, HIGH); digitalWrite(Blue, HIGH); delay (100); digitalWrite(Red, LOW); digitalWrite(Green, LOW); digitalWrite(Blue, LOW); break;

} }

Hi, your descriptions of the colours you see and what happens when you press buttons is a little vague, but perhaps I can help.

The resistors connected to your buttons and the 10K pulldown together form a "potential divider". When the button with the 5K resistor is pressed, the value returned by analogRead will be around 1023 x (10 / (10 +5)) so around 682. The second button will give around 730. The other resistors will give higher values. The map function will turn anything between around 512 to 768 into 2, and anything above 768 into 3. So I would predict you should see red when no buttons are pressed, blue when buttons 1 or 2 are pressed and white for the other 2 buttons.

Paul