Error push button

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

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]);
  } 
}

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

As posted your code is hard to follow. Read the forum guidelines 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) 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.

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.

Please post photos of your wiring.

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...

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) 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!

You've used the same pin number for both button_state and button_state2.
Surely, one of them should use pin 2.

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]);
} 

}

Formatted

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.

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

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.. :smiley: :+1: :coffee: :australia:

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" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.