Here is some code that someone was gracious enough to help me with running an LED in an off, dimmed, and bright state depending on the digital inputs off both a slide switch and a push button. This code also includes debouncing information.
Please note: I didn't write it in the code, but you will see in the analogWrite lines that the LED is connected to pin 9.
Basic explanation of operation:
If the slide switch is turned on, the turn the LED on dimmed
If the button is pressed while the slide switch is turned on, turn the LED on full power
If the button is released while the slide switch is turned on, return the LED to the dimmed state
If the slide switch is turned off, turn the LED off
If the button is pressed while the slide switch is turned off, turn the LED on to full power
if the button is released while the slide switch is turned off, turn the LED off
Now, here's the code on a silver platter
(I like silver platters. Someone gave me a silver platter and now I clone it (copy and paste) and give one to you!):
// constants won't change. They're used here to
// set pin numbers:
const int BUTTONpin = 12; // the number of the pushbutton pin
const int SWITCHpin = 7; // the number of the pushSWITCH pin
// Variables will change:
int BUTTONstate; // the current BUTTONreading from the input pin
int lastBUTTONstate = LOW; // the previous BUTTONreading from the input pin
long lastBUTTONDebounceTime = 0; // the last time the output pin was toggled
int SWITCHstate; // the current SWITCHreading from the input pin
int lastSWITCHstate = LOW; // the previous SWITCHreading from the input pin
long lastSWITCHDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 100; // the debounce time; increase if the output flickers
void setup() {
pinMode(BUTTONpin, INPUT);
pinMode(SWITCHpin, INPUT);
}
void loop()
{
getSWITCHreading();
getBUTTONreading();
//SWITCHstate is the current state of the SWITCH
//BUTTONstate is the current state of the BUTTON
if(SWITCHstate==1) //if SWITCH is on
{ //and
if(BUTTONstate == 1) //if BUTTON is pressed
{ //then
analogWrite(9, 255); //LED Full Power
} //but
else //if BUTTON not pressed
{ //then
analogWrite(9, 50); //LED Dimmed
}
}
else //or if switch is off
{
if(BUTTONstate == 1) //if BUTTON is pressed
{ //then
analogWrite(9, 255); //LED Full Power
} //otherwise
else //if BUTTON not pressed
{ //then
analogWrite(9, 0); //LED Off
}
}
}
//This is all the debouncing stuff...
void getSWITCHreading()
{
// read the state of the switch into a local variable:
int SWITCHreading = digitalRead(SWITCHpin);
// check to see if you just pressed the SWITCH
// (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 (SWITCHreading != lastSWITCHstate) {
// reset the debouncing timer
lastSWITCHDebounceTime = millis();
}
if ((millis() - lastSWITCHDebounceTime) > debounceDelay) {
// whatever the SWITCHreading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
SWITCHstate = SWITCHreading;
}
// save the SWITCHreading. Next time through the loop,
// it'll be the lastSWITCHstate:
lastSWITCHstate = SWITCHreading;
}
void getBUTTONreading()
{
// read the state of the switch into a local variable:
int BUTTONreading = digitalRead(BUTTONpin);
// 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 (BUTTONreading != lastBUTTONstate) {
// reset the debouncing timer
lastBUTTONDebounceTime = millis();
}
if ((millis() - lastBUTTONDebounceTime) > debounceDelay) {
// whatever the BUTTONreading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
BUTTONstate = BUTTONreading;
}
// save the BUTTONreading. Next time through the loop,
// it'll be the lastBUTTONstate:
lastBUTTONstate = BUTTONreading;
}
Notice the use of if and else in pairs.
If statement is true, then do this, otherwise (else) do this instead.
Hope this helps your understanding!