SUMMARY: Trying to toggle ON/OFF (HIGH/LOW) states for a relay using serial commands, but MUST have the initial state be HIGH (off).
I am using an arduino 2560 Mega to control a 16 channel relay board, which controls brewing heating elements & pumps. (I am going straight from the Arduino, to the board. Using a 12V 1A power supply for the board. All relay channels work.)
When I use this script, all the Digital Pins send LOW output (on). I am trying to make the system send HIGH(off), while allowing me to toggle on/off relays via serial commands. Reasoning is, I need all equipment to unpowered when I turn on my system.
Any help, I've been working at it for days now.
(I can define in a basic sketch digitalWrite(RELAY1, HIGH), but need to know how to toggle the state off/on. I can insert a delay, but turning the relay back off via serial is an unknown process to me)
PLEASE, PLEASE, THANK YOU, THANK YOU!
#define RELAY1 22
#define RELAY2 23
#define RELAY3 24
#define RELAY4 25
#define RELAY5 26
#define RELAY6 27
#define RELAY7 28
#define RELAY8 29
#define RELAY9 30
#define RELAY10 31
#define RELAY11 32
#define RELAY12 33
#define RELAY13 34
#define RELAY14 35
#define RELAY15 36
#define RELAY16 37
void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
pinMode(RELAY9, OUTPUT);
pinMode(RELAY10, OUTPUT);
pinMode(RELAY11, OUTPUT);
pinMode(RELAY12, OUTPUT);
pinMode(RELAY13, OUTPUT);
pinMode(RELAY14, OUTPUT);
pinMode(RELAY15, OUTPUT);
pinMode(RELAY16, OUTPUT);
Serial.begin(9600); // open serial
}
void loop() {
{
static int relayVal = 0;
int cmd;
while (Serial.available() > 0)
{
cmd = Serial.read();
switch (cmd)
{
case '1':
{
relayVal ^= 1; // xor current value with 1 (causes value to toggle)
if (relayVal)
Serial.println("Relay 1 on");
else
Serial.println("Relay 2 off");
break;
}
default:
{
Serial.println("Press enter just the relay number/letter to turn relays on/off");
}
}
if (relayVal)
digitalWrite(RELAY1, LOW);
else
digitalWrite(RELAY1, HIGH);
}
}
}