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