How do you stop a 7 segment display by a push button?

Hi. I'm currently a student studying robotics cause its required for my school. I have an assignment about making a buzzer beater. Like the ones on shows where there's a limited time and the players have to press a button where the buzzer will make a loud noise and stop the timer. Well I had tried to make one in Tinkercad with such little notes my teacher gave me and it's not really working out for me. I was hoping someone could help me out cause its deadline is near. Also I'm really new to coding and robotics so pls be nice cuz i really dont know what im doing.

// define the pins, LEDs, and variables as constant
const int LED1 = 13;
const int PUSHBTN1 = 12;
const int LED2 = 11;
const int PUSHBTN2 = 10;
const int PIEZO = 9;

// define the pins, LEDs, and variables
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int PUSHBTN1Value;
int PUSHBTN2Value;

void setup() { 
  Serial.begin(9600);
  pinMode (pinA, OUTPUT); // sets the pins as an OUTPUT
  pinMode (pinB, OUTPUT);
  pinMode (pinC, OUTPUT);
  pinMode (pinD, OUTPUT);
  pinMode (pinE, OUTPUT);
  pinMode (pinF, OUTPUT);
  pinMode (pinG, OUTPUT);
  pinMode (LED1, OUTPUT); // sets the LEDs as an OUTPUT
  pinMode (LED2, OUTPUT);
  pinMode (PIEZO, OUTPUT);
  pinMode (PUSHBTN1, INPUT); // sets the push buttons as an INPUT
  pinMode (PUSHBTN2, INPUT);
}
void one(){
  //1 1111001 
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void two(){
  //2 0100100
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void three(){
  //3 0110000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void four(){
  // 4 0011001
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
  delay(1000);
}
void five(){
  // 5 0010010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void six(){
  // 6 0000010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void seven(){
  // 7 1111000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void eight(){
  // 8 1111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void nine(){
  // 9 1001111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void zero(){
  // 0 0111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, HIGH);
} 
void stop(){
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void loop() {
  PUSHBTN1Value = digitalRead(PUSHBTN1);
  PUSHBTN2Value = digitalRead(PUSHBTN2);
  
  if(PUSHBTN1 == 1){
    exit(0);
    digitalWrite (LED1, HIGH);
    tone(PIEZO, 800);
    
  }else if (PUSHBTN2 == 1){
    exit(0);
    digitalWrite (LED2, HIGH);
    tone(PIEZO, 800);
  }else{
    one();
    delay(1000);
    two();
    delay(1000);
    three();
    delay(1000);
    four();
    delay(1000);
    five();
    delay(1000);
    six();
    delay(1000);
    seven();
    delay(1000);
    eight();
    delay(1000);
    nine();
    delay(1000);
    zero();
    delay(1000);
    tone(PIEZO, 500);
    delay(700);
    exit(0);
  }
}

Statement like delay(1000) mean "do nothing for 1 second". That means you can't read a button.

Study this tutorial to learn how to avoid using delay(): https://www.baldengineer.com/blink-without-delay-explained.html

This looks quite a bit like this thread. Seven segment display with one pushbutton on arduino uno - #6 by Programmer0009

it is but my code also doesn't work. I'm stuck in a last(one) step.

did you try with a input_pullup

How do you do that?

https://www.google.com/search?as_q=arduino+input_pullup

How do you stop a 7 segment display by a push button?

Fit a shot gun cartridge underneath the display. Arrange that a hammer swings on a thread and hit the cartridge on the ignition point. Have the thread holding back the hammer cut when a solenoid with a scalpel blade on it is fired.

Then wire that solenoid to a logic level FET to an output pin. Detect the logic level of a switch and let that trigger a pulse on the output connected to the solenoid. It works very good but only once.


Alternately you can use an if statement to see if the time is up and when it is turn the sound on.
Tip never use the exit command anywhere it is a sign that you haven't got a clue about how to program and you wouldn't want to give that impression would you?

So if the loop is terminated because PUSHBTN1 is equal to 1 how are the instructions that follow it ever going to be executed? Not even the else will be reached because that exit stops anything else happening.

1 Like

usually studying is done after school at a university
If you have left school and started studying.

The most important thing to learn is to become independent of the professors "teaching" which is often not teaching but read out.

The basic thing is to learn how to do independent completion of projects.

This includes asking experts (other experts than the professors)

You seem to have underestimated how much time it needs to learn programming
or other possability you have been lazy about working on this project.

Your code needs to count-down until the button is pressed.
Then the count-down shall stop and the buzzer shall be turned on
which means change from executing count-down to execute buzzing.

Be the change you want to see in the world
best regards Stefan

1 Like

@chibbs, do not cross-post. Other thread removed.

1 Like

Hi Stefan. Thanks for the comment and trying to help me but I'm still having some trouble. I know it's stupid and I am really stupid for this subject so I'm sorry if you get disappointed for the result. I tried someone's advice to make the timer play. It eventually did but my problem now is the button. I also tried the demonstration code you gave me. Um it did not work for me. I probably did it wrong and put just stuff that did not make sense. The 7 segment display is just not working and I'm really confused. I didn't get a good foundation on what are functions and stuff, I just followed my teacher's code that's literally all I learned. Not blaming it on them though, I need to study more of this. But if you could give me some advice, that would be great. Here is the code I made where the timer works.

// define the pins, LEDs, and variables as constant
const int LED1 = 13;
const int PUSHBTN1 = 12;
const int LED2 = 11;
const int PUSHBTN2 = 10;
const int PIEZO = 9;

// define the pins, LEDs, and variables
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int PUSHBTN1Value;
int PUSHBTN2Value;
int counter = 10;

void setup() { 
  Serial.begin(9600);
  pinMode (pinA, OUTPUT); // sets the pins as an OUTPUT
  pinMode (pinB, OUTPUT);
  pinMode (pinC, OUTPUT);
  pinMode (pinD, OUTPUT);
  pinMode (pinE, OUTPUT);
  pinMode (pinF, OUTPUT);
  pinMode (pinG, OUTPUT);
  pinMode (LED1, OUTPUT); // sets the LEDs as an OUTPUT
  pinMode (LED2, OUTPUT);
  pinMode (PIEZO, OUTPUT);
  pinMode (PUSHBTN1, INPUT); // sets the push buttons as an INPUT
  pinMode (PUSHBTN2, INPUT);
}
void one(){
  //1 1111001 
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void two(){
  //2 0100100
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void three(){
  //3 0110000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void four(){
  // 4 0011001
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void five(){
  // 5 0010010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void six(){
  // 6 0000010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void seven(){
  // 7 1111000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void eight(){
  // 8 1111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void nine(){
  // 9 1001111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void zero(){
  // 0 0111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, HIGH);
} 
void stop(){
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void countdown(int countdown10){
  if (countdown10 == 1){
    one();
  } else if (countdown10 == 2){
    two(); 
  } else if (countdown10 == 3){
    three(); 
  } else if (countdown10 == 4){
    four(); 
  } else if (countdown10 == 5){
    five(); 
  } else if (countdown10 == 6){
    six();
  } else if (countdown10 == 7){
    seven();
  } else if (countdown10 == 8){
    eight(); 
  } else if (countdown10 == 9){
    nine();
  } else if (countdown10 == 0){
    zero(); 
  }
}
 
void loop() {
  
  PUSHBTN1Value = digitalRead(PUSHBTN1);
  PUSHBTN2Value = digitalRead(PUSHBTN2);
  
  if(PUSHBTN1 == 1){
    exit(0);
    digitalWrite (LED1, HIGH);
    tone(PIEZO, 800);
    
  }else if (PUSHBTN2 == 1){
    exit(0);
    digitalWrite (LED2, HIGH);
    tone(PIEZO, 800);
  }else{
    countdown(counter);
    counter = counter - 1;
  }
  delay(1000);
}

Here is the code where I followed your code.

#define ProjectName "Buzzer Beater"
#define unPressed HIGH
#define pressed   LOW

// define the pins, LEDs, and variables as constant
const int LED1 = 13;
const int LED2 = 11;
const int PIEZO = 9;
const byte pushBTN1 = A0;
const byte pushBTN2 = A1;

bool activationMode = false;
unsigned long myCounter = 10;

// define the pins, LEDs, and variables
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int PUSHBTN1Value;
int PUSHBTN2Value;
int counter = 10;

void setup() { 
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  
  pinMode (pinA, OUTPUT); // sets the pins as an OUTPUT
  pinMode (pinB, OUTPUT);
  pinMode (pinC, OUTPUT);
  pinMode (pinD, OUTPUT);
  pinMode (pinE, OUTPUT);
  pinMode (pinF, OUTPUT);
  pinMode (pinG, OUTPUT);
  pinMode (LED1, OUTPUT); // sets the LEDs as an OUTPUT
  digitalWrite(LED1, LOW);
  pinMode (LED2, OUTPUT);
  digitalWrite(LED2, LOW);
  pinMode (PIEZO, OUTPUT);
  pinMode (pushBTN1, INPUT_PULLUP); // sets the push buttons as an INPUT
  pinMode (pushBTN2, INPUT_PULLUP);
}
 
void one(){
  //1 1111001 
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void two(){
  //2 0100100
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void three(){
  //3 0110000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, LOW);
}
void four(){
  // 4 0011001
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWArite (pinG, LOW);
  delay(1000);
}
void five(){
  // 5 0010010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void six(){
  // 6 0000010
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void seven(){
  // 7 1111000
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}
void eight(){
  // 8 1111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void nine(){
  // 9 1001111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, LOW);
}
void zero(){
  // 0 0111111
  digitalWrite (pinA, LOW);
  digitalWrite (pinB, LOW);
  digitalWrite (pinC, LOW);
  digitalWrite (pinD, LOW);
  digitalWrite (pinE, LOW);
  digitalWrite (pinF, LOW);
  digitalWrite (pinG, HIGH);
} 
void stop(){
  digitalWrite (pinA, HIGH);
  digitalWrite (pinB, HIGH);
  digitalWrite (pinC, HIGH);
  digitalWrite (pinD, HIGH);
  digitalWrite (pinE, HIGH);
  digitalWrite (pinF, HIGH);
  digitalWrite (pinG, HIGH);
}

 void my_Action(int countdown10){
    myCounter++;
    Serial.println(myCounter);
  if (countdown10 == 1){
    one();
  } else if (countdown10 == 2){
    two(); 
  } else if (countdown10 == 3){
    three(); 
  } else if (countdown10 == 4){
    four(); 
  } else if (countdown10 == 5){
    five(); 
  } else if (countdown10 == 6){
    six();
  } else if (countdown10 == 7){
    seven();
  } else if (countdown10 == 8){
    eight(); 
  } else if (countdown10 == 9){
    nine();
  } else if (countdown10 == 0){
    zero(); 
  }
}
 
void loop() {
  activationMode = GetToggleSwitchState(); // must be executed all the time
  execute_if_Active(activationMode);
}

bool GetToggleSwitchState(){
  // "static" makes variables persistant over function calls
  static bool toggleState = false;
  static bool lastToggleState = false;
  
  static byte buttonStateOld = unPressed;
  static unsigned long buttonScanStarted = 0;
  unsigned long buttonDebounceTime = 50;
  unsigned long buttonDebounceTimer;
  
  byte buttonStateNew;
  
  if ( TimePeriodIsOver(buttonDebounceTimer, buttonDebounceTime) ) {
    // if more time than buttonDebounceTime has passed by
    // this means let pass by some time until
    // bouncing of the button is over
    buttonStateNew = digitalRead(pushBTN1);
    
    if (buttonStateNew != buttonStateOld){
      // if button-state has changed
      digitalWrite (LED1, HIGH);
      if (buttonStateNew == unPressed) {
        // if button is released
        toggleState = !toggleState; //toggle state-variable
        my_Action(counter);
        counter = counter - 1;
      } // the attention-mark is the NOT operator
    } // which simply inverts the boolean state
  } // !true = false NOT true is false
  // !false = true NOT false is true
  return toggleState;
}

void execute_if_Active(bool p_IsActivated) {
  
  static bool lastIsActivated;
}
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis = millis();
  if (currentMillis - expireTime >= TimePeriod ) {
    expireTime = currentMillis; // set new expireTime
    return true;				// more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;
  delay(1000);
}

Also, I'm really grateful for you helping me. I'm sorry if it feels like your teaching me without me paying you. But if you can't help me with this, it's all good either way.

I don't believe you if you say

It took you

two days

to answer.

To be more precise:
Your code needs to count-down until the button is pressed.

if button is not pressed simply count-down.

If button is pressed
Then the count-down shall stop

and the buzzer shall be turned on
which means change from executing count-down

to execute buzzing.

There is no way around learning the basics.
If you want to keep my support. You should answer quickly with a question about a detail.
Regardless what the detail is. You seem to try to finish this at highest-speed in the last second. This does not work.

You should change to learn at highest intensity = asking dozens of questions about the details.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

Be the change you want to see in the world
best regards Stefan

Why are you ignoring my post #8? The exit call is just screwing you up.
As soon as you use it the function ends and the instructions following can NOT be run.

Yes yes I knew what you were talking about. I was just trying to figure out how to stop the timer. I wasn't trying to ignore your post. I'm sorry if you saw it that way. Also I took your note about the exit call, I was just trying to show the code for the timer.

I'm sorry if I took two days to answer. I had family matters and my teacher changed the deadline because too many students were complaining about the deadline. I'm also sorry if I don't understand the concept of this subject. I'm a literal teen learning this subject with minimal notes about the basics. I was taught to just copy what the teacher gave me, and if you think I'm making excuses for "not studying" the basics and me being "lazy" about this project, I'm being full honest here so I could finish my project on time. Thank you for your help but I'm sorry if I'm too busy to even reply to you faster than you want. It's midnight and I am struggling right now.

So this answer did explain a little bit about your situation.

when

will you post a question about a detail?
This evening?
or do you want to go to bed now and post the questions
5-6 hours later?

5-6 hours later. I'm sorry if this makes you feel unhappy or disappointed that "I'm not learning" or I'm not trying my best but I am trying and just trying to pass this project on time. It's due on April 18 so feel free to not even help me anymore if you're just gonna tell me I'm lazy about this project.

until April 18 is 8 days. Caclulated from tomorrow until 17 april.
How much time can you afford each day learning programming?

3-5 hours maybe. I do house chores everyday and I have 3 projects due on Monday and Tuesday.

3-5 hours per day or in summary?