Push button if statement

I am trying to create a program that can accomplish this:

  • I want to have two push buttons that accomplish different things
  • If I press the first button once, an RGB LED turns GREEN
  • If I press the first button twice, the RGB LED turns BLUE
  • The second pushbutton is a normal LED that turns one colour

Here is my code:

int TestSwitchPin = 2; int TestSwitchPin2= 9; int RGBledpin5 = 5; int RGBledpin6 = 6; int RGBledpin7 = 7; int ledpin8 = 8; void setup() { pinMode(TestSwitchPin, INPUT_PULLUP); pinMode(TestSwitchPin2, INPUT_PULLUP); pinMode(RGBledpin5, OUTPUT); pinMode(RGBledpin6, OUTPUT); pinMode(RGBledpin7, OUTPUT); pinMode(ledpin8, OUTPUT); //pinMode(txPin, OUTPUT); }

void loop() { if(digitalRead(TestSwitchPin) == LOW) { digitalWrite(RGBledpin5, HIGH); digitalWrite(ledpin8, LOW); if (digitalRead(TestSwitchPin) == LOW) { digitalWrite(RGBledpin6, HIGH); digitalWrite(ledpin8, LOW); }

} if(digitalRead(TestSwitchPin2) == LOW) { digitalWrite(ledpin8, HIGH); digitalWrite(RGBledpin5, LOW);

} }

My problem is that if I press the first pushbutton, the rgb turns green, if I press it twice, it turns Bluish-Green

void loop()
{
    if(digitalRead(TestSwitchPin) == LOW)
    {
   digitalWrite(RGBledpin5, HIGH);
   digitalWrite(ledpin8, LOW);
   if (digitalRead(TestSwitchPin) == LOW)   // if it was LOW a few microseconds ago when you last looked then what are the chances that it has changes since then?
   {
      digitalWrite(RGBledpin6, HIGH);
   digitalWrite(ledpin8, LOW);
   }

Yes, of course it turns blue-green. If the button is pressed then the first if is true and turn on the blue side. Then the second if is also true and turns on the green. So now you have blue-green.

Did you want to wait for the pin to go LOW and then HIGH again so you know it has been released? Think a state machine then.

Take a look at the state change detection example. It certainly seems to me that you care when the switch BECOMES pressed, not when the switch IS pressed.