Hello everybody! So I'm pretty newbie in eletronics in general, and I'm trying to make a little project however I'm facing some troubles..
So what I want to do is basicly activate a LED (park) 2 seconds after the arduino receive an input in pin 5 (pisca), so I've tought in making a timer, that is set to 0 everytime the pin 5 receives an input.
When the timer reaches 2000ms (2 seconds) it enters in the correspondent "if" and turns the LED on, however when compiling this code to arduino it keeps blinking at each 2 seconds...
const int park = 12;
const int pisca = 5;
unsigned long previousMillis = 0;
const long timer = 2000;
int state; // THIS VARIABLE CALLED STATE IS USED TO READ A BUTTON STATE
void setup()
{
pinMode(park, OUTPUT);
pinMode(pisca, INPUT);
state = 0;
}
void loop()
{
unsigned long currentMillis;
state = digitalRead(pisca);
if (state == HIGH)
{
digitalWrite(park, LOW);
currentMillis = millis();
}
if (state == LOW && currentMillis - previousMillis >= timer)
{
digitalWrite(park, HIGH);
previousMillis = currentMillis;
}
}
I've spent couple of hours already but can't figure how to do this, so a help would be very appreciated!!
If you connect button to "pisca" pin, you should use pull-up/or pull-down resistor. See this post Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum
I've used a physical resistor to do the job, thanks anyways for the advice!
There are two problem in your code.
-
You should put currentMillis = millis(); outside of if(){}
-
To detect "the pin 5 receives an input", Instead of checking the current state only, you should detect the change from HIGH to LOW (in case of using pull-up resistor), or LOW to HIGH (in case of using pull-down resistor). See this tutorial
You should consider 3 LED states: Off. Waiting and On. Then you can control better when the state should change from On to Off again.
Why not use the free and already existing internal pull up resistor, connecting the button to GND and to the input? Do have shares in the resistor making industri? I'm joking.
Hello!
So I've followed some of your tips and I've done this and it's working exactly how I want it to work, I just want to make sure there's no errors in the code, let me note that when the button is not pressed the light "TX" in the arduino nano keeps constantly on, is that a problem? The sentence "park activated" keeps being spammed lots of times per senconds too. Should I modify something?
const int BUTTON_PIN = 6;
const int park = 2;
int lastState = LOW;
int botao;
const long timer = 1000;
unsigned long counter;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(park, OUTPUT);
}
void loop() {
botao = digitalRead(BUTTON_PIN);
unsigned long currentMillis;
currentMillis = millis();
if(botao == LOW)
{
if(lastState == HIGH)
{
Serial.println("The button is pressed");
digitalWrite(park, LOW);
}
Serial.println(counter);
counter = currentMillis;
Serial.println(counter);
}
if(lastState == LOW && botao == HIGH && currentMillis - counter >= timer)
{
Serial.println("The button is released");
lastState = botao;
}
if(currentMillis - counter >= timer)
{
Serial.println("Park activated");
digitalWrite(park, HIGH);
}
}
MiguelEduardo:
when the button is not pressed the light "TX" in the arduino nano keeps constantly on, is that a problem? The sentence "park activated" keeps being spammed lots of times per senconds too.
Those things are closely related: the TX light is lit as "Park activated" is sent again and again.
If you don't touch the button, after 1000mS, currentMillis-counter will be greater than timer, so your final if will be executed on every iteration of loop, causing the spamming you see.
Perhaps you should set a flag when you have sent "Park activated" and use it to avoid doing so until a button has been pressed.
Those things are closely related: the TX light is lit as "Park activated" is sent again and again.
If you don't touch the button, after 1000mS, currentMillis-counter will be greater than timer, so your final if will be executed on every iteration of loop, causing the spamming you see.
Perhaps you should set a flag when you have sent "Park activated" and use it to avoid doing so until a button has been pressed.
I've resolved the problem by using a "==" instead of ">=" in the park activated part, but it keeps sending around 5 or 6, it's not a problem at all, I think, at least the light in Nano turns off...
But again, if I keep the finger in the button it spams the "Button pressed" mensage thousands of times, shouldn't it just send one and then another one when released?
lastState = botao;
You have this assignment in the wrong place burried in a release condition. To be responsive to state change it needs to constantly updated to the current state. Delete the line from the current location
if (lastState == LOW && botao == HIGH && currentMillis - counter >= timer)
{
Serial.println("The button is released");
//lastState = botao;
}
and place it as the first line of loop().
void loop() {
lastState = botao;//will take on the value of the previous reading
botao = digitalRead(BUTTON_PIN);
Now, we’re past all that, think about a rewrite that not only includes what you’ve learned, but also consider detecting when the button changes state, not just if it is pressed or not.
This will give you more flexibility in the future to add features.
Remember to look at debouncing the switch contacts in some way.