LED RGB with button

After each button press I want the LED's to go on on 1 by 1, so the sequence would be something like this.

1-2-3-1-2-3

I've already created the first part, but I don't really know yet how to go about making this.

const int PIN_LED_RED = 4;
const int PIN_LED_GREEN = 5;
const int PIN_LED_BLUE = 6;
const int PIN_BUTTON_1 = 8;
const int PIN_BUTTON_2 = 9;

int buttonState_1 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_LED_RED, OUTPUT);
  pinMode(PIN_LED_GREEN, OUTPUT);
  pinMode(PIN_LED_BLUE, OUTPUT);
  pinMode(PIN_BUTTON_1, INPUT_PULLUP);
  pinMode(PIN_BUTTON_2, INPUT_PULLUP);
}

void loop() {
  buttonState_1 = digitalRead(PIN_BUTTON_1);

  if (buttonState_1 == LOW)
  {
    digitalWrite(PIN_LED_RED, HIGH);
    digitalWrite(PIN_LED_GREEN, LOW);
  }
}

You need to increment a variable ( say button_count) with each press, along the lines.. button_count++

To detect the button have a google for state change to detect individual button presses

Then light the led according to the count value . You might also need to debounce the button

Google is your friend !

This will do everything but turn on the LEDs:

StateChangeDetection

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.