Please checkout my most recent post. Controlling Relays through serial w/ MAX3323 IC - #10 by Divinitous - Programming Questions - Arduino Forum
I have a task where I need to control two relays through serial on my arduino (Uno or Nano). The arduino is connected as the third component in a serial daisy chain. The first two devices are a type of lab equipment that dose a specific amount of liquid when the software commands it. Below is the code I've put together and it works fine with hyperterminal and such, however I need to change the arduinos trigger from the integer to a text command.
Where the problem lies is the two dosing devices operate with an address. For instance the first unit is assigned 01 and the second is 02. When the software sends a command to one of them it will send the address followed by the command (ie. 01RH, 02DA10, 02BF and such). This will create havoc with my arduino as any time it sees a 1 or 2 it will activate a valve. No good. I need the arduino to be triggered by a text command (ie 06VALVE1, 06VALVE2). I only add 06 for conformity, this isn't necessary.
How can I get this accomplished? I have dabbled with the arduino a good bit with making control circuits from sensors/switches, but I admit that this is my first time interacting with serial. I think I need to abandon the switch setup and maybe go with if commands. I'm a bit stumped with the added ignorance with serial interaction.
#define RELAY1 2
#define RELAY2 3
void setup()
{
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
Serial.begin(4800);
Serial.println("Type Relay1 or Relay2 to toggle relay on/off");
}
boolean relay1State = false; // Beginning state off
boolean relay2State = false; // Beginning state off
void loop()
{
static int relayVal = 0;
int cmd;
while (Serial.available() > 0)
{
cmd = Serial.read();
switch (cmd)
{
case '1':
{
relay1State = !relay1State; // If true, make false. If false, make true
digitalWrite(RELAY1, relay1State);
Serial.print("Relay1 State is now: "); // Prints the current state
Serial.println(relay1State);
break;
}
case '2':
{
relay2State = !relay2State; // If true, make false. If false, make true
digitalWrite(RELAY2, relay2State);
Serial.print("Relay2 State is now: "); // Prints the current state
Serial.println(relay2State);
break;
}
}
}
}