2020-12-27 16:54:53 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <EEPROM.h>
|
2020-12-27 19:19:16 +01:00
|
|
|
#include <Wire.h>
|
2020-12-27 16:54:53 +01:00
|
|
|
|
2020-12-27 19:19:16 +01:00
|
|
|
#define SLAVE_ADDR 9
|
2020-12-27 16:54:53 +01:00
|
|
|
const int bjtCount = 4;
|
|
|
|
|
|
|
|
const int bjtPin[bjtCount] = {6, 5, 3, 10};
|
|
|
|
int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open"
|
|
|
|
|
2020-12-27 19:19:16 +01:00
|
|
|
String receivedSerialData;
|
|
|
|
String pendingSerialData;
|
|
|
|
String receivedI2cData;
|
|
|
|
|
2021-01-01 00:14:50 +01:00
|
|
|
void changeLights(String data)
|
2020-12-27 19:19:16 +01:00
|
|
|
{
|
2021-01-01 00:14:50 +01:00
|
|
|
for (int i = 0; i < bjtCount; i++)
|
|
|
|
{
|
|
|
|
if (data.indexOf(String(i)) == 0) //changed to i2c for testing
|
|
|
|
{
|
|
|
|
if (data.indexOf("t") == 1) //changed to i2c for testing
|
|
|
|
{
|
|
|
|
if (bjtState[i] != 0)
|
|
|
|
{
|
|
|
|
bjtState[i] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bjtState[i] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (data.indexOf("i") == 1)
|
|
|
|
{
|
|
|
|
bjtState[i] += 5;
|
|
|
|
}
|
|
|
|
else if (data.indexOf("d") == 1)
|
|
|
|
{
|
|
|
|
bjtState[i] -= 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//clamp state between 0 and 255
|
|
|
|
if (bjtState[i] > 255)
|
|
|
|
{
|
|
|
|
bjtState[i] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bjtState[i] < 0)
|
|
|
|
{
|
|
|
|
bjtState[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set absolute state for all
|
|
|
|
if (data == "off")
|
|
|
|
{
|
|
|
|
bjtState[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data == "on")
|
|
|
|
{
|
|
|
|
bjtState[i] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
analogWrite(bjtPin[i], bjtState[i]);
|
|
|
|
EEPROM.update(i, bjtState[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
receivedSerialData = "";
|
|
|
|
receivedI2cData = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void receiveEvent(int byteCount)
|
|
|
|
{
|
|
|
|
char buffer[byteCount];
|
|
|
|
for (int i = 0; i < byteCount; i++)
|
2020-12-27 19:19:16 +01:00
|
|
|
{
|
2021-01-01 00:14:50 +01:00
|
|
|
buffer[i] = Wire.read();
|
2020-12-27 19:19:16 +01:00
|
|
|
}
|
2021-01-01 00:14:50 +01:00
|
|
|
receivedI2cData = String(buffer);
|
|
|
|
|
|
|
|
changeLights(receivedI2cData);
|
2020-12-27 19:19:16 +01:00
|
|
|
}
|
2020-12-27 16:54:53 +01:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.setTimeout(5);
|
2020-12-27 19:19:16 +01:00
|
|
|
Wire.begin(SLAVE_ADDR);
|
|
|
|
Wire.onReceive(receiveEvent);
|
2020-12-27 16:54:53 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < bjtCount; i++)
|
|
|
|
{
|
|
|
|
pinMode(bjtPin[i], OUTPUT);
|
|
|
|
bjtState[i] = EEPROM.read(i);
|
|
|
|
digitalWrite(bjtPin[i], bjtState[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
while (Serial.available())
|
|
|
|
{
|
2020-12-27 19:19:16 +01:00
|
|
|
receivedSerialData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t
|
2020-12-27 16:54:53 +01:00
|
|
|
Serial.flush();
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:14:50 +01:00
|
|
|
changeLights(receivedSerialData);
|
|
|
|
|
|
|
|
pendingSerialData = "";
|
2020-12-27 16:54:53 +01:00
|
|
|
for (int i = 0; i < bjtCount; i++)
|
|
|
|
{
|
2021-01-01 00:14:50 +01:00
|
|
|
noInterrupts();
|
2020-12-27 19:19:16 +01:00
|
|
|
pendingSerialData += String(bjtState[i]) + ",";
|
2021-01-01 00:14:50 +01:00
|
|
|
interrupts();
|
2020-12-27 16:54:53 +01:00
|
|
|
}
|
2020-12-27 19:19:16 +01:00
|
|
|
Serial.println(pendingSerialData);
|
2020-12-27 16:54:53 +01:00
|
|
|
}
|