Creating increment and decrement when button push

I am trying to make a program that counts from 0 t0 7 and when the button pressed, it will count from 7 to 0. And when I uploaded to my board, it behaves differently. thanks in advance!

Here's my code.

const int ledPin1 = 11;
const int ledPin2 = 12;
const int ledPin3 = 13;
const int inPin = 2;
int val = 0;

void setup()
{

pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(inPin,INPUT);
}

void loop()

{
val= digitalRead(inPin);
Initialval();

if (val == HIGH)
{
Gohigh();
}
else
{
Golow();
}
}

void Gohigh()

{
delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);

}

void Golow()

{
delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);

delay(700);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(1000);
}

void Initialval()
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(2000);
}

What is it doing that it shouldn't? What isn't it doing that it should? Hard to diagnose a problem if you don't tell the symptoms.

  1. It would help to know about the wiring, also.

  2. Pin 13 is connected to a built-in LED on some Arduinos. What Arduino do you have?

I'm using Uno board.I use pin 11 to 13 for led's and each of them has a 220 ohm resistor. On pin 2, I use push button switch and it has a 10k pull down resistor.

I observed that even if I didn’t press the button, it will execute all the lines of code. It bypass the IF ELSE syntax. I am don’t know the problem on my code. thanks

Use code tag to post the code.

As the initval() function uses 2sec delay, so you might need to press a little longer to register the button press.

As @Delta_G mentioned, tell us the issue you are facing.

if(digitalRead(inPin)= HIGH){
  GoHigh();
}
else{
  GoHigh();
}

Try this.

sarouje:

if(digitalRead(inPin)= HIGH){

GoHigh();
}
else{
  GoHigh();
}



Try this.

Except write the if condition properly. 



if(digitalRead(inPin)== HIGH){

My applogies, thanks for correcting it @delta,

Hi,
I have a suggestion. Normally when using a switch I used the command INPUT_PULLUP. That will pull up the switch to VCC. Just need the switch to ground the input. It is a good practices when using switches as input to change the input condition. Just an advice. You do not said if you have a pull up in the input. You should pull the input high and then ground the input.

I used the command INPUT_PULLUP.

That is NOT a command. It is a VALUE.