Detecting Third button press

Hi guys,

I have this problem:

Create an Arduino device with three Input Buttons and four LED output.

The four LED lights will be used to count from 0 up to 15.
That means it should be able to count from 0 to 1, 0 to 2, 0 to 6, etc.

Three buttons should work in the following way.
Button #1: The number of times you push this button will be the number that your LEDs will count to.
Button #2: Count button: When button two is pressed the LED lights will begin counting
Button #3: Reset button: When button three is pressed the LED lights will stop counting the Button #1 count will be reset to zero.

Note: Your four LEDs must display the counter in binary

My code:

int ledOne = 7;
int ledTwo = 6;
int ledThree = 5;
int ledFour = 4;
int buttonCount;// Count how many times button is pressed
int buttonState = 0;
int buttonState2 = 0;
int lastButtonState = 0;
int lastButtonState2 = 0;


void setup()
{
 Serial.begin(9600);
 pinMode(11,INPUT);// pin to keep track of buttonCounts
 pinMode(12, INPUT);//Button to start the counting
 pinMode(13, INPUT);
}

void loop()
{
  //BUTTON STATES USED TO MAKE SURE THE BUTTON DOESNT REPEATEDLY SEND INFO WHEN PRESSED (LIKE A DEBOUNCE)
  buttonState = digitalRead(11) ;//Press to count
  buttonState2 = digitalRead(12);//Press to light up leds (enter button) 
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      if (buttonCount < 15) {
          buttonCount++;
      }
      else {
          buttonCount = 0;
      }
     Serial.println(buttonCount);
    }
  }
    
  lastButtonState = buttonState;
    
  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      for (int i = 0; i < buttonCount; i++) {
        beginCount(i + 1);
      }
    }
  }
    
  lastButtonState2 = buttonState2;
   

}

void beginCount(int number) {
    int binaryArray[4];
    if(number == 0) {
      binaryArray[0] = 0;
      binaryArray[1] = 0;
      binaryArray[2] = 0;
      binaryArray[3] = 0;
    }
    else {
      for(int i = 0; number > 0; i++) {
        binaryArray[i] = number % 2;
        number = number / 2;
      }   
    }
    if(binaryArray[0] == 1) {
      digitalWrite(ledOne,HIGH);
    }
    if(binaryArray[1] == 1) {
      digitalWrite(ledTwo,HIGH);
    }
    if(binaryArray[2] == 1) {
      digitalWrite(ledThree,HIGH);
    }
    if(binaryArray[3] == 1) {
      digitalWrite(ledFour,HIGH);
    }
    delay(400);
    digitalWrite(ledOne, LOW);
    digitalWrite(ledTwo, LOW);
    digitalWrite(ledThree, LOW);
    digitalWrite(ledFour, LOW);
    delay(400);  
}

My circuit:

How do i make it so that button 3 (the button on the rightside) resets the buttonCount to 0 AND it stops the lights from displaying?

Note: Our teacher said that you CANNOT use the reset pin on the board (idk y).

When is our assignment due to be handed in?

Tomorrow. All I need to do is to basically reset the board, without using the reset pin

Your tomorrow, or my tomorrow, which is only a couple of hours away?

Go to bed NOW !

1 Like

Ok it is currently 5pm where I am. This is due at 12 pm tomorrow, which is in 19 hours

What does this mean ?

turn buttonCount to 0, and stop all the lights from blinking, if they are.

Did you write the code you posted ?

ye i wrote all the code i posted

But you forgot to make the outputs outputs.

The code itself is fine. All I need is disabling the lights if they are blinking, and setting buttonCount to 0

Ok.
Calibrated

Can I ask if you've built the hardware and tested the software on it?

Your switches are not wired properly.

Most here recommend wiring your switches as S3 in the image below:

I just started coding with Arduino. I can barely decipher this image

Just be aware when a switch is pushed/closed, you are attaching 5v to the input pin.

yes? How does this have to do with resetting the board with a switch

We try to explain the the best way to do things here.


Well, if the sketch works for you as it is, then what you want should be simple.

When a/any LED(s) are flashing, set a Flag variable to true.
When a/any LED(s) stop flashing reset this Flag.

When it comes to the time or event when things should reset, and if the Fag is true, make your counter zero (0) and stop flashing.

If the LEDs are flashing, they're flashing very dimly

I made it so that whenever I press button 3, I set a flag, stopLights to true. Then when i press button 2, I put a condition before executing beginCount(i+1); saying that if stopLights is true, then break out of the loop. However, the issue is that button 3 won't detect input until the for loop is done executing the code.