Hello,
I am struggling to write this code working.
I imagine this code can be written much simpler and clean way. but I can't really figure out why it is not working.
Basically what I am trying to do is:
if you press the button 2 and 3, a LED is only on while one of the buttons is pressed.
but when you press button 4, LED stays on even though the button is released until you press button again.
and I would like to add denounce function on the button 4.
any advise or tip would be grateful.
thanks
int ledPin = 13; // LED is connected to pin 12
int switchPin = 2;
int switchPin2 = 3;
int switchPin3 = 4;
int val;
int val2;
int check1;
int dbcheck1;
int button3State ; //button three state
int lightMode = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
digitalWrite(ledPin, LOW);
button3State= digitalRead(switchPin3);
}
void loop(){
val = digitalRead(switchPin);
val2 = digitalRead(switchPin2);
check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);
// read input value and store it in val
if (val == LOW || val2 ==LOW ) { // check if the button is pressed
digitalWrite(ledPin, LOW); // turn LED on
}
if (val == HIGH || val2 ==HIGH) { // check if the button is not pressed
digitalWrite(ledPin, HIGH); // turn LED off
}
check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);
// read the input again to check for bounces
if (check1 == dbcheck1) { // make sure we got 2 consistant readings!
if (val != button3State) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // is the light off?
lightMode = 1; // turn light on!
digitalWrite(ledPin, HIGH);
} else {
lightMode = 0; // turn light off!
digitalWrite(ledPin, LOW);
}
}
}
button3State = val; // save the new state in our variable
}
}
Sorry I must have massed up with my other code.
this was the final code I tried.
still not working
all the inputs have been defined. but still same.
int ledPin = 13; // LED is connected to pin 12
int switchPin1 = 2;
int switchPin2 = 3;
int switchPin3 = 4;
int val;
int val2;
int check1;
int dbcheck1;
int button3State ; //button three state
int lightMode = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT); // Set the switch pin as input
digitalWrite(ledPin, LOW);
button3State= digitalRead(switchPin3);
}
void loop(){
val = digitalRead(switchPin1);
val2 = digitalRead(switchPin2);
check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);
// read input value and store it in val
if (val == LOW || val2 ==LOW ) { // check if the button is pressed
digitalWrite(ledPin, LOW); // turn LED on
}
if (val == HIGH || val2 ==HIGH) { // check if the button is not pressed
digitalWrite(ledPin, HIGH); // turn LED off
}
// read the input again to check for bounces
if (check1 == dbcheck1) { // make sure we got 2 consistant readings!
if (check1 != button3State) { // the button state has changed!
if (check1 == LOW) { // check if the button is pressed
if (lightMode == 0) { // is the light off?
lightMode = 1; // turn light on!
digitalWrite(ledPin, HIGH);
} else {
lightMode = 0; // turn light off!
digitalWrite(ledPin, LOW);
}
}
}
button3State = val; // save the new state in our variable
}
}
if (val == LOW || val2 ==LOW ) { // check if the button is pressed
digitalWrite(ledPin, LOW); // turn LED on
}
if (val == HIGH || val2 ==HIGH) { // check if the button is not pressed
digitalWrite(ledPin, HIGH); // turn LED off
}
The logic in this section doesnt add up..
Rethink, rewrite...
if you press the button 2 and 3, a LED is only on while one of the buttons is pressed.
This isnt very clear..
Can you describe , step by step, how you want this to work..:
What conditions must be present for led on?
What cond..... to keep it on?
What cond... to switch it off ?
Should action happen at key press , key release or while key is down
I ran your code..: it worked the way you said it should.
pin 2 and/or 3 -> led on while presses
pin 4 -> toggle led for each press
I had all inputpins open - then tried to ground them one by one.
Have you tried using the internal pull up resistors:
pinMode(switchPin, INPUT_PULLUP);
It's possible that the pin voltage is floating enough to confuse the arduino. They should definitely read Low if the switch connects them to ground, though.
You can also check to see what Arduino is seeing by using
Serial.println(digitalRead(b2));
That will help narrow down the problem between your logic programming and the pins.
All pins read HIGH when I read them. I agree - use pullups!
If your environment is electrically noisy, use external pullups <=10k.
My voltage measures 4.75 .. 4.8 V
Hi, can you post a picture of your project and a CAD of your circuit in jpg, png or pdf, or a picture of hand drawn circuit diagram please.
Hope to help.. Tom.....
..There is noting to draw...: Naked board !
I have used a jumper (grounded) which I'v uses to touch input_pins 2,3 and 4.
As I touch pin 2 or 3 -> led lights up, but as soon as I remove from pin. ->darkness!
Repeated touch to pin 4 toggles led.
Hi, what do you mean by naked board?
Can you post a picture of your project please.
"Naked board" do you mean shield on a Arduino UNo, or have you assembled a bare bones arduino borad.
Prefer a picture please.
The fact that knut_ny can get your sketch to run okay, means you may have a layout problem.
Tom......