Hi all,
i'm trying to program 2 led that turns on when i press button 1 or button2. Each led for each button. When i press a button i want also the led to blink. And here i met the first problem: led are turning on and off if i press buttons, but no blink. Then i wanted to set the 2 led off if i press the 2 buttons at the same time, but i could't make this working.. i know is something with code and i think all connection to breadboard are right. Help please =(
int led=8;
int led2=9;
int val=0;
int val2=0;
int button=5;
int button2=2;
void setup(){
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(button,INPUT);
pinMode(button2,INPUT);
}
void loop(){
val=digitalRead(button);
val2=digitalRead(button2);
//Turn on led
if(val==HIGH){
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led,LOW);}
else{digitalWrite(led,LOW);
}
//Turn on led2
if(val2==HIGH){
digitalWrite(led2,HIGH);
delay(500);
digitalWrite(led2,LOW);}
else{digitalWrite(led2,LOW);}
//if both button pressed
if(val==val2){
digitalWrite(led,LOW);
digitalWrite(led2,LOW);
}
else{digitalWrite(led,LOW);
digitalWrite(led2,LOW);}
this is a nice little project with lots of learning possibilities.
There are several things wrong with your code, but instead of correcting them for you, i would suggest a couple of things.
The first thing would that you take a look at the "BlinkWithoutDelay" Example on your IDE or take a look at this page:
The thing here is that the delay() function (which you are using several times) works like a "pause" on your sketch. this means that while one of the delays is going on, nothing else can happen (no button reads, no turning leds on or off,...).
The BlinkWithoutDelay, like the name tells, explains a nice technic to achieve a delay effect without using the delay() function. Very useful!
Even though you are using only 2 leds and 2 buttons (i am sorry if i am wrong, bu i am assuming that you are doing it to learn something...), i would also encourage you to read about Arrays.
Arrays are like lists of things. For instance, when you are making a shopping list you don't take a paper and write "butter", another paper to write "milk" and another paper to write "eggs". You take ONE paper (your list) where you will write all the elements you want.
The same could happen with your LEDs. instead of having them separate, you could create an array (a "list") and put all your leds inside. maybe another array for the buttons,...
Here is a nice page about this topic:
Another idea.
What you are doing now with your code is something like if the button is pressed "bla bla bla".
But do you actually mean the "bla bla bla" to happen all the time when the button is pressed (what would happen if the button is pressed for 5 minutes?) or do you want it to happen when the button "becomes" pressed (the moment when it went from "not being pressed" to "being pressed")?
For this there is something called State Change Detection. It's not complicated but it is very usefull.
You can learn about it here:
another thing i would advise you to try, would be to write a separate function for the "blinking of the leds".
Like this, when you want an LED to blink, instead of having to write the code for it, you just need to "call" the blink function (which will have all the blinking information/code) for that specific LED.
if would maybe look something like this:
void setup() {
// your setup here
}
void loop() {
// all your loop code here
// when a button is pressed:
blink (ledx); // this will run the function "blink" to the "ledx" LED
}
//function to blink the leds
void blink (ledToBlink) {
// your blink code here
digitalWrite(ledToBlink,HIGH)
//and so on. Try to use the Blink Without Delay ideas... ;)
}
Hmmm, i realize this is a lot of info just to make two leds blink, but i guess you will learn some cool basic stuff which you then allow you to program more complicated things!
OK first thanks for your advices they are really helpful for me! Now i've learned something with arrays and i see it's quite quicker to program with them! So i rewrote the code with arrays so it come back something like this
int led[]={8,9};
int button[]={5,2};
int val;
void setup(){
for(int i=0;i<2;i++){
pinMode(led[i],OUTPUT);
pinMode(button[i],INPUT);
}
}
void loop(){
for(int i=0;i<2;i++)
{
val=digitalRead(button[i]);
//Turn on led
if(val==HIGH){
digitalWrite(led[i],HIGH);
}
else{
digitalWrite(led[i],LOW);
}
}
}
and then i didn't get what you mean with press both buttons.What I mean is: if i PRESS on them and status of buttons change both on HIGH i want leds to turn completly off instead of turning both on! Again really thanks for you help and for your patience on me!
what i was trying to tell you about the button press is that if you use something like:
if(val==HIGH){
digitalWrite(led[i],HIGH);
}
then you that will be true the whole time you have your finger on the button. That is different than just making it true when you PRESS the button.
Let me give you an example to try to explain the difference.
Lets say you have a variable called V. You want this variable to store how make times you have pressed the button, so you can write a code and each time the button is pressed you can cay that V++ (which mean V = V + 1).
What would happen with your code is that each time you would press the button the (val == HIGH) would be true many times (because the arduino runs your loop MANY times in a second) so your val would increase a lot, not just one.
So what you need to do is to find out the moment when the button BECOMES pressed.
Check that State Change Detection page, they explain it quite well...
I think you should have an array also for the val, so you can store the value of each led.
It may help you to use something like
const int led[]={8,9};
const int button[]={5,2};
int ledState=LOW;
int buttonpushcounter=0;
int buttonstate=0;
int lastbuttonstate=0;
long previousMillis=0;
long interval=1000;
void setup(){
for(int i=0;i<2;i++){
pinMode(led[i],OUTPUT);
pinMode(button[i],INPUT);
Serial.begin(9600);
}
}
void loop(){
//ciclo Blink-ledState
for(int i=0;i<2;i++){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// salva l'ultima volta che il led si è acceso
previousMillis = currentMillis;
// se il led è acceso e vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
buttonstate=digitalRead(button[i]);
//se il bottone viene premuto aumenta il counter
if(buttonstate !=lastbuttonstate){
if (buttonstate=HIGH){
buttonpushcounter++;
}
else{
}
}
lastbuttonstate=buttonstate;
if(buttonpushcounter %1==0){
digitalWrite(led[i],ledState);
}
else{
digitalWrite(led[i],LOW);
}
}
}
}
So basically i tryed to put togheter the blinking code and counter also.. I dont have time to check if it's working or not. I tryed to listen to all of you suggestion.. the only problem is with the debounce code. i don't know where to set it. Thanks again for help guys !