unsigned long currentMillis = millis();
unsigned long messageRxd = millis();
const int MAXTIME = 6000; // Max allowed time between messages before we set everything off.
// Irrigation Message Format
// "IRRIG" + Bank, plus 8 channels = T = On or F = Off.
const int messageLength = 14;
const int ENDOFMSG=13;//69; //="E"
char data [messageLength];
int number_of_bytes_received;
const int MAXCHANNELS = 8;
const int MAXBANKS = 5;
const int BANKNOPOS = 5;
const int CHANNELSPOS = 6;
boolean started = false;
int channelMap [MAXBANKS][MAXCHANNELS] = {
{
2,3,4,5,6,7,8,9 }
,
{
10,11,12,13,22,23,24,25 }
,
{
26,27,28,29,30,32,32,33 }
,
{
34,35,36,37,38,39,40,41 }
,
{
42,42,44,45,46,47,48,49 }
};
const int PUMPSTATUSPIN = 49;
void setup(void)
{
Serial.begin(57600);
for ( int bankNo = 0 ; bankNo < MAXBANKS;bankNo++){
for ( int channelNo = 0 ; channelNo < MAXCHANNELS;channelNo++)
{
pinMode(channelMap[bankNo][channelNo], OUTPUT);
digitalWrite(channelMap[bankNo][channelNo], LOW);
}
}
pinMode(PUMPSTATUSPIN, INPUT_PULLUP);
}
void loop(void)
{
int command ; // This is the command char, in ascii form, sent from the serial port
unsigned long currentMillis = millis();
int result;
int bank;
int pumpStatus = digitalRead(PUMPSTATUSPIN);
for (int x=0 ;x<messageLength;x++){
data[x] = 0;
}
if(Serial.available() > 0)
{
number_of_bytes_received = Serial.readBytesUntil (ENDOFMSG,data,messageLength);
result = mystrncmp (data, "IRRIG", 5);
if (result == true)
{
started=true;
Serial.print("OK ");
pumpStatus = digitalRead(PUMPSTATUSPIN);
Serial.print (pumpStatus);
messageRxd = millis();
bank = int(data[BANKNOPOS]);
bank = bank - 48; //convert from ascii to integer
for ( int channelNo = 0 ; channelNo < MAXCHANNELS;channelNo++)
{
//Serial.print(data [CHANNELSPOS+channelNo]);
if (data [CHANNELSPOS+channelNo]=='T'){
digitalWrite(channelMap[bank][channelNo], HIGH);
}
else{
digitalWrite(channelMap[bank][channelNo], LOW);
}
}
}
else
{
Serial.print("POK");
pumpStatus = digitalRead(PUMPSTATUSPIN);
Serial.print (pumpStatus);
}
}
if (started == true){
if (currentMillis>MAXTIME ){
if (currentMillis-MAXTIME > messageRxd ){
for ( int bankNo = 0 ; bankNo < MAXBANKS;bankNo++){
for ( int channelNo = 0 ; channelNo < MAXCHANNELS;channelNo++)
{
digitalWrite(channelMap[bankNo][channelNo], LOW);
}
}
messageRxd = millis();
Serial.print("NOK");
pumpStatus = digitalRead(PUMPSTATUSPIN);
Serial.print (pumpStatus);
}
}
}
}
boolean mystrncmp (char item1[], char item2[], int numberToCompare){
for (int n=0; n<numberToCompare; n++){
if (item1[n] != item2[n]){
return false;
}
}
return true;
}