Led+switches

http://postimg.org/image/890lqgdcv/

Hello, i'm trying to control 4 leds with 2 buttons. Every cilck of the right button turn on next led and the switch on the left turning of the leds.

Presumably you're asking a question? :slight_smile:

Have a look at this and see how to detect button presses, then you should be able to use the count to do what you need.

I' made it just like in the example but i don't know how to do to turn on one more led with every push of the 1st button (4 led max) and opposite with the 2nd button.

const int buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int buttonPin2 = 3;
const int ledPin1 = 4; 
const int ledPin2 = 5;
const int ledPin3 = 6;
const int ledPin4 = 7;

int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button

int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button


void setup() {
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT);
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(ledPin3, OUTPUT);
   pinMode(ledPin4, OUTPUT);
}

void loop() 
{
 buttonState1 = digitalRead(buttonPin1);
 
if (buttonState1 != lastButtonState1) {
 
 if (buttonState1 == HIGH) {
   buttonPushCounter1++;
   } 
   
   else {
     Serial.println("off");
   }
 }
 lastButtonState1 = buttonState1;
 
 buttonState2 = digitalRead(buttonPin2);
 
if (buttonState2 != lastButtonState2) {
 
 if (buttonState1 == HIGH) {
   buttonPushCounter2++;
   } 
   else {
     Serial.println("off");
   }
 }
 lastButtonState2 = buttonState2;
 
}

Each time the up button is pushed, increment a counter.
Ever time the down button is pushed decrement the counter.
Use switch statement to turn on/off LEDs at certain counter values.
Use the default case to maintain the upper count LED state.

what's wrong and how to do maintain in default? :frowning:

int buttonPin1 = 2;    // the pin that the pushbutton is attached to
int buttonPin2 = 3;
int ledPin1 = 4; 
int ledPin2 = 5;
int ledPin3 = 6;
int ledPin4 = 7;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button

int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button


void setup() {
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
}

void loop() 
{
  buttonState1 = digitalRead(buttonPin1);
  lastButtonState1 = buttonState1;
  buttonState2 = digitalRead(buttonPin2);
  lastButtonState2 = buttonState2;
  
  if (buttonState1 != lastButtonState1) {
  
  if (buttonState1 == HIGH) {
    buttonPushCounter++;
    } 
    else if (buttonState2 != lastButtonState2) {
    
  if (buttonState2 == HIGH) {
    buttonPushCounter--;
    } 
    
    else {
    }

  switch (buttonPushCounter){
    
    case buttonPushCounter == 1 :
    pinMode(ledPin1, HIGH);
    pinMode(ledPin2, LOW);
    pinMode(ledPin3, LOW);
    pinMode(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 2 :
    pinMode(ledPin1, HIGH);
    pinMode(ledPin2, HIGH);
    pinMode(ledPin3, LOW);
    pinMode(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 3 :
    pinMode(ledPin1, HIGH);
    pinMode(ledPin2, HIGH)
    pinMode(ledPin3, HIGH);
    pinMode(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 4 :
    pinMode(ledPin1, HIGH);
    pinMode(ledPin2, HIGH);
    pinMode(ledPin3, HIGH);
    pinMode(ledPin4, HIGH);
    break;
  }

You use pinMode once in setup to make the LED pins OUTPUT.
Don't use HIGH or LOW with pinMode.

In your case statements, you do:
digitlaWrite( yourPin, HIGH/LOW); // turns ON/OFF a particular output pin.

ok but I still have this error 'buttonPushCounter' cannot appear in a constant-expression

int buttonPin1 = 2;    // the pin that the pushbutton is attached to
int buttonPin2 = 3;
int ledPin1 = 4; 
int ledPin2 = 5;
int ledPin3 = 6;
int ledPin4 = 7;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button

int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button


void setup() {
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    digitalWrite(ledPin1, OUTPUT);
    digitalWrite(ledPin2, OUTPUT);
    digitalWrite(ledPin3, OUTPUT);
    digitalWrite(ledPin4, OUTPUT);
}

void loop() 
{
  buttonState1 = digitalRead(buttonPin1);
  lastButtonState1 = buttonState1;
  buttonState2 = digitalRead(buttonPin2);
  lastButtonState2 = buttonState2;
  
  if (buttonState1 != lastButtonState1) {
  
  if (buttonState1 == HIGH) {
    buttonPushCounter++;
    } 
    else if (buttonState2 != lastButtonState2) {
    
  if (buttonState2 == HIGH) {
    buttonPushCounter--;
    } 
    
    else {
    }

  switch (buttonPushCounter){
    
    case buttonPushCounter == 1 :
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 2 :
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 3 :
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH)
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    break;
    
     case buttonPushCounter == 4 :
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    break;
  }

case buttonPushCounter == 1 :
Should be:
case 1:

case buttonPushCounter == 2 :
Should be:
case 2:
.
.
.

default:
//put code here to handle counts which are not 1,2, or 3
//for example turn on an overfow LED

}

Couple of things.

Get into the habit of wiring push-buttons to ground and using INPUT_PULLUP as the mode instead of using pull-up (or pull-down) resistors.

Second, you need proper de-bouncing. Presently, your de-bouncing is by good luck using for example, the delay inherent in the serial.write. With faster code, you will find it behaving erratically.

Finally, you have not properly outlined the function you desire. I gather it is that each "right" button press lights one more LED, and each "left" button press lights one less LED, with a corresponding maximum and minimum (or else it could loop around so that after all are lit, the next press turns all off and so on).

You really should explain it fully. I may run up a version of my standard "button" sketch later if I have a chance. :grinning: