What is the best way to go??
i have a single button (with led) that must run 3 functions
function 1 > button led is off > system off
function 2 > button led fades up/down > system is auto (switches lights on with motion/ off when no motion is present)
function 3 > button led on > system on (lights just stay on permanently)
So do you just want to do those three things with an LED? The fact that it is an illuminanated button is irrelevant.
Look at the LED fade tutorial. The other two cases are just each end of a fade.
You have not said what else you want to do, that will affect how you implement it.
the system needs to run 3 functions with only one button( the illuminated button just for status of mode /looks). if the system boots it's in auto mode if you enter the room it fade's the lights on and holds if you exit (button led fade's up/down) . if I press the button the system is in on mode (button led is on). if the same button is pressed again the system is off(button led off)
how would i tell the program that button is pressed mode1 do not run the rest of the program until button is pressed for 2nd mode and for mode 3 .
how would i tell the program that button is pressed
You haven't shown us your program, but I'd use a variable to keep the current state, and call the appropriate function depending on its value.
Check the switch from time to time to see if it appropriate to change the value of the state variable.
You have only described two button presses and you have three modes.
Do you want a button push to change successively between modes?
If so you have a mode variable, you add one to it each time you detect a button press. Then check this count and if it is greater than three set it back to one.
Get that bit going first, print out the mode variable every time it is changed. When that is going it is then time to think about how you want to indicate the mode but take it one step at a time.
If so you have a mode variable, you add one to it each time you detect a button press.
This means tracking when the state changes. This means keeping track of what the state was.
int currState;
int prevState = LOW; // If HIGH means pressed
int pressCount = 0;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
if(currState == HIGH) // Assumes HIGH means pressed
{
pressCount++;
}
}
prevState = currState;
if(pressCount > 3)
pressCount = 1;
switch(pressCount)
{
case 1:
// Do something
break;
case 2:
// Do something
break;
case 3:
// Do something
break;
}
}
/*
* BoardRoom Light Test Prog
*/
int switchPin = 2; // switch is connected to pin 2
int ledPin = 9;
int Pir = 3;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int lightMode = 0; // What mode is the light in? Auto
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(ledPin, OUTPUT); // Set ledPin as Output
pinMode(Pir, OUTPUT); // Motion detect for Auto fade on/off
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // auto fade
lightMode = 1; // turn lights on!
} else {
if (lightMode == 1) { // if its lights on
lightMode = 2; // make it off!
} else {
if (lightMode == 2) { // if its off
lightMode = 0; // make it auto fade!
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // auto fade - default
fade code with pir sensor;
}
if (lightMode == 1) { // light on
digitalWrite(ledPin, HIGH);
}
if (lightMode == 2) { // light off
digitalWrite(ledPin, LOW);
}
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // auto fade - default
fade code with pir sensor;
}
if (lightMode == 1) { // light on
digitalWrite(ledPin, HIGH);
}
if (lightMode == 2) { // light off
digitalWrite(ledPin, LOW);
}
Is there any possibility that more than one of these statements will evaluate to true on any pass through loop()? If not, is there any reason to evaluate all of them? Stop when you've found the correct one, by making the second two "else if" statements.