Hello,
I am learning Arduino along with my two boys (trying to get them into electronics). We are trying to get a bush button event to open a circuit to control (shut off) a relay and turn on another relay for a specific amount of time (say 12seconds) then automatically switch back to normal.
My son came up with this but its not working as we would like.
Would one of you be so kind to review it and correct the issue?
const int ledPin = 2; // the number of the LED pin
const int buttonPin = 3; // Button pin number
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
int buttonState = 0; // Checks if button is pushed
int buttonPush = 0; // Toggles between 0 to hold state
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonPush = 1;
if (buttonPush == 1){
if(currentMillis - previousMillis > interval) {
previousMillis = millis();
digitalWrite(ledPin, HIGH);
}
else {
buttonPush = 0;
}
}
else {
digitalWrite(ledPin, LOW);
}
}
}
buttonPush = 1;
if (buttonPush == 1){
You've just set it to one.
How could it have any other value?
Allow me introduce you to the built in constants, "true" and "false". Use them instead of "0" and "1". It will make your code more readable.
One thing is that all the time variables should be unsigned. I know they are not in the blink without delay example but that is a wrong that will take weeks of running to show up. You may say "so what? I only run it for less than a day!" but then you make a habit and some day it will bite you and you may spend a very long time figuring out why and how.
You may be having trouble with contact bounce of the button. Your loop() runs more than fast enough to see every button press as many button presses, what we call bounce.
You can code around bounce or you can add a 1 uF capacitor across the button leads and no more bounce.
What I do to debounce in software is to watch the pin change state and only when it stays the same for usually 2 to 5 millis does my code set a variable buttonPush (as opposed to pin state) to 0 or 1.
Inside of loop() I separate the code for the button from the code for the relays. I use a global variable (buttonPush) to pass information from button code to relay code. It makes debugging easier and I can replace the code for button or relay with something else easier too.
Assuming the button is debounced in hardware (capacitor across the leads), not in software.
const byte ledPin = 2; // the number of the LED pin
const byte buttonPin = 3; // Button pin number
unsigned long previousMillis = 0; // will store last time LED was updated
byte buttonPush = 0; // Toggles between 0 to hold state
unsigned long interval; // interval at which to blink (milliseconds), starts as 0
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT); // the pin will be LOW by default
pinMode(buttonPin, INPUT);
}
void loop()
{
// button code for button debounced in hardware is very simple.
if ( digitalRead(buttonPin) ) // a return of 0 is false, non-0 is true, no need for ( val == 1 )
{
buttonPush = 1;
}
else
{
buttonPush = 0;
}
// led code because now there are not two relays so the led stays on for 1 second
if ( interval ) // will be true if interval is not 0
{
if ( millis() - previousMillis >= interval ) // if time is up then turn the led off and stop checking
{
digitalWrite( ledPin, LOW );
interval = 0;
}
}
else // interval is 0 at this point
{
if ( buttonPush ) // if buttonPush is not 0 then set up the timer and light the led
{
previousMillis = millis();
interval = 1000;
digitalWrite( ledPin, HIGH );
}
}
}