Six leds lights up sequence and back by pressing buttonup/down 5 times.

Hello fellow forumers,

I will start with the question first. How do I use the 'if' statement correctly to get accomplished what I want and what more statements do I need to use if needed?

What I want accomplish is that the the leds burn in sequance by pressing a button for up. And back by pressing a button for down.

foto of the setup.

I want to make the leds burn from pin7 to pin2 bij pushing the buttonup (pin8).
At each moment there is only one led lit.
The next thing is that I want to make the leds burn from pin2 to pin7 by pushing buttondown(pin9).

The approach I use is taking this code.

const int buttonUp    = 9;
const int buttonDown  = 8;
const int ledPin1     = 7;
const int ledPin2     = 6;
const int ledPin3     = 5;
const int ledPin4     = 4;
const int ledPin5     = 3;
const int ledPin6     = 2;

int buttonPushCounter1 = 0;
int buttonState1 = 0;
int lastButtonState = 0;
int buttonState2 = (buttonState - 1);

void setup() {
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  pinMode (ledPin4, OUTPUT);
  pinMode (ledPin5, OUTPUT);
  pinMode (ledPin6, OUTPUT);
  pinMode(buttonUp,  INPUT);
  pinMode(buttonDown,INPUT);
  Serial.begin(9600);  
}


void loop(){
  buttonState1 = digitalRead(buttonUp);
  
  if (buttonState1 != lastButtonState) {
    if (buttonState1 == HIGH) {
      buttonPushCounter1++;
      if(buttonState2 == HIGH){
        buttonPushCounter1--;
        Serial.println("on");
        Serial.print("number of button pushes:  ");
        Serial.println(buttonPushCounter1);
}
else {
  Serial.println("off");
}
    }

Am I on the right track or is there a easier way to do this?

Kind regards and sorry for the pore grammer:(

James

Probably you can make it more efficient. I am on tablet and don't have the compiler. I made this in notepad ... there might some typo errors but you get the picture ...

boolean changeLed = FALSE;
unsigned int ledCounter = 0; 

void loop(){

if (buttonUpPressed ()==0 ){
  if (ledCounter < 5) {
    ledCounter++ ;
    changeLed = TRUE;
  }
}

If (button2Pressed()==0){
  if (ledCounter > 1) {
    ledCounter-- ;
    changeLed = TRUE;
  }
}

if (changeLed == TRUE){
LedPin1 = LOW; 
LedPin2 = LOW;
LedPin3 = LOW;
LedPin4 = LOW;
LedPin5 = LOW; 
LedPin6 = LOW; 
}

switch (ledCounter){
case 0 : LedPin1 = HIGH;break; 
case 1 : LedPin2 = HIGH;break;
case 2 : LedPin3 = HIGH;break; 
case 3 : Ledpin4 = HIGH;break;
case 4 : Ledpin5 = HIGH;break; 
case 5 : Ledpin6 = HIGH;break;  
default: Ledpin1 = HIGH;break;  
}

changeLed = FALSE;
delay(20);
}

int button1Pressed (void) {

  if (analogRead(buttonUp) == LOW){
   delay(1); // 
      if (analogRead(buttonUp) == HIGH){
       delay(1);    
         if (analogRead(buttonUp) == HIGH){ 
          delay(1);
             if (analogRead(buttonUp) == HIGH){ 
             return 0;
             }
         }
      }
  
  }
return 1;
}


int button2Pressed (void) {

  if (analogRead(buttonDown) == LOW){
   delay(1); // 
      if (analogRead(buttonDown) == HIGH){
       delay(1);    
         if (analogRead(buttonDown) == HIGH){ 
          delay(1);
             if (analogRead(buttonDown) == HIGH){ 
             return 0;
             }
         }
      }
  
  }
return 1;
}

Thank you for your quick response.

I still have a question.

The code you have written is without the void setup. Do I still need it or is your enough?
And I mean with the voideo setup the pin arrangement and what put it is?

Sorry for the silly questions.

I used your setup and wrote only the main loop. This should be the full code:

const int buttonUp    = 9;
const int buttonDown  = 8;
const int ledPin1     = 7;
const int ledPin2     = 6;
const int ledPin3     = 5;
const int ledPin4     = 4;
const int ledPin5     = 3;
const int ledPin6     = 2;

boolean changeLed = false;
unsigned int ledCounter = 0; 

void setup() {
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  pinMode (ledPin4, OUTPUT);
  pinMode (ledPin5, OUTPUT);
  pinMode (ledPin6, OUTPUT);
  pinMode(buttonUp, INPUT);
  pinMode(buttonDown,INPUT);
  Serial.begin(9600);  
}

void loop(){

if (button1Pressed ()==0 ){
  if (ledCounter < 5) {
    ledCounter++ ;
    changeLed = true;
  }
}

if (button2Pressed()==0){
  if (ledCounter > 1) {
    ledCounter-- ;
    changeLed = true;
  }
}

if (changeLed == true){
  digitalWrite(ledPin1, LOW); 
  digitalWrite(ledPin2, LOW); 
  digitalWrite(ledPin3, LOW); 
  digitalWrite(ledPin4, LOW); 
  digitalWrite(ledPin5, LOW); 
  digitalWrite(ledPin6, LOW); 
}

switch (ledCounter){
case 0 : digitalWrite(ledPin1, HIGH);break; 
case 1 : digitalWrite(ledPin2, HIGH);break; 
case 2 : digitalWrite(ledPin3, HIGH);break;
case 3 : digitalWrite(ledPin4, HIGH);break;
case 4 : digitalWrite(ledPin5, HIGH);break;
case 5 : digitalWrite(ledPin6, HIGH);break; 
default: digitalWrite(ledPin1, HIGH);break; 
}

changeLed = false;
delay(20);
}

int button1Pressed (void) {

  if (analogRead(buttonUp) == LOW){
   delay(3); // 
      if (analogRead(buttonUp) == HIGH){
       delay(3);    
         if (analogRead(buttonUp) == HIGH){ 
          delay(3);
             if (analogRead(buttonUp) == HIGH){ 
             return 0;
             }
         }
      }
  
  }
return 1;
}


int button2Pressed (void) {

  if (analogRead(buttonDown) == LOW){
   delay(3); // 
      if (analogRead(buttonDown) == HIGH){
       delay(3);    
         if (analogRead(buttonDown) == HIGH){ 
          delay(3);
             if (analogRead(buttonDown) == HIGH){ 
             return 0;
             }
         }
      } 
  }
return 1;
}

It's not tested. The debouncer (the multiple reads to see if he pin is in high; error protection) might be a little bit to "harsh". If the pressed of a pushbutton is not always seen you can start looking there (in the buttonxPressed() functions).

Good luck

Hoi JKapteijn, en welkom op het forum.

It's clear to me that you are new to all this, and are trying to start understanding how things work.
I think it's a very good idea to do so using the examples that came with IDE, and change those to see what your changes do.
You asked a question about using an if statement, and got some answer using functions in a way that is not documented on this site.
It probably is good C code, but you will have to go somewhere else to find out how this works, and i don't think that's a good idea for someone starting.
And that answer doesn't address your question at all, it offers some way to cut the corner to your end goal, without any explanation at all.

So my answer to your question:
You are going in the right direction, but (of course..):
You are reading to see the "up" button.
Then you are comparing it to the last state, but lastButtonState was only set during initialisation , and never reset.
So you need to set lastButtonState somewhere.
Then, after deciding a new buttonUp press occurred, you are counting up.
Now, while still handling the buttonUp press, you are checking buttonState2 without reading buttonDown.
So buttonState2 will never set to anything but the value that was set during initialisation.

Seems to me you are trying to do too much at once.
You can go this way, but take it step by step.
So first have your sketch count up, then add the down part, but do not try to have both at once.

Thank you both for your quick replies.

Calin Tamaian thank you for your shortcut but what MAS3 said is that I have to take it step by step.

MAS3 my brother is a programmer and helpt me a lot with the structure and it works now and understand nearly intirely:). My code at this moment is like this:

// constants won't change. They're used here to 
// set pin numbers:
const int upButtonPin = 8;     // the number of the pushbutton pin
const int downButtonPin =  9;
const int firstGearLed =   2; // 
const int secondGearLed =  3; // 
const int thirdGearLed =   4; // 
const int fourthGearLed =   5; // 
const int fifthGearLed =   6; // 
const int sixthGearLed =   7; // 

int gearNumber = 0;
int upButtonState = 0;
int downButtonState = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(firstGearLed, OUTPUT);
  pinMode(secondGearLed, OUTPUT); 
  pinMode(thirdGearLed, OUTPUT);
  pinMode(fourthGearLed, OUTPUT); 
  pinMode(fifthGearLed, OUTPUT); 
  pinMode(sixthGearLed, OUTPUT); 

  // initialize the pushbutton pin as an input:
  pinMode(upButtonPin, INPUT);     
  pinMode(downButtonPin, INPUT); 
}

void loop(){
  // read the state of the pushbutton value:
  upButtonState = digitalRead(upButtonPin);
  downButtonState = digitalRead(downButtonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (upButtonState == HIGH) {
    if ( (gearNumber >= 0) && gearNumber <= 6){
      gearNumber = gearNumber + 1;
      delay(500);
//      gearNumber = gearNumber + 1;

      switch(gearNumber){
       case 1: 
           digitalWrite(firstGearLed, HIGH);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 2:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, HIGH);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 3:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, HIGH);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 4:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, HIGH);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 5:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, HIGH);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 6:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, HIGH);
         break;
      }

    }
  }
  
  //DOWN SHIFT
  if (downButtonState == HIGH) {
    if (gearNumber >= 1){
      gearNumber = gearNumber - 1;
      delay(500);
//      gearNumber = gearNumber + 1;

      switch(gearNumber){
       case 6: 
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, HIGH);
         break;
         
       case 5:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, HIGH);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 4:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, HIGH);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 3:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, HIGH);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 2:
           digitalWrite(firstGearLed, LOW);
           digitalWrite(secondGearLed, HIGH);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
         
       case 1:
           digitalWrite(firstGearLed, HIGH);
           digitalWrite(secondGearLed, LOW);
           digitalWrite(thirdGearLed, LOW);
           digitalWrite(fourthGearLed, LOW);
           digitalWrite(fifthGearLed, LOW);
           digitalWrite(sixthGearLed, LOW);
         break;
      }
    }
  }

  
  
  
}

My next goal is to use the button press to turn a servo to a programmed state in 6 steps. I ll try first and comeback with the code I produced:)

Thank you again!

Your drawing does not show current limiting resistors on the LED's this is not a good thing as you may damage your Arduino.

Thank you for your reply.

The circuit I showed is indeed not complete but I have them in place. I'll change the first comment to prevent people braking there arduino board.

I just fnished the code with this bit of code.

#include <Servo.h> 

Servo myservo;

void setup() 
{ 
  myservo.attach(9);
  myservo.write(90);  // set servo to mid-point
} 

void loop() {}

And put the myServo.write in the switch code with different angles with every different case..

Thanks for the help and the next goal is to incoporate a display in the code and circuit.