Hi
I am using an Uno and a Duinotech board with 4 relays.
I am using relays 1 and 2 as single pole double throw control for a motor to change the direction of the motor. CW and CCW.
Relay 3 supplies the power to Relays 1 and 2.
Relay 4 is in series with relay 3, and looks for an input from a second Arduino (Arduino2) to ensure that power is not supplied to relays 1 and 2 unless Arduino 2 is running its cycle. Arduino 2 controls a fertiliser pump. I would rather not try and incorporate this new code into the Arduino Code as that seems a step too far, and also I will not always need the Arduino 1 code in play when running Arduino 2.
The process steps for Arduino 1, as I want them, are as follows.
Relays 1 - 4 are LOW.
Arduino 2 starts and "Doser Control" of Arduino 1 is read as HIGH.
Relay 4 goes HIGH as Doser Control is HIGH, and will stay HIGH until Arduino 2 goes LOW.
As Relay4 is high, Relay 3 has a 5 second delay and then goes HIGH. As Relay 1 and 2 are Double throw, the motor attached to them runs clockwise. Relays 3 and 4 are both HIGH so current flows.
The motor runs a winch and the unit goes along a line until it reaches a magnet on the line and "Hall_1" goes HIGH for a moment as the Hall Sensor detects the magnet.
As Hall_1 went HIGH, Relays 1 and 2 now go HIGH. Relay 3 goes LOW for 5 seconds so that the winch motor is not thrown from one direction straight into the opposite direction. Relay 3 goes HIGH after the 5 seconds and the motor runs Counter clockwise until it reaches the magnet at the other end and Hall_1 goes HIGH for a moment and Relays 1 and 2 go low. Relay 3 goes low for 5 seconds.
As long as 'Doser Control' is HIGH then this process of going back and forth continues. When Doser Control is LOW I want all Relays to be LOW.
So that explains how I want it to be.
Now to explain how it is at the moment.
At the moment the code I have is that when started, all relays are LOW, and then after the 5 second debounce time (StateChangeDelay) then Relay 3 goes HIGH. This means that even when 'Doser Control' is LOW, Relay 3 goes HIGH, and at some point it will wear out or just waste power sitting HIGH when it doesn't need to be on.
So my question is, what do I need to do, to get relay 3 to stay LOW when 'Doser Control' is LOW?
Then when Doser control goes HIGH, relay 3 comes on and works as it currently does.
I should also note that I realise that relay 4 is kind of unnecessary, as Relay 3 should be used on its own if I can get it to stay off when Doser Control is LOW, but that was too much for my brain to manage.
I have pasted my code below.
CODE:
// this constant won't change:
const int Hall_1Pin = 2;
const int relayIN1 = 3;
const int relayIN2 = 4;
const int relayIN3 = 5;
const int relayIN4 = 6;
const int DoserControl = 8;
// Variables will change:
int DoserState; //Variable for current Doser state
int relay1State = LOW; //Variable for current Relay state
int relay2State = LOW; //Variable for current Relay state
int relay3State = LOW; //Variable for current Relay state
int relay4State = LOW; //Variable for current Relay state
int Hall_1State; //Variable for current Hall reading
int lastHall_1State = LOW; //Variable for last known Hall1 reading
int lastDoserState = LOW; //Variable for last known Doser reading
unsigned long lastDebounceTime = 0; //last time the pin was toggled, used to keep track of time
unsigned long debounceDelay = 50; //the debounce time which user sets prior to run
unsigned long StateChangeTime = 0; // last time the pin was toggled, used to keep track of time
unsigned long StateChangeDelay = 5000; //the delay before turning on relay 3
unsigned long lastDoserTime = 0; //last time the pin was toggled, used to keep track of time
unsigned long debounceDoserDelay = 50; //the debounce time which user sets prior to run
void setup() {
pinMode(relayIN1, OUTPUT);
pinMode(relayIN2, OUTPUT);
pinMode(relayIN3, OUTPUT);
pinMode(relayIN4, OUTPUT);
pinMode(Hall_1Pin, INPUT);
pinMode(DoserControl, INPUT);
digitalWrite(relayIN1, relay1State);
digitalWrite(relayIN2, relay2State);
digitalWrite(relayIN3, relay3State);
digitalWrite(relayIN4, relay4State);
}
void loop(){
int reading3 = digitalRead(DoserControl); //Is the doser on?
if (reading3 != lastDoserState) {
lastDoserTime = millis();
}
if ((millis()- lastDoserTime) > debounceDoserDelay) {
if (reading3 != DoserState) {
DoserState = reading3;
if (DoserState == HIGH) {
relay1State = relay1State;
relay2State = relay2State;
relay3State = relay3State;
relay4State = HIGH;
}
}
}
else {
relay4State = LOW;
relay3State = LOW;
}
digitalWrite (relayIN3, relay3State);
digitalWrite (relayIN4, relay4State);
//save the reading. next time through the loop the state of the reading will be known as the lastDoserState
lastDoserState = reading3;
//read the Hall pin, if pressed will be high, if not pressed will be low
int reading1 = digitalRead(Hall_1Pin);
//in the case that the reading is not equal to the last state set the last debounce time to current millis time
if (reading1 != lastHall_1State) {
lastDebounceTime = millis();
StateChangeTime = millis();
}
//check the difference between current time and last registered button press time, if it's greater than user defined delay then change the LED state as it's not a bounce
if ((millis()-lastDebounceTime) > debounceDelay) {
if (reading1 != Hall_1State) {
Hall_1State = reading1;
if (Hall_1State == LOW) {
relay1State = !relay1State;
relay2State = !relay2State;
relay3State = LOW;
}
}
}
if ((millis()-StateChangeTime) > StateChangeDelay) {
relay3State = HIGH;
}
digitalWrite(relayIN1, relay1State);
digitalWrite(relayIN2, relay2State);
digitalWrite(relayIN3, relay3State);
//save the reading. next time through the loop the state of the reading will be known as the lastHall_1State
lastHall_1State = reading1;
}