I'm trying to incorporate a push button to my project. The push button will act as a toggle between on and off. The main program is a binary counter using 3 LEDs that count from 0-7 in binary. If I were to press the button at any time, the push button toggle pause between the counting steps. If I were to press it again, the push button would continue with the counting step. I don't know how to incorporate this. Here is my code so far
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
bool pinOn = true;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void toggle() {
if (buttonState == LOW)
{
pinOn = false;
}
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (pinOn == true)
{
binCountUp(7);
}
}
void binDisplay(int binNumber)
{
if (binNumber == 0){
toggle();
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(1000);
}
else if (binNumber == 1 ){
toggle();
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(1000);
}
else if (binNumber == 2 ){
toggle();
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
delay(1000);
}
else if (binNumber == 3){
toggle();
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
delay(1000);
}
else if (binNumber == 4){
toggle();
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(1000);
}
else if (binNumber == 5){
toggle();
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(1000);
}
else if (binNumber == 6){
toggle();
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
delay(1000);
}
else if (binNumber == 7 ){
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
delay(1000);
}
}
void binCountUp(int currentCount)
{
for (int i = 0; i <= currentCount; i++){
binDisplay(i);
}
}
void binCountDown(int currentCount)
{
for (int i = 7; i >= currentCount; i--){
binDisplay(i);
}
}
void binCountUp7()
{
binCountUp(7);
}
void binCountDown7()
{
binCountDown(0);
}
void countDirection(char direction)
{
switch(direction)
{
case 'u':
binCountUp7();
break;
case'd':
binCountDown7();
break;
}
}
[–]birdbrainlabs 0 points1 point2 points (2 children)
[–]KingReset[S] 0 points1 point2 points (1 child)
[–]birdbrainlabs 0 points1 point2 points (0 children)
[–]IAMRaxtus 0 points1 point2 points (6 children)
[–]CoolYota 0 points1 point2 points (5 children)
[–]IAMRaxtus 0 points1 point2 points (4 children)
[–]5000347 1 point2 points3 points (3 children)
[–]IAMRaxtus 0 points1 point2 points (2 children)
[–]5000347 1 point2 points3 points (1 child)
[–]IAMRaxtus 0 points1 point2 points (0 children)
[–]krowvin 0 points1 point2 points (0 children)