This repository has been archived on 2023-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
old-monorepo/Arduino/ArduinoRelayControl.ino
2020-08-02 10:56:05 +02:00

65 lines
No EOL
1.6 KiB
C++

#include <EEPROM.h>
const int relayCount = 4;
const int relayPin[relayCount] = {3, 4, 5, 6};
const int btnPin[relayCount] = {7, 8, 9, 10};
byte relayState[relayCount] = {HIGH, HIGH, HIGH, HIGH}; //high -> relay off
byte buttonState[relayCount] byte lastButtonState[relayCount];
String receivedCom;
String statusToSend;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(5);
for (int i = 0; i < relayCount; i++)
{
pinMode(btnPin[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
relayState[i] = EEPROM.read(i);
digitalWrite(relayPin[i], relayState[i]);
}
}
void loop()
{
while (Serial.available())
{
receivedCom = Serial.readString(); //FORMAT: 0123 -> toggles all relays | 12 -> toggles only relay with the index 1 and 2
Serial.flush();
}
for (int i = 0; i < relayCount; i++)
{
buttonState[i] = digitalRead(btnPin[i]);
if ((buttonState[i] == LOW && buttonState[i] != lastButtonState[i]) || (receivedCom.indexOf(String(i)) >= 0))
{
receivedCom.replace(String(i), "");
relayState[i] = !relayState[i];
}
if (receivedCom == "off")
{
relayState[i] = LOW;
}
if (receivedCom == "on")
{
relayState[i] = HIGH;
}
digitalWrite(relayPin[i], relayState[i]);
EEPROM.update(i, relayState[i]);
lastButtonState[i] = buttonState[i];
statusToSend += relayState[i];
}
Serial.println(statusToSend);
statusToSend = "";
receivedCom = "";
}