I have a 2 LED display that needs to blink in different patterns. I would like help to fix the code for when (buttonPushCounter == 5), currently all the delays in it makes the process hang so you can't easily cycle to the next button press.
//
//c++
int ledState1 ; // led1 control
int ledState2 ; // led2 control
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState; // current state of the button
int lastButtonState; // previous state of the button
int led1 = 10;
int led2 = 11;
int ledPin = 13; // the pin that the LED is attached to
unsigned long previousMillis = 0; //time of last blink
const long interval175 = 175; //175 millisecond blink interval
const long interval200 = 200;
void setup() {
// put your setup code here, to run once:
//Turn off Built in LED
digitalWrite(ledPin, LOW);
pinMode(9,INPUT); // we are setting pin 2 as our input where we are connectign the soft touch switch
pinMode(led1, OUTPUT); // 1st LED light
pinMode(led2, OUTPUT); // 2nd LED light
pinMode(ledPin, OUTPUT); //Built in LED
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(9);
unsigned long currentMillis = millis();
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print("number of button pushes:");
Serial.println(buttonPushCounter);
}
// Delay a little bit to avoid bouncing
delay(20);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if(buttonPushCounter==6){
buttonPushCounter=1;
Serial.print("number of button pushes:");
Serial.println(buttonPushCounter);
}
if(buttonPushCounter == 1){
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
}
else if(buttonPushCounter == 2){
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (buttonPushCounter == 3){
if (currentMillis - previousMillis >= interval175) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (ledState1 == LOW) {
ledState1 = HIGH;
ledState2 = LOW;
} else {
ledState1 = LOW;
ledState2 = HIGH;
}
digitalWrite(led1, ledState1);
digitalWrite(led2, ledState2);
}}
else if (buttonPushCounter == 4){
if (currentMillis - previousMillis >= interval200) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (ledState1 == LOW) {
ledState1 = HIGH;
ledState2 = HIGH;
} else {
ledState1 = LOW;
ledState2 = LOW;
}
digitalWrite(led1, ledState1);
digitalWrite(led2, ledState2);
}}
else if (buttonPushCounter == 5){
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(450); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(450); // wait
}}
I've been trying, I used them for the previous two button pushes. I can't seem to work out how to get it to work with the last pattern since it is not a constant blink.
Congrats You have a code doing the job!
Any crappy, spagetti, code doing what it's wanted to do is a good code. A sophisticated code malfunctioning makes nobody satisfied. Your code is not that bad.
Keep Your eyes open looking at other topics and suck up new ways to do things in the future.
//
//c++
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState; // current state of the button
int lastButtonState; // previous state of the button
int led1 = 10;
int led2 = 11;
int ledPin = 13; // the pin that the LED is attached to
void setup() {
// put your setup code here, to run once:
pinMode(9, INPUT); // we are setting pin 2 as our input where we are connectign the soft touch switch
pinMode(led1, OUTPUT); // 1st LED light
pinMode(led2, OUTPUT); // 2nd LED light
pinMode(ledPin, OUTPUT); //Built in LED
Serial.begin(9600);
}
void loop() {
//Turn off Built in LED
digitalWrite(ledPin, LOW);
// read the pushbutton input pin:
buttonState = digitalRead(9);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print("number of button pushes:");
Serial.println(buttonPushCounter);
}
// Delay a little bit to avoid bouncing
delay(20);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == 5) {
buttonPushCounter = 1;
Serial.print("number of button pushes:");
Serial.println(buttonPushCounter);
}
if (buttonPushCounter == 1) {
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
}
else if (buttonPushCounter == 2) {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (buttonPushCounter == 3) {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(150); // wait
}
else if (buttonPushCounter == 4) {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(450); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(450); // wait
}
}
When I get to the 5th button push you have to hold the button down to get it to register the push, I know this is due to all the delays written in the code for that but I can't find a better way to do it.
Button Push 1 is both LEDs off
Button Push 2 is both LEDs on
Button Push 3 is alternating LEDs
Button Push 4 is Blinking together
Button Push 5 is a 3 blink alternating LED pattern
My "problem" is when it is at the 5th button push you have to either hold the button down or push it multiple times before it cycles to the next function.
I know the problem is really the 3 blink alternating LED pattern, I don't know how to make this pattern without using delays and the delays make it not register the button push.
I'm watching some videos on arrays and State Machines now, I'm sure I will have questions right now I'm trying to learn what they are and come up with a idea on how to redo my code
After the button is pressed again it will reset to the beginning
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if(buttonPushCounter==6){
buttonPushCounter=1;
Serial.print("number of button pushes:");
Serial.println(buttonPushCounter);
}
I had also tried a variation with these changes but it didn't help
void blinkLED3() {
static unsigned long LED3millis = millis();
if (millis() - LED3millis > 450) {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(450); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(150); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
LED3millis = millis();
}
}