i have everything working with my code except when i restart my arduino. when it first turns on it opens the drain solenoid then everything returns to normal. i think ive narrowed it down to the part of the code that says drain cycle. can someone look at this and tell me where the error is please.
//READ ONLY BUTTON CONFIGURATION
int pumprelay = A1;
int drainbutton = A1;
int fanrelay = A0;
int drainrelay = A4;
//INITAL PINSTATES
int drainrelayState = HIGH;
int fanrelayState = HIGH;
int pumprelayState = HIGH;
int drainbuttonState = HIGH;
//FAN ON/OFF CYCLE
unsigned long currentMillis = millis();
unsigned long previousMillis = 0; //
unsigned long interval = 0; //
unsigned long onInterval = 138000UL; // FAN ON TIME 2.3 MINUTES
unsigned long offInterval = 1800000UL; // FAN OFF TIME 30 MINUTES
//PUMP ON/OFF CYCLE
unsigned long interval2 = 0;
unsigned long pumppreviousMillis = 0;
unsigned long pumponInterval = 36000UL; // spray on time 26 SECONDS
unsigned long pumpoffInterval = 6480000UL; // sprayer off time 1.8 HRS
//DRAIN RELAY ON TIME
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 2000; //DRAIN OPEN TIME
byte previousdrainbuttonState;
byte currentdrainbuttonState;
void setup() {
interval = offInterval;
pinMode (drainbutton, INPUT_PULLUP); //DRAIN ACTIVATION PIN
pinMode (fanrelay, OUTPUT); //FAN POWER RELAY
pinMode (pumprelay, OUTPUT); //WATER PRESSURE PUMP
pinMode (drainrelay, OUTPUT); //DRAIN SOLENOID POWER RELAY
digitalWrite(pumprelay, HIGH); //SET INITAL RELAY STATE
digitalWrite(fanrelay, HIGH); //SET INITAL RELAY STATE
digitalWrite(drainrelay, HIGH); //SET INITAL RELAY STATE
digitalWrite(drainbutton, HIGH); //SET INITAL BUTTON STATE
Serial.begin(9600);
Serial.print("Ready");
}
void loop() {
//FAN CYCLE
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (fanrelayState == HIGH) { //IF ITS OFF
fanrelayState = LOW; // THEN TURN ON
interval = onInterval; // ASK HOW LONG TO STAY ON
}
else {
fanrelayState = HIGH; // OTHERWISE OFF
interval = offInterval; //ASK HOW LONG TO STAY OFF
}
digitalWrite(fanrelay, fanrelayState); //UPDATE STATUS OF FAN
}
//PUMP CYCLE
if(currentMillis - pumppreviousMillis > interval2) {
pumppreviousMillis = currentMillis;
if (pumprelayState == HIGH) { //IF ITS OFF
pumprelayState = LOW; // TURN IT ON
interval2 = pumponInterval; //ASK HOW LONG TO TURN IT ON
}
else {
pumprelayState = HIGH; //OTHERWISE TURN IT OFF
interval2 = pumpoffInterval; //ASK HOW LONG TO TURN IT OFF
}
}
//DRAIN CYCLE
currentTime = millis();
previousdrainbuttonState = currentdrainbuttonState;
currentdrainbuttonState = digitalRead(drainbutton); //ASK STATE OF DRAIN BUTTON
if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
{
startTime = millis();
digitalWrite(drainrelay, LOW);//TURN IT ON
}
if (currentTime - startTime >= period) //ASK HOW LONG TO STAY ON
{
digitalWrite(drainrelay, HIGH);
}
}
When an Arduino is first turned on all the pins default to input. If you have a pin connected to a FET gate this will turn on until your code runs and sets that pin to be an output.
So to prevent this you need a 10K resistor on the FET's gate to ground.
i have 2 other relays that dont do that. when i edit out that part of the code at the very bottom it dont come on. it takes about 3 seconds after i plug it in for it to do that. i have pullup resistors in place
if i change
if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
{
startTime = millis();
digitalWrite(drainrelay, LOW);//TURN IT ON
TO
if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
{
startTime = millis();
digitalWrite(drainrelay, HIGH);//TURN IT ON
it wont come on. someone that piece of code is not running right?
drain cycle is only to kick in after pin a1 aka the pump, turns on then off. ive been scratching my head for a while now and dont know what going on
I think i got it working by changing
byte previousdrainbuttonState;
byte currentdrainbuttonState;
to
int previousdrainbuttonState = HIGH;
int currentdrainbuttonState = HIGH;
notsolowki:
I think i got it working by changing
byte previousdrainbuttonState;
byte currentdrainbuttonState;
to
int previousdrainbuttonState = HIGH;
int currentdrainbuttonState = HIGH;
By default uninitialized global variables are initialized to 0 (and LOW == 0). But IIRC you should not rely on this because it is how Arduino (and many other) compilers work but there may be compilers that don't initialize uninitialized variables - in this case they will hold "random" value. It is OK to declare uninitialized variable but you should write a value into it before you read it for the first time. (In your case "previousdrainbuttonState = currentdrainbuttonState;" leads to reading currentdrainbuttonState before you write anything into it.)
I think you can safely keep both the value byte.
You are using currentMillis as part of your timing logic. You only set it once when the variable is first declared. You're also using currentTime for a similar purpose which correctly gets set on each iteration of loop.
Something is wrong. You probably only need one of them.
im really new at thisand im just trying to get started. im sure theres a ton of errors in all my code lol. between code errors and emi ive lost a lot of sleep
notsolowki:
i have 2 other relays that dont do that.
So what, a floating input to a FET can be anything, it doesn't stop you needing a pull down resistor on a FET gate, even if you see no problem at the moment, you will in time.
Wise up.
im not using a fet. and the problem wasnt the resistor i told you i already had pullUP resistors in place. it ended up being the code problem solved. idk why changing byte to int helped. like i said im new to this. if you want to help me please go to my other thread and see if you can answer my question because no one else cares to and im trying to get this part over with before i fall asleep
notsolowki:
idk why changing byte to int helped.
Read post #6. It was not due to changing byte to int. It is because declaring
byte x;
is the same as
byte x=0;
is the same as
byte x=LOW;
To fix your original code
byte x=HIGH;
was needed. It does not matter if it is byte or int. The fact you set it HIGH in declaration is what matters.
can you look at my other thread "help with if statements. scroll to the bottom and read my question maybe you would have some input on this too. thankyou for the help
im not using a fet.
But you said in the first post:-
when it first turns on it opens the drain solenoid
So drain was not referring to the solenoid driver then, like any electronics bloke might think. Nice of you to tell me this in reply #2 when you have the opportunity. Oh wait you did no such thing.