Programming help

hi,

i want , that when i press the button, the next led start to be bright. there are 3 led’s.
when the third led isn’t bright, the first led start to be bright.
nothing happends wehen i press the button with the code.

here is my code :


int led = 13;
int button = 10;
void setup() {
pinMode(led,OUTPUT);

}

void loop() {
digitalWrite(led,HIGH);
if (digitalRead(button) == HIGH)
{
led = led - 1;
}

if (led < 11)
{
led = 13;
}

}


i hope you can help me :slight_smile:

Well, start by adding some IO pins to your sketch. Wire the button so when pressed, the pin connects to Gnd, and use the internal pullup resistor:

pinMode (button, INPUT_PULLUP);

and keep track of which LED is on. Say you used outputs pins 2,3,4 for the LEDs:

if (digitalRead (button) == LOW){ // button is pressed?
  digitalWrite (LEDcount, LOW); // turn off current LED
  LEDcount = LEDcount +1;
    if (LEDcount == 5){
    LEDcount = 2;  // reset to first one
    }
  digitalWrite (LEDcount, HIGH); // turn on the next one
  delay(100); // limits changes to 10/second
}