Okay, so I've just gotten an Arduino Uno to add to my collection of gizmos laying around. I took C++ about 6 years ago, then never touched it again so I'm basically starting at zero on my programming. I'm running through various examples online, both on the site here and on J. Blum's YouTube. My current project is just some extra credit for myself so that I gather a better understanding of things again.
My idea is to have one LED come on and off with the push of a switch, and a second LED have 5 varying brightness settings. I'd like the variable LED to increase in brightness with each push of the button, then go off once I've reached the PWM max. I've been able to get it to brighten and dim and go off, but never in my expected 6 clicks of the button. I'm thinking that either A) I don't truly understand the debounce function of the code and which variable to pass to my PWM led, or B) that I need to utilize some sort of counter on the button pushes and use that as a multiplier for my increasing brightness variable. Any help would be great. I'm at a bit of a road block and the misses is getting a bit frustrated with how much time I'm geeked out on my laptop. ![]()
The area I'm stuck with is towards the bottom of my code and is lacking all my overly gratuitous comments.
Here's my code:
/*
/*
Switch debounce via software code. This idea was from Jeremy
Blums YouTube Channel.
Chris Cronan
Arduino Uno
*/
// Global variables
int switchPin = 8; // Initializes an integer global variable named 'switchPin' on digital pin 8 of the Uno
int ledPin = 13; // Initializes an integer global variable named 'ledPin' on digital pin 13 of the Uno
int pwmPin = 3; // Initializes PWM pin 3 as the variable 'pwmPin'
int brightness = 0; // Initializes the variable 'brightness' and sets it to equal 0
int brightLED = 51; // Initializes the variable brightLED and sets it equal to 51
// Global variable used for removing the bounce from the switch push
boolean lastButton = LOW; // Initializes a true/false, high/low, on/off global variable named 'lastButton' and sets it to LOW
boolean ledOn = false; // Initialzes a boolean globabl variable of 'ledOn' and sets it to fasle/off/low
boolean currentButton = LOW; // Initializes a boolean global variable of 'currentButton' in the off state
void setup()
//Only ran once
{
pinMode (switchPin, INPUT); // Sets pin 8 'switchPin' as an input pin
pinMode (ledPin, OUTPUT); // Sets pin 13 'ledPin' as an output pin
pinMode (pwmPin, OUTPUT);
}
boolean debounce (boolean last) // creates a boolean logic program to negate the bouncing of the switch when pressed
{
boolean current = digitalRead (switchPin); // Initializes a variable of 'current' and sets it to equal the read input of the 'switchPin' on pin 8
if (last != current) // if the last state of the button is not equal to the current state of the button, read on switchPin
{
delay (5); // wait 5 milliseconds
current = digitalRead (switchPin); // then read the current state of the switchPin again and set it to equal variable 'current'
}
return current; // now pass the variable current back to the beginning of the function to be set equal to the currently
}
// ends the debounce function
void loop()
{
// Turns and LED on and off with a software based captive relay
currentButton = debounce(lastButton); // sets 'currentButton' equal to the cleaned up last button push
if (lastButton == LOW && currentButton == HIGH) // if the 'lastButton' is equal to LOW, and the 'currentButton' is equal to HIGH
{
ledOn = !ledOn; // change the state of the variable 'ledOn' to the opposite of its current state
}
lastButton = currentButton; // now set the 'lastButton' push to equal the 'currentButton' state
digitalWrite (ledPin, ledOn); // then write the 'ledPin' (13) to the proper state as passed in the variable 'ledOn'
// Modulates the brightness of an LED with PWM on PWM pin 3, changed with each button push
if (currentButton == HIGH)
{
brightness = brightness + brightLED; // Increases the brightness of the pwm led by the amount defined in the variable 'brightLED'
}
if (brightness > 255) brightness = 0;
analogWrite (pwmPin, brightness);
}
// end of sketch