0

I want to make sequential LED by using arduino that controlled by button. If I press + button, LED will increase from L1 to (L1 and L2 is HIGH), then I press button again, L1, L2, and L3 will be HIGH. If I press again, all the LED (L1, L2, L3, L4) will be high. Same case if I press “ – “ button, LED will be lower from the previous state. Which mean that if L1, L2, and L3 is HIGH. There will be L1 and L2 only HIGH if I press ‘ – ‘ button. I dont how to do it. This is code for increase but I dont know how to make it reverse flow.

const int buttonPin = 2; // the pin number of the pushbutton input pin  
int ledPins[] = {3, 4, 5,};  
 // variables will change:  
 int buttonState = 0; // variable for reading the pushbutton status  
 int buttonPressCount = 0;  
 int numberOfLED = 3;  
 void setup() {  
  // initialize the LED pin as an output:  
  for (int i = 0; i < numberOfLED; i++) {  
   pinMode(ledPins[i], OUTPUT);  
  }  
  pinMode(buttonPin, INPUT);  
 }  
 void loop() {  
  // read the state of the pushbutton value:  
  buttonState = digitalRead(buttonPin);  
  // check if the pushbutton is pressed.  
  // if it is, the buttonState is HIGH:  
  if (buttonState == HIGH) {  
   for (int i = 0; i < numberOfLED; i++) {  
    if (buttonPressCount % numberOfLED == i) {  
     // turn LED on:  
     digitalWrite(ledPins[i], HIGH);  
    } else {  
     // turn LED off:  
     digitalWrite(ledPins[i], LOW);  
    }  
   }  
   buttonPressCount++;  
   delay(400);  
  }  
 }  

closed as too broad by KIIV, Enric Blanco, Code Gorilla, Greenonline, SDsolar Jun 1 '17 at 7:16

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

My original answer was probably over-kill....Just keep track of how many leds are lit:

int ledPins[] = {3, 4, 5,};
const int num_leds = sizeof(ledPins) / sizeof(ledPins[0]);  // 3 in this example
byte num_leds_lit = 0

void set_leds() {
    for (int i = 0; i < num_leds; i++) {
        digitalWrite(ledPins[i], i < num_leds_lit ? HIGH : LOW);
    }
}

Then, on button press, increase the count:

if (num_leds_lit < num_leds) {
    num_leds_lit += 1;
    set_leds();
}

Then to turn the leds off with the other button press:

if (num_leds_lit > 0) {
    num_leds_lit -= 1;
    set_leds();
}

------------------------- Original answer

There are a few ways to do it. One way would be to use a bitmask. I would write a function to turn the leds on/off

// Each bit represents the state of one LED (0 == off, 1 == on)
byte led_bits = 0;
int ledPins[] = {3, 4, 5,};
const int num_leds = sizeof(ledPins) / sizeof(ledPins[0]);  // 3 in this example
// Need a mask to make sure we don't set too many bits
const byte led_mask = 0xFF >> 8 - num_leds;   // 00000111B in this case

void set_leds() {
    for (int i = 0; i < num_leds; i++) {
        // Turn on/off based on state of bit
        digitalWrite(ledPins[i], (1 << i) & led_bits ? HIGH : LOW);
    }
}

Then, on button press, set the mask:

led_bits << = 1;          // Shift left (001 -> 010)
led_bits |= 1;            // Set lsb    (010 -> 011)
led_bits &= led_mask;     // Ensure max bits: (1111 -> 0111)
set_leds();

Then to turn the leds off with the other button press:

led_bits >>= 1;          // Shift right (011 -> 001)
set_leds();

Not the answer you're looking for? Browse other questions tagged or ask your own question.