Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to enable invert of the on/off state #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions ObservatoryPowerControl.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ char receiveBuffer [SerialBufferSize];
int bufferPosition = 0;
enum SerialState lastState = start;

// As per original sketch
//const int digital_on = 0;
//const int digital_off = 1;

// to handle relays which are reversed, swap these around eg XC-4418 (https://www.jaycar.com.au/medias/sys_master/images/images/9403712995358/XC4418-dataSheetMain.pdf)
const int digital_on = 1;
const int digital_off = 0;


// Command reference
//
// :{S|R}{relaynum 0-7}{1=on, 0=off}#
// eg. :S00# = Set Relay 0 to OFF
// eg. :S01# = Set Relay 0 to ON
// eg. :R0# = Read status of relay 1, returns 0=Off, 1=On


void setup()
{
// put your setup code here, to run once:

for (int i = 0; i < 8; i++)
{
digitalWrite(relayArray[i],1);
digitalWrite(relayArray[i],digital_off); // switch off relays by default
pinMode(relayArray[i], OUTPUT);
}
}
Expand Down Expand Up @@ -49,14 +65,14 @@ void testMode()
{
for (int i = 0; i < 8; i++) //turn relays on
{
digitalWrite(relayArray[i], 0);
delay (1000);
WriteRelayPin(relayArray[i], digital_on);
delay (250);
}

for (int i = 0; i < 8; i++) //turn relays off
{
digitalWrite(relayArray[i], 1);
delay (1000);
WriteRelayPin(relayArray[i], digital_off);
delay (250);
}
}

Expand Down Expand Up @@ -168,7 +184,7 @@ void DoReadCommand()

void WriteRelayPin(int relayPin, int relayValue)
{
digitalWrite(relayPin, relayValue == 1 ? 0 : 1);
digitalWrite(relayPin, relayValue == digital_on ? digital_on : digital_off);
}

void DoSetCommand()
Expand All @@ -187,7 +203,11 @@ void DoSetCommand()
Serial.println("Bad data");
return;
}
int relayValue = onOffCommand - '0';

//int relayValue = onOffCommand - '0';
int relayValue;
if (onOffCommand == '0' ? relayValue = digital_off : relayValue = digital_on);

WriteRelayPin(relayPin, relayValue);
SendRelayStatus(relay,relayValue);
}
Expand All @@ -210,16 +230,4 @@ void InterpretCommand()
Serial.println("Bad command");
}
}