I have been working on a little project controlling LEDs.
Here is what I have, an Arduino Pro mini. I have one LED on pin 10, another LED on pin 11, and a pushbutton on pin 12.
Wiring, I have a 10K ohm resistor from ground to pin 12 and the push button going from pin 12 to VCC.
The basic premise is turn on. set "stage" = 0. Press Push button, "stage" = 1. Press Push button, "stage" = 2. Press Push button, "stage" = 0, and just keeps looping.
Stage = 0, led 1 does stuff, led 2 off
Stage = 1, led 1 does other stuff, led 2 off
Stage = 2, led 2 does stuff, led 1 off
I copied the "debounce" code from the examples and modified it a little to count instead of just toggle. But here is the issue. when I hit the button, sometimes it will swap to the next stage, but most times it won't.
I really need some help to get this figured out. I have included the entire sketch.
The LED portion works just fine. It is the stepping through that is the issue.
I am open to any suggestions.
Thank you in advance for your help.
/*
Fading
Created by Vader
22 July 2015
*/
const int buttonPin = 12; // Mode switch
int stage; // Mode stage 0 = solid, 1 = yellow fade, 2 = color fade
int lastButtonState = LOW;
int buttonState;
byte ledPinY = 10; // LED connected to digital pin 9
byte ledPinC = 11; // LED connected to digital pin 10
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 10; // the debounce time; increase if the output flickers
void setup() {
// Pin Setup
pinMode(ledPinY, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(buttonPin, INPUT);
stage = 0;
}
void loop() {
// Button debouce control
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
/*
if (reading == 1 && stage < 2) {
stage = stage + 1;
}
if (reading == 1 && stage ==2) {
stage = 0;
}
*/
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only change STAGE if the new button state is HIGH
if (buttonState == HIGH) {
if (stage < 2) {
stage = stage + 1;
}
else {
stage = 0;
}
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
// LED flash control
/*
if (stage == 0) {
analogWrite(ledPinY, 255); // Yellow soild on
analogWrite(ledPinC, 0); // Colored off
}
if (stage == 1) {
analogWrite(ledPinY, 0); // Yellow soild on
analogWrite(ledPinC, 255); // Colored off
}
if (stage == 2) {
analogWrite(ledPinY, 255); // Yellow soild on
analogWrite(ledPinC, 255); // Colored off
}
*/
// Yellow LED solid on
if (stage == 0) {
analogWrite(ledPinY, 255); // Yellow soild on
analogWrite(ledPinC, 0); // Colored off
}
//Yellow LED fade in and out
if (stage == 1) {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPinY, fadeValue);
analogWrite(ledPinC, 0);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPinY, fadeValue);
analogWrite(ledPinC, 0);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
// Color LED fade in and out
if (stage == 2) {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPinC, fadeValue);
analogWrite(ledPinY, 0);
// wait for 30 milliseconds to see the dimming effect
delay(15);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPinC, fadeValue);
analogWrite(ledPinY, 0);
// wait for 30 milliseconds to see the dimming effect
delay(15
);
}
}
}