# Error push button

**URL:** <https://forum.arduino.cc/t/error-push-button/1076398>\
**Category:** General Guidance\
**Created:** [January 12, 2023, 3:44am UTC](https://forum.arduino.cc/t/error-push-button/1076398 "2023-01-12T03:44:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![siegfred](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@siegfred](https://forum.arduino.cc/u/siegfred)\
**Post date:** [January 12, 2023, 3:44am UTC](https://forum.arduino.cc/t/error-push-button/1076398/1 "2023-01-12T03:44:34Z")

</div>

Hey guys in need of some help. If I do, in actuality, it didn't work  
The push button will not detect; it just starts to count from 0-9 to 9 - 0 even if I don't press it,  
My project is if I press the button one, it counts to 0 - 9 and stop, and if I press button 2, it counts to 9- 0 and stoped

 ![111111](https://europe1.discourse-cdn.com/arduino/original/4X/4/6/f/46fef8459b9b1879a88fa6bb10491642ce823f1a.png)

int pinA = 13;  
int pinB = 12;  
int pinC = 11;  
int pinD = 10;  
int pinE = 9;  
int pinF = 8;  
int pinG = 7;

int Arduino\_Pins[7] = {pinA, pinB, pinC, pinD, pinE, pinF, pinG};  
int Segment\_Pins[10][7] = {{1, 1, 1, 1, 1, 1, 0}, // 0  
{0, 1, 1, 0, 0, 0, 0}, // 1  
{1, 1, 0, 1, 1, 0, 1}, // 2  
{1, 1, 1, 1, 0, 0, 1}, // 3  
{0, 1, 1, 0, 0, 1, 1}, // 4  
{1, 0, 1, 1, 0, 1, 1}, // 5  
{1, 0, 1, 1, 1, 1, 1}, // 6  
{1, 1, 1, 0, 0, 0, 0}, // 7  
{1, 1, 1, 1, 1, 1, 1}, // 8  
{1, 1, 1, 1, 0, 1, 1}, // 9  
};

void setup() {

```
// 7 SEGMENTS 
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
// BUTTON
pinMode(3,INPUT);
  pinMode(2,INPUT);

```

}

void print(int number);  
void turnoff() {

digitalWrite(pinA,LOW);  
digitalWrite(pinB,LOW);  
digitalWrite(pinB,LOW);  
digitalWrite(pinC,LOW);  
digitalWrite(pinD,LOW);  
digitalWrite(pinE,LOW);  
digitalWrite(pinF,LOW);  
digitalWrite(pinG,LOW);  
}

void loop() {

```
  // PARA 0 TO 9 
int button_state = digitalRead(3);
if (button_state == HIGH) {    
  for (int i = 0; i <=9;i++ ) {
      print(i); 
      delay(1000);
}
}
  else {	 
  
turnoff();
  } 

```

int button\_state2 = digitalRead(3);  
if (button\_state2 == HIGH) {

```
  for (int i =9;i >= 0; i--) { 
  print(i);
  delay(1000);

}
}
else { 
  turnoff();

```

}

```
}

void print(int number ) { 
int pin;
  //PARA PRINT 
  for (int i = 0 ; i < 7; ++i ) { 
    pin = Arduino_Pins[i];
    digitalWrite(pin,Segment_Pins[number][i]);
  } 
}

```

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [January 12, 2023, 4:46am UTC](https://forum.arduino.cc/t/error-push-button/1076398/2 "2023-01-12T04:46:12Z")

</div>

Check you have your switches wired correctly , such that when not pressed the inputs are pulled to 0 v via the resistor.  
Open circuit inputs with randomly float between logic levels .  
Alternatively look at using “ INPUT\_PULLUP “

Your seven segment display needs resistors for each segment , 330-680R,

Have a look at how to draw schematics, it will serve you well - people here hate Fritzing diagrams

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [January 12, 2023, 4:54am UTC](https://forum.arduino.cc/t/error-push-button/1076398/3 "2023-01-12T04:54:10Z")

</div>

As posted your code is hard to follow. **[Read the forum guidelines](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems)** to see how to properly post code and some good information on making a good post.

Use[the IDE autoformat tool (ctrl-t or Tools, Auto format)](https://www.tutorialspoint.com/auto-format-code-in-arduino-ide) before posting code in code tags.

Please go back and **fix your original post.**

With those 6mm tactile switches it is better to **wire to diagonal terminals** to avoid a short.

 ![dig sw button](https://europe1.discourse-cdn.com/arduino/original/4X/1/b/2/1b2f2806f40728ccf5e3f94516a645ac0aea533d.jpeg)

As shown the switch is wired to ground and to an input set to pinMode INPUT\_PULLUP. This is the accepted way to wire a switch.

 ![digin](https://europe1.discourse-cdn.com/arduino/original/4X/1/0/b/10b7425253067f34d10484a81e7e29f2d3cc4f59.jpeg)

Please post photos of your wiring.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [January 13, 2023, 2:49am UTC](https://forum.arduino.cc/t/error-push-button/1076398/4 "2023-01-13T02:49:54Z")

</div>

In your program, just above this line:  
` if (button_state == HIGH) {`  
write this line:  
`button_state = HIGH;`  
It will force your program to execute the counting and will let you know if your LED is connected correctly.

Then, do the same thing above this line:  
`if (button_state2 == HIGH) {`  
write this line:  
`(button_state2 = HIGH;`

Two more things... the lines that have:  
`print(i);`  
~~should read:~~ (scratch that - my error)  
If you change it to:  
`Serial.print(i);`  
you will see that your "count up" and "count down" logic work (your code does not let the buttons work).

Here is your current code (it has a few flaws that you need to correct), formatted for easier reading...

```cpp
int pinA = 13;
int pinB = 12;
int pinC = 11;
int pinD = 10;
int pinE = 9;
int pinF = 8;
int pinG = 7;

int Arduino_Pins[7] = {pinA, pinB, pinC, pinD, pinE, pinF, pinG};
int Segment_Pins[10][7] = {{1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}, // 9
};

void setup() {

  // 7 SEGMENTS
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  // BUTTON
  pinMode(3, INPUT);
  pinMode(2, INPUT);

}

void print(int number);
void turnoff() {

  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
  digitalWrite(pinB, LOW);
  digitalWrite(pinC, LOW);
  digitalWrite(pinD, LOW);
  digitalWrite(pinE, LOW);
  digitalWrite(pinF, LOW);
  digitalWrite(pinG, LOW);
}

void loop() {

  // PARA 0 TO 9
  int button_state = digitalRead(3);
  if (button_state == HIGH) {
    for (int i = 0; i <= 9; i++ ) {
      print(i);
      delay(1000);
    }
  }
  else {

    turnoff();
  }

  int button_state2 = digitalRead(3);
  if (button_state2 == HIGH) {

    for (int i = 9; i >= 0; i--) {
      print(i);
      delay(1000);

    }
  }
  else {
    turnoff();

  }

}

void print(int number ) {
  int pin;
  //PARA PRINT
  for (int i = 0 ; i < 7; ++i ) {
    pin = Arduino_Pins[i];
    digitalWrite(pin, Segment_Pins[number][i]);
  }
}

```

Hey, @siegfred - I used the online simulator ([wokwi.com](http://wokwi.com)) and your code and circuit work. Just be sure to check your wiring, and type of 7seg LED (common cathode or common anode). Good work!

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [January 13, 2023, 9:36am UTC](https://forum.arduino.cc/t/error-push-button/1076398/5 "2023-01-13T09:36:40Z")

</div>

> [@siegfred](#):
>
> `int button_state = digitalRead(3);`  
> int button\_state2 = digitalRead(3);

You've used the same pin number for both button\_state and button\_state2.  
Surely, one of them should use pin 2.

---

<div class="post-metadata">

**Author:** ![siegfred](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@siegfred](https://forum.arduino.cc/u/siegfred)\
**Post date:** [January 12, 2023, 2:10am UTC](https://forum.arduino.cc/t/error-push-button/1076398/6 "2023-01-12T02:10:27Z")

</div>

Hey guys in need of some help. If I do, in actuality, it didn't work  
The push button will not detect; it just start to count to 0-9 to 9 - 0 even I don't press it,  
My project is if i pressed the button one i counts to 0 - 9 and stop and if i pressed button 2 it count to 9- 0 stoped

int pinA = 13;  
int pinB = 12;  
int pinC = 11;  
int pinD = 10;  
int pinE = 9;  
int pinF = 8;  
int pinG = 7;

int Arduino\_Pins[7] = {pinA, pinB, pinC, pinD, pinE, pinF, pinG};  
int Segment\_Pins[10][7] = {{1, 1, 1, 1, 1, 1, 0}, // 0  
{0, 1, 1, 0, 0, 0, 0}, // 1  
{1, 1, 0, 1, 1, 0, 1}, // 2  
{1, 1, 1, 1, 0, 0, 1}, // 3  
{0, 1, 1, 0, 0, 1, 1}, // 4  
{1, 0, 1, 1, 0, 1, 1}, // 5  
{1, 0, 1, 1, 1, 1, 1}, // 6  
{1, 1, 1, 0, 0, 0, 0}, // 7  
{1, 1, 1, 1, 1, 1, 1}, // 8  
{1, 1, 1, 1, 0, 1, 1}, // 9  
};

void setup() {  
/  
// 7 SEGMENTS  
pinMode(pinA, OUTPUT);  
pinMode(pinB, OUTPUT);  
pinMode(pinC, OUTPUT);  
pinMode(pinD, OUTPUT);  
pinMode(pinE, OUTPUT);  
pinMode(pinF, OUTPUT);  
pinMode(pinG, OUTPUT);  
// BUTTON  
pinMode(2, INPUT);  
pinMode(3, INPUT);  
// POTENTIOMETER

}

// Function declaration  
void print(int number);  
void turnoff() {

digitalWrite(pinA,LOW);  
digitalWrite(pinB,LOW);  
digitalWrite(pinB,LOW);  
digitalWrite(pinC,LOW);  
digitalWrite(pinD,LOW);  
digitalWrite(pinE,LOW);  
digitalWrite(pinF,LOW);  
digitalWrite(pinG,LOW);  
}  
//  
void loop() {

// read potentiometer value

```
// PARA 0 TO 9 

```

int button\_state = digitalRead(2);  
int button\_state2 = digitalRead(3);

if (digitalRead(3) == HIGH) {  
for (int i = 0; i \<=9;i++ ) {  
print(i); //  
delay(1000);

}

}

else {  
turnoff;

}

if (digitalRead(2) == HIGH) {

```
for (int i =9;i >= 0; i--) { //Para Mo count  
  print(i);
  delay(1000);

```

}  
}  
else {  
turnoff();  
}

}

void print(int number ) {  
int pin;

```
for (int i = 0 ; i < 7; ++i ) { 
  pin = Arduino_Pins[i];
  digitalWrite(pin,Segment_Pins[number][i]);
} 

```

}

 ![111111](https://europe1.discourse-cdn.com/arduino/original/4X/4/6/f/46fef8459b9b1879a88fa6bb10491642ce823f1a.png)

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [January 12, 2023, 2:51am UTC](https://forum.arduino.cc/t/error-push-button/1076398/7 "2023-01-12T02:51:22Z")

</div>

Formatted

```cpp
int pinA = 13;
int pinB = 12;
int pinC = 11;
int pinD = 10;
int pinE = 9;
int pinF = 8;
int pinG = 7;

int Arduino_Pins[7] = {pinA, pinB, pinC, pinD, pinE, pinF, pinG};
int Segment_Pins[10][7] = {{1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}, // 9
};

void setup() {
  /
  // 7 SEGMENTS
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  // BUTTON
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  // POTENTIOMETER

}

// Function declaration
void print(int number);
void turnoff() {

  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
  digitalWrite(pinB, LOW);
  digitalWrite(pinC, LOW);
  digitalWrite(pinD, LOW);
  digitalWrite(pinE, LOW);
  digitalWrite(pinF, LOW);
  digitalWrite(pinG, LOW);
}
//
void loop() {

  // read potentiometer value

  // PARA 0 TO 9

  int button_state = digitalRead(2);
  int button_state2 = digitalRead(3);

  if (digitalRead(3) == HIGH) {
    for (int i = 0; i <= 9; i++ ) {
      print(i); //
      delay(1000);

    }

  }

  else {
    turnoff;

  }

  if (digitalRead(2) == HIGH) {

    for (int i = 9; i >= 0; i--) { //Para Mo count
      print(i);
      delay(1000);

    }
  }
  else {
    turnoff();
  }

}

void print(int number ) {
  int pin;

  for (int i = 0 ; i < 7; ++i ) {
    pin = Arduino_Pins[i];
    digitalWrite(pin, Segment_Pins[number][i]);
  }

}

```

Your "delay()" might be blocking button pushes.

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [January 12, 2023, 7:12am UTC](https://forum.arduino.cc/t/error-push-button/1076398/8 "2023-01-12T07:12:40Z")

</div>

get rid of the +v and resistors, and wire the switch direct from gnd to the input pin. Change PINMODE to INPUT\_PULLUP. Test for the switch going LOW

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 12, 2023, 11:18pm UTC](https://forum.arduino.cc/t/error-push-button/1076398/9 "2023-01-12T23:18:17Z")

</div>

Hi, @siegfred

Have you got some simple code that JUST tests the buttons using the IDE Monitor as your output device?  
To test your button operation?

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [January 21, 2023, 9:41am UTC](https://forum.arduino.cc/t/error-push-button/1076398/10 "2023-01-21T09:41:28Z")

</div>

Cross-posts merged per report.

@siegfred in the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the ["How to get the best out of this forum"](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2023, 9:41am UTC](https://forum.arduino.cc/t/error-push-button/1076398/11 "2023-07-20T09:41:59Z")

</div>

Closed on July 20, 2023, 9:41am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
