Controlling Leds with buttons

Hi I am making a very simple project a prototype of a project I have in mind, one of my problems is I am not very smart. I want to turn on leds one at a time when a button is pressed. and have a second button to clear them. With all the tutorials i can find I can not accomplish this can someone help. here is my code so far, I would like to have 8 leds total once its working right. any help would be huge.

const int button1 = 9;
const int button2 =10;
const int kLed1 = 2;
const int kLed2 = 4;
int i = 0;

void setup() {
// put your setup code here, to run once:
pinMode(button1, INPUT);
digitalWrite(button1, HIGH);
pinMode(kLed1, OUTPUT);
pinMode(kLed2, OUTPUT);
pinMode(button2, INPUT);
digitalWrite(button2, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(button1) == LOW){
  digitalWrite(kLed1, HIGH);
}
else if(digitalRead(button2) == LOW){
  digitalWrite(kLed1, LOW);
  digitalWrite(kLed2, LOW);
}
}

Have you thought about how much time should elapse between one led being switched on and the next ?

OOPS! :blush:

6v6gt:
Have you thought about how much time should elapse between one led being switched on and the next ?

time between them turning on does not matter, I want to make a simple control panel with the LED's for tracking turn sequence in the table top game Arkham Horror LCG. I have one that uses battery and switches no arduino works great but I want to make a more permanent one with one button to light up the leds one at a time, each time its pressed and then have another button to switch them all off a reset button. any help is greatly apreciated. I have mental problems and forget easily, I know this is basic stuff but cant for the life of me remember the basics.

Is this more or less what you hope to achieve? :

Initial state:
All leds off.

first main button press:
led 1 switches on and stays on

second main button press:
led 2 swiches on and stays on
led 1 stays on

third main button press:
led 2 stays on
led 1 stays on
(ie no action )

etc.

reset button press:
all leds are switched off

moJoeRedRog:
I am not very smart.

Well, if you want to convince us of that, you have made a great start.

  1. This is the tutorial section. The thread at the top of the section says "PLEASE DO NOT POST YOUR PROBLEMS HERE".

  2. There is a section called "LEDs and multiplexing". Does that not sound more appropriate?

  3. Each forum section has a sticky post that tells you how to post code properly, and other important things you need to know. You clearly didn't read that.

I suggest you click "report to moderator" and ask for your thread to be moved to a more appropriate section. Then read the sticky post, find out how to use code tags and edit your post to fix it.

Yes , Button 1 turns on the led's 1 at a time button 2 resets the led's. So press button 1 Led 1 comes on press button 1 led 2 comes on.. press button 1 led 8 comes on,

then press button 2 and all led's turn off

then start again button 1 turns them on one at a time and they stay on until button 2 is pressed.

ok, before someone writes it for you, how about you start with a counter that counts from 0-7 each time button 1 is pressed...

Then add a simple feature to reset the count to zero when button 2 is pressed.

To save on worrying about LEDs and resistors for now, use Serial.print() to display the counter value as it changes, then after it’s all working, add the code to control the LEDs.

You may like to post a diagram of how your buttons and LEDs are connected to the Arduino - as that will help us to help you.

ok great thanks for the direction, I will try that

Just a hint...
Test when the buttons are pressed, not just down, otherwise you’ll soon discover how fast the Arduino can count... very!

The term to look out for is state-change.

Threads merged.

PaulRB:
Well, if you want to convince us of that, you have made a great start.

  1. This is the tutorial section. The thread at the top of the section says "PLEASE DO NOT POST YOUR PROBLEMS HERE".

  2. There is a section called "LEDs and multiplexing". Does that not sound more appropriate?

  3. Each forum section has a sticky post that tells you how to post code properly, and other important things you need to know. You clearly didn't read that.

I suggest you click "report to moderator" and ask for your thread to be moved to a more appropriate section. Then read the sticky post, find out how to use code tags and edit your post to fix it.

Ya I know I posted in the wrong forum but hey now there merge so all is forgiven right:) I wanted to delete the post but was un able due to the software of the forum.

Progress has been made. I used the tutorial on counters and have multiplied the code to use 2 buttons, my serial monitor logs each press of the two buttons. I dont know how to make the buttons activate the leds and how to use the second button to clear the leds. Any help is much appreciated. heres the modified code.

// this constant won't change:
const int  buttonPin = 6;    // the pin that the pushbutton is attached to
const int ledPin = 8;       // the pin that the LED is attached to
const int buttonPin2 = 7;
const int ledPin2 = 5;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonPushCounter2 = 0;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int lastButtonState2 = 0;
int buttonState2 = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


 
  if (buttonPushCounter  >= 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  buttonState2 = digitalRead(buttonPin2);
 if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("number of 2nd button pushes: ");
      Serial.println(buttonPushCounter2);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
    lastButtonState2 = buttonState2;
  }



void resetLights(){
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
}

This is an old sketch that does roughly what you need, with a few minor changes you can get it to do what you want. Try to understand how it works in its current form first. Focus mainly on the setup and the loop, leave the class for later, it just handles the button state detection and debouncing.
If you have any questions, feel free to ask.

Code:


---




```
class PushButton
{
public:
PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
: pincolor=#000000[/color] { // remember the push button pin
pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressedcolor=#000000[/color] // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state = digitalReadcolor=#000000[/color];               // read the button's state
int8_t stateChange = state - previousState;  // calculate the state change since last time

if (stateChange == falling) { // If the button is pressed (went from high to low)
       if (milliscolor=#000000[/color] - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
         pressed = true; // the button is pressed
       }
     }
     if (stateChange == rising) { // if the button is released or bounces
       previousBounceTime = milliscolor=#000000[/color]; // remember when this happened
     }

previousState = state; // remember the current state
     return pressed; // return true if the button was pressed and didn't bounce
   };
 private:
   uint8_t pin;
   bool previousState = HIGH;
   unsigned long previousBounceTime = 0;

const static unsigned long debounceTime = 25;
   const static int8_t rising = HIGH - LOW;
   const static int8_t falling = LOW - HIGH;
};

const uint8_t ledPins[] = {12, 11, 10, 9};
const uint8_t nb_leds = sizeofcolor=#000000[/color] / sizeofcolor=#000000[/color];
PushButton pushbutton = {2};

void setupcolor=#000000[/color] {
 for (const uint8_t &ledPin : ledPins)
   pinMode(ledPin, OUTPUT);
}

void loopcolor=#000000[/color] {
 static uint8_t nb_presses = 0;
 if color=#000000[/color] {
   if (nb_presses == nb_leds) {
     for (const uint8_t &ledPin : ledPins)
       digitalWrite(ledPin, LOW);
     nb_presses = 0;
   } else {
     digitalWrite(ledPins[nb_presses], HIGH);
     nb_presses++;
   }
 }
}
```

|

Pieter

That old code did the trick with two bulbs im going to try and get up to 8 one thing that it does undesirably is if you press the first button when the two are lit they shut off, but still its doing the job. Ty!