Hi everyone. I cant figure out how can I run 3 leds by line with ONE Pushbutton. Any help would be cool.
First, all leds should be off. (lets call led1, led2, led3)
One press to pushbutton should turn on led1, others will be remain off.
When second press, turn on led2 and rest(1,3) should be off.
When third press, turn on led3 and rest(1,2) should be off.
I did it with one led but couldnt with 3. Could you help me please?
Here you can see the circuit and code I made. I think circuit is ok. But code is not ok ofcourse because I dont know how can I add second and third properly
//this code is not working well. something is wrong.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPina = 13; // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPina, OUTPUT);
pinMode(ledPinb, OUTPUT);
pinMode(ledPinc, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPina, HIGH);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPinb, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPinc, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
}
//this code is not working well. something is wrong.
You need to look at the state change detection example. It shows how to count switch presses.
Then, you can do something like:
switch(pressCount)
{
case 0: // never been pressed; all LEDs should be off
digitalWrite(led_a, LOW);
digitalWrite(led_b, LOW);
digitalWrite(led_c, LOW);
break;
case 1: // first press
digitalWrite(led_a, HIGH);
digitalWrite(led_b, LOW);
digitalWrite(led_c, LOW);
break;
}
You can easily add the other two cases.
You will need to decide what happens when you press the switch for the 4th, and subsequent, times.
Hi again. I tried but failed.
It fails because:
pressing 2 times makes led_a on, pressing 3 times makes led_b on, pressing 4 times makes led_c on.
Sounds good until now. But when pressing 4 times it makes led_c on (good ) and also led_a on (bad ) because: 4 = 2+2 and it makes led_a + led_c on at 4.press... loops fails so on.
//this code is not working well. something is wrong.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPina = 13; // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPina, OUTPUT);
pinMode(ledPinb, OUTPUT);
pinMode(ledPinc, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPina, HIGH);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPinb, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPinc, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
}
//this code is not working well. something is wrong.
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
This assumes that you have an external resistor wired with the switch? Do you? How IS the switch wired?
Until you know that you are reading the switch correctly, the rest of the code does not matter.
Your handling of buttonPushCounter is all wrong, or your understanding of the % operator is. The value on the right needs to be the same in all the statements. What you are comparing the result to will be different.
Hey. Thanks for reply. Yes I connected a resistor to pushbutton. Here is circuit image. I think its ok.
I dont know "buttonPushCounter" or "% operator" functions. I just know primary codes. I found these codes on an example and modified it but it didnt worked.
arduinoist:
Hey. Thanks for reply. Yes I connected a resistor to pushbutton. Here is circuit image. I think its ok.
I dont know "buttonPushCounter" or "% operator" functions. I just know primary codes. I found these codes on an example and modified it but it didnt worked.
Any help would be nice.
//this code is not working well. something is wrong.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPina = 13; // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPina, OUTPUT);
pinMode(ledPinb, OUTPUT);
pinMode(ledPinc, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPina, HIGH);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPinb, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinc, LOW);
}
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPinc, HIGH);
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
}
//this code is not working well. something is wrong.
This says "if the number of switch presses is even...". Is that what you want?
if (buttonPushCounter % 3 == 0) {
This says "if the number of switch presses is a multiple of 3...". Is that what you want?
It is generally far simpler to do something like:
switch(buttonPushCounter)
{
case 0:
// Nothing to do
break;
case 1:
// Light first LED
break;
case 2:
// Light second LED
break;
case 3:
// Light third LED
break;
case 4:
buttonPushCounter = 1;
break;
}
You might want to set back to 0 when it gets to 4, or you might want to set it back to 1. Only you know for sure.