Three LEDs control by one momentary switch.
The Spec:
1/ Hold the switch Red LED on, other two LEDs Green & Blue off.
2/ Click the switch toggles LEDs Green & Blue on, LED Red off.
Below two Sketches work fine! One sketch toggles LEDs Green & Blue light by momentary short click, other sketch lights up LED Red, other LED off by long hold; Short click LED Red off, other LED on .
Third one was combined two sketches. It does meet Spec 1/, but does not toggle LEDs Green & Blue. Can someone help to fix so both specs are met?
Thank you for your attention, Your help is greatly appreciated!!
***************************************************************************************
#1
// Three leds, one momentary switch
// Two Leds are toggled on by click, third one off.
// Third led on by hold, other two leds off.
float pressLength = 0;
int optionOne = 100;
int optionTwo = 2000;
int buttonPin = 2;
int ledPin_Option_1 = 13;
int ledPin_Option_2 = 12;
int ledPin_Option_3 = 11; // New add
int val; // New add
int val2; // New add
int buttonState; // New add
int Mode = 0; // New add
void setup(){
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin_Option_1, HIGH);
pinMode(ledPin_Option_2, HIGH);
pinMode(ledPin_Option_3, HIGH); // New add
buttonState = digitalRead(buttonPin); // New add
digitalWrite(ledPin_Option_2, HIGH);
Serial.begin(9600);
}
void loop() {
while (digitalRead(buttonPin) == LOW ){
delay(100);
pressLength = pressLength + 100;
Serial.print("ms = ");
Serial.println(pressLength);
}
if (pressLength >= optionTwo){
digitalWrite(ledPin_Option_1, LOW);
digitalWrite(ledPin_Option_2, HIGH);
}
else if(pressLength >= optionOne){
digitalWrite(ledPin_Option_2, LOW);
val = digitalRead(buttonPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(buttonPin); // 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 (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 0;
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (Mode == 0) { // all-off
digitalWrite(ledPin_Option_1, HIGH);
digitalWrite(ledPin_Option_3, LOW);
}
if (Mode == 1) {
digitalWrite(ledPin_Option_1, LOW);
digitalWrite(ledPin_Option_3, HIGH);
}
}
pressLength = 0;
}
***************************************************************************************
#2
// Two leds light toggle by momentary switch click.
int buttonPin = 2; // switch is connected to pin 2
int ledPin_Option_1 = 13;
int ledPin_Option_3 = 11;
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 Mode = 0; // What mode is the light in?
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the switch pin as input
pinMode(ledPin_Option_1, OUTPUT);
pinMode(ledPin_Option_3, OUTPUT);
buttonState = digitalRead(buttonPin); // read the initial state
}
void loop(){
val = digitalRead(buttonPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(buttonPin); // 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 (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 0;
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (Mode == 0) { // all-off
digitalWrite(ledPin_Option_1, LOW);
digitalWrite(ledPin_Option_3, HIGH);
}
if (Mode == 1) {
digitalWrite(ledPin_Option_1, HIGH);
digitalWrite(ledPin_Option_3, LOW);
}
}
***************************************************************************************
#3
/ Two leds light on/off toggle. One by click, other by hold.
float pressLength_milliSeconds = 0;
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;
int buttonPin = 2;
int ledPin_Option_1 = 13;
int ledPin_Option_2 = 12;
void setup(){
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin_Option_1, HIGH);
pinMode(ledPin_Option_2, HIGH);
Serial.begin(9600);
}
void loop() {
while (digitalRead(buttonPin) == LOW ){
delay(100);
pressLength_milliSeconds = pressLength_milliSeconds + 100;
Serial.print("ms = ");
Serial.println(pressLength_milliSeconds);
}
if (pressLength_milliSeconds >= optionTwo_milliSeconds){
digitalWrite(ledPin_Option_1, LOW);
digitalWrite(ledPin_Option_2, HIGH);
}
else if(pressLength_milliSeconds >= optionOne_milliSeconds){
digitalWrite(ledPin_Option_1, HIGH);
digitalWrite(ledPin_Option_2, LOW);
}
pressLength_milliSeconds = 0;
}