Help!
I cannot get the relays for an 8 Relay Module to end in an off position. Not sure how to accomplish this. I have worked and worked through this problem and am also complete. I just need to get an answer to the final step. I had a problem with getting the relays to start in the off position. I was able to solve them with the following commands.
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
Here is the code.
//keep I/O serial pin 1 in case of serial
#define startButton 12
#define compAirSlnd 2
#define drainSlnd 3
#define h2oSlnd 4
#define causticSlnd 5
#define causticRtrnSlnd 6
#define saniSlnd 7
#define saniRtrnSlnd 8
#define co2PurgeSlnd 9
#define co2FillSlnd 10
#define pumpSwitch 11
#define idle 0
#define airWaterAir1 1
#define airWaterAir2 2
#define caustic 3
#define sanitizer 4
#define co2Purge 5
#define co2Fill 6
//set initial state idle
//idle waits for start button
byte state = idle;
boolean stateChange = false;
byte buttonState = HIGH;
byte lastButtonState = HIGH;
unsigned long currentMillis = millis();
unsigned long stateStartMillis = millis();
unsigned long timeRemaining = 0;
//i2c lcd dislplay
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip
void setup() {
pinMode(startButton, INPUT_PULLUP);
digitalWrite (compAirSlnd, HIGH);
pinMode (compAirSlnd, OUTPUT);
digitalWrite (co2FillSlnd, HIGH);
pinMode (co2FillSlnd, OUTPUT);
digitalWrite (co2PurgeSlnd, HIGH);
pinMode (co2PurgeSlnd, OUTPUT);
digitalWrite (h2oSlnd, HIGH);
pinMode (h2oSlnd, OUTPUT);
digitalWrite (pumpSwitch, HIGH);
pinMode (pumpSwitch, OUTPUT);
digitalWrite (causticSlnd, HIGH);
pinMode (causticSlnd, OUTPUT);
digitalWrite (causticRtrnSlnd, HIGH);
pinMode (causticRtrnSlnd, OUTPUT);
digitalWrite (saniSlnd, HIGH);
pinMode (saniSlnd, OUTPUT);
digitalWrite (saniRtrnSlnd, HIGH);
pinMode (saniRtrnSlnd, OUTPUT);
digitalWrite (drainSlnd, HIGH);
pinMode (drainSlnd, OUTPUT);
lcd.begin(16, 2);
lcd.print(“Keg Wash”);
delay(1000);
}
void loop() {
//use one value of time for all events in loop and functions
currentMillis = millis();
switch (state) {
case idle: {
buttonState = digitalRead(startButton);
//may need debouncing?
if (buttonState == LOW && lastButtonState == HIGH)
{
state = airWaterAir1;
}
//the next line may not be needed as will be leaving state on first press
//lastButtonState default HIGH from setup
lastButtonState = buttonState;
break;
}
case airWaterAir1: {
airWaterAir(15, 20, 15);
break;
}
case caustic: {
unsigned long causticTime = 90000UL;
if (stateChange == false)
{
stateStartMillis = millis();
stateChange = true;
digitalWrite(causticSlnd, HIGH);
digitalWrite(causticRtrnSlnd, HIGH);
digitalWrite(pumpSwitch, HIGH);
}
if (stateChange == true)
timeRemaining = stateStartMillis + causticTime - currentMillis;
if (currentMillis - stateStartMillis >= causticTime)
{
digitalWrite(causticSlnd, LOW);
digitalWrite(causticRtrnSlnd, LOW);
digitalWrite(pumpSwitch, LOW);
stateChange = false;
state = airWaterAir2;
}
break;
}
case airWaterAir2: {
airWaterAir(15, 15, 15);
break;
}
case sanitizer: {
unsigned long sanitizerTime = 60000UL;
if (stateChange == false)
{
stateStartMillis = millis();
stateChange = true;
digitalWrite(saniSlnd, HIGH);
digitalWrite(saniRtrnSlnd, HIGH);
digitalWrite(pumpSwitch, HIGH);
}
if (stateChange == true)
timeRemaining = stateStartMillis + sanitizerTime - currentMillis;
if (currentMillis - stateStartMillis >= sanitizerTime)
{
digitalWrite(saniSlnd, LOW);
digitalWrite(saniRtrnSlnd, LOW);
digitalWrite(pumpSwitch, LOW);
stateChange = false;
state = co2Purge;
}
break;
}
case co2Purge: {
unsigned long co2purgeTime = 30000UL;
if (stateChange == false)
{
stateStartMillis = millis();
stateChange = true;
digitalWrite(co2PurgeSlnd, HIGH);
digitalWrite(drainSlnd, HIGH);
}
if (stateChange == true)
timeRemaining = stateStartMillis + co2purgeTime - currentMillis;
if (currentMillis - stateStartMillis >= co2purgeTime)
{
digitalWrite(co2PurgeSlnd, LOW);
digitalWrite(drainSlnd, LOW);
stateChange = false;
state = co2Fill;
}
break;
}
case co2Fill: {
unsigned long co2fillTime = 10000UL;
if (stateChange == false)
{
stateStartMillis = millis();
stateChange = true;
digitalWrite(co2FillSlnd, HIGH);
}
if (stateChange == true)
timeRemaining = stateStartMillis + co2fillTime - currentMillis;
if (currentMillis - stateStartMillis >= co2fillTime)
{
digitalWrite(co2FillSlnd, LOW);
stateChange = false;
state = idle;
}
break;
}
}
lcdDisplay();
}
void airWaterAir(byte T1, byte T2, byte T3) {
unsigned long firstAirTime = T1 * 1000UL;
unsigned long h2oTime = T2 * 1000UL;
unsigned long secondAirTime = T3 * 1000UL;
unsigned long drainTime = (T1 + T2 + T3) * 1000UL;
static boolean firstAirComplete = false;
if (stateChange == false)
{
stateStartMillis = millis();
stateChange = true;
digitalWrite(compAirSlnd, HIGH);
digitalWrite(drainSlnd, HIGH);
}
if (stateChange == true)
timeRemaining = stateStartMillis + drainTime - currentMillis;
if (firstAirComplete == false && (currentMillis - stateStartMillis >= firstAirTime))
{
digitalWrite(compAirSlnd, LOW);
digitalWrite(h2oSlnd, HIGH);
digitalWrite(pumpSwitch, HIGH);
digitalWrite(drainSlnd,HIGH);
firstAirComplete = true;
}
if (currentMillis - stateStartMillis >= (firstAirTime + h2oTime))
{ digitalWrite(h2oSlnd, LOW);
digitalWrite (pumpSwitch, LOW);
digitalWrite(drainSlnd,HIGH);
digitalWrite (compAirSlnd, HIGH);
}
if (currentMillis - stateStartMillis >= drainTime)
{ digitalWrite(compAirSlnd, LOW);
digitalWrite(drainSlnd, LOW);
if (state == airWaterAir1)
state = caustic;
else
state = sanitizer;
stateChange = false;
firstAirComplete = false;
}
}
void lcdDisplay()
{
const char displayState[17] =
{
"idle ",
"airWaterAir1 ",
"airWaterAir2 ",
"caustic ",
"sanitizer ",
"co2Purge ",
"co2Fill ",
};
const int displayInterval = 1000;
static unsigned long lastDisplay = 0;
static byte lastState = 99;//forces initial display to idle
if (millis() - lastDisplay >= displayInterval)
{
lastDisplay += displayInterval;
if (state != lastState)
{
lcd.setCursor (0, 0);
lcd.print(displayState[state]);
}
lcd.setCursor(0, 1);
if (state == idle)
{
lcd.print(“XX”);
//timeRemaining = 0;
}
else if (timeRemaining / 1000 <= 10)
{
lcd.print(timeRemaining / 1000); //seconds
lcd.print(" ");//clear trailing 0
}
else
lcd.print(timeRemaining / 1000);
lastState = state;
}
}