Hi guys I posted awhile back on a project I was doing and thanks to all the input I took a few free classes and learned a few things :-).
I now have a somewhat working program but it's not working as I intended it to. The current issue is the millis part I believe. When switch goes low I want the relay to be energized for 2 seconds, however it continues to Energize every other 2 seconds, when I want it to only energize once until it sees the switch go low voltage again.
Here's my code
unsigned long previousMillis = 0;
const long interval = 2000;// 2 seconds
int ajarSwitch = 2;// set pulldown input to ajarSwitch
int latchRelay = 4;// set pin 2 to variable latchRelay
int ledRelay = 7;// set pin 7 to variable ledRelay
void setup()
{
unsigned long previousMillis = millis();
pinMode(2, INPUT_PULLUP); // set pin 2 as a pullup input
pinMode(4, OUTPUT);// output for door latch relay
pinMode(7, OUTPUT);// output for led relay
}
void loop(){
unsigned long currentMillis = millis();
if ((digitalRead(ajarSwitch) == LOW) && (currentMillis - previousMillis >= interval)) //if door switch is opened, command latch relay on for 2 seconds
{
digitalWrite(latchRelay, HIGH);//command latch relay on for 2 seconda
previousMillis = currentMillis;// save new time
}
if (digitalRead(ajarSwitch) == LOW)
{
digitalWrite(ledRelay, HIGH);// command led relay on
}
else (ajarSwitch == HIGH);
{
digitalWrite(latchRelay, LOW);//turn off latch relay
digitalWrite(ledRelay, LOW);//turn off led relay
}
delay(10); // Delay a little bit to improve simulation performance
}
Railroader:
Looking at lines in loop() I read this:
else (ajarSwitch == HIGH);
That must be a mistake. It looks odd and strange.
There are multiple errors in that line, luckily the solution is easy. Remove everything except
else
You can have another comparison with else if, but in your case that is unnecessary, because the previous IF is testing for LOW and there are only two possibilities with HIGH/LOW. Additionally you forgot the digitalRead() and placed a ";" at the end.
else if ( digitalRead( ajarSwitch ) == HIGH )
Additional notes: These are not bug's but could become one.
You defined the three pins and used in in loop but not in setup. It should look like the following. This will allow you to change a pin with just changing one line of code.
int ajarSwitch = 2;// set pulldown input to ajarSwitch
pinMode( ajarSwitch, INPUT_PULLUP ); // set pin ajarSwitch as a pullup input
I would even go one further and call it AJAR_SWITCH_PIN. It is a constant that will not change. CAPITAL case with underscore is often used for constants.
const int AJAR_SWITCH_PIN = 2;
pinMode( AJAR_SWITCH_PIN, INPUT_PULLUP );
Now read your comments and see if they are true and add additional true information.
int ajarSwitch = 2;// set pulldown input to ajarSwitch set - maybe, pulldown - No, input - No
You can write these lines so clear, that you do not need comments. I know it's sometimes difficult to know when to comment and when not.
Firstly, in setup() function, please do not re-declare variable again.
change "unsigned long previousMillis = millis();" to "previousMillis = millis();"
#include <ezButton.h>
unsigned long previousMillis = 0;
const long interval = 2000;// 2 seconds
int ajarSwitch = 2;// set pulldown input to ajarSwitch
int latchRelay = 4;// set pin 2 to variable latchRelay
int ledRelay = 7;// set pin 7 to variable ledRelay
ezButton button(ajarSwitch); // create Button object that attach to pin 7;
void setup() {
button.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode(4, OUTPUT);// output for door latch relay
pinMode(7, OUTPUT);// output for led relay
previousMillis = millis();
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()){
digitalWrite(latchRelay, HIGH);//command latch relay on for 2 second
digitalWrite(ledRelay, HIGH);// command led relay on
previousMillis = millis();
}
if(millis() - previousMillis > interval)
digitalWrite(latchRelay, LOW);//turn off latch relay
digitalWrite(ledRelay, LOW);//turn off led relay
}
}
Thank you everyone for help and pointers. Yea mistyped that last line I forgot the digital read.
I have no previous coding experience and have been studying and taking some free classes to learn. I appreciate all the help and pointers.
I'm going to fix the code tonight and retest it with all the input I've received from you guys. Does anyone know however if with these changes that the latchRelay code work as I was wanting it to?
Right now with my improper code it looks like the program is looping and commanding the latch relay back on for 2 seconds every few seconds. I'm going to make the changes and see if that corrects the problem.