I have a very basic circuit setup:
- A button connected to pin 2
- An LED connected to pin 9
Desired Functionality:
When the button is pressed, the user can toggle through 4 LED patterns.
The Problem:
Once the first fade pattern begins, the user has to wait until it has finished running before the button can toggle to the next pattern. What I want is that the button push will move to pattern 2, even if pattern 1 has already started.
Attempted Solutions:
I've tried to insert a while function in a couple of places i.e while buttonstate =lastbuttonstate, but that doesn't seem to work.
Here's the code: (I realize all the fadeloops are the same right now, they will different in the working model)
//PROBLEM: The fade function has to complete a full loop before allowing toggle to next one
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 9; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int brightness = 0; // how bright the LED is
int firstFadeAmount = 5; // how many points to fade the LED by
int secondFadeAmount = 5; // how many points to fade the LED by
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState = buttonState;
while (buttonPushCounter = lastButtonState) {
if (buttonPushCounter == 1 && buttonState == HIGH) {
fadeLoop1();
delay(50);//JP Edit
}
else if (buttonPushCounter == 2 && buttonState == HIGH) {
fadeLoop2();
delay(50);
}
else if (buttonPushCounter == 3 && buttonState == HIGH) {
fadeLoop3();
delay(50);
}
else if (buttonPushCounter == 4 && buttonState == HIGH) {
fadeLoop4();
delay(50);
}
}
if (buttonPushCounter == 4) { //Resets back to zero after 4 presses
buttonPushCounter = 0;
}
}
void fadeLoop1()
{
for (int i = 0; i < 100; i++) { //Set the length of the first fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + firstFadeAmount;
if (brightness <= 0 || brightness >= 255) {
delay(1000); //Sets the hold at the top and bottom of the cycle
firstFadeAmount = -firstFadeAmount;
}
delay(100);
}
for (int i = 0; i < 100; i++) { //Set the length of second fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + secondFadeAmount;
if (brightness <= 0 || brightness >= 255) {
secondFadeAmount = -secondFadeAmount;
}
delay(30);
}
digitalWrite(ledPin, LOW);
}
void fadeLoop2()
{
for (int i = 0; i < 100; i++) { //Set the length of the first fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + firstFadeAmount;
if (brightness <= 0 || brightness >= 255) {
delay(1000); //Sets the hold at the top and bottom of the cycle
firstFadeAmount = -firstFadeAmount;
}
delay(100);
}
for (int i = 0; i < 100; i++) { //Set the length of second fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + secondFadeAmount;
if (brightness <= 0 || brightness >= 255) {
secondFadeAmount = -secondFadeAmount;
}
delay(30);
}
digitalWrite(ledPin, LOW);
}
void fadeLoop3()
{
for (int i = 0; i < 100; i++) { //Set the length of the first fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + firstFadeAmount;
if (brightness <= 0 || brightness >= 255) {
delay(1000); //Sets the hold at the top and bottom of the cycle
firstFadeAmount = -firstFadeAmount;
}
delay(100);
}
for (int i = 0; i < 100; i++) { //Set the length of second fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + secondFadeAmount;
if (brightness <= 0 || brightness >= 255) {
secondFadeAmount = -secondFadeAmount;
}
delay(30);
}
digitalWrite(ledPin, LOW);
}
void fadeLoop4()
{
for (int i = 0; i < 100; i++) { //Set the length of the first fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + firstFadeAmount;
if (brightness <= 0 || brightness >= 255) {
delay(1000); //Sets the hold at the top and bottom of the cycle
firstFadeAmount = -firstFadeAmount;
}
delay(100);
}
for (int i = 0; i < 100; i++) { //Set the length of second fading cycle
analogWrite(ledPin, brightness);
brightness = brightness + secondFadeAmount;
if (brightness <= 0 || brightness >= 255) {
secondFadeAmount = -secondFadeAmount;
}
delay(30);
}
digitalWrite(ledPin, LOW);
}