kind of responds to i2c button press

This commit is contained in:
GHOSCHT 2020-12-27 19:19:16 +01:00
parent 615b627153
commit 1036895fc8

View file

@ -1,25 +1,36 @@
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>
#define SLAVE_ADDR 9
const int bjtCount = 4;
const int bjtPin[bjtCount] = {6, 5, 3, 10};
const int btnPin[bjtCount] = {7, 8, 9, 4};
int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open"
byte btnState[bjtCount];
byte lastbtnState[bjtCount];
String receivedData;
String pendingData;
String receivedSerialData;
String pendingSerialData;
String receivedI2cData;
void receiveEvent(int howMany)
{
char buff[howMany];
for (int i = 0; i < howMany; i++)
{
buff[i] = Wire.read();
}
receivedI2cData = String(buff);
}
void setup()
{
Serial.begin(9600);
Serial.setTimeout(5);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
for (int i = 0; i < bjtCount; i++)
{
pinMode(btnPin[i], INPUT_PULLUP);
pinMode(bjtPin[i], OUTPUT);
bjtState[i] = EEPROM.read(i);
digitalWrite(bjtPin[i], bjtState[i]);
@ -55,39 +66,32 @@ void loop()
{
while (Serial.available())
{
receivedData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t
receivedSerialData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t
Serial.flush();
Serial.println("cum");
}
for (int i = 0; i < bjtCount; i++)
{
btnState[i] = digitalRead(btnPin[i]);
if ((btnState[i] == LOW && btnState[i] != lastbtnState[i]))
if ((receivedI2cData.indexOf(String(i)) == 0)) //changed to i2c for testing
{
toggleLED(i);
}
if (receivedData.indexOf(String(i)) == 0 && receivedData.length() == 2)
{
if (receivedData.indexOf("t") >= 0)
if (receivedI2cData.indexOf("t") >= 0) //changed to i2c for testing
{
toggleLED(i);
}
else if (receivedData.indexOf("i") >= 0)
else if (receivedSerialData.indexOf("i") >= 0)
{
bjtState[i] += 5;
}
else if (receivedData.indexOf("d") >= 0)
else if (receivedSerialData.indexOf("d") >= 0)
{
bjtState[i] -= 5;
}
}
absoluteLED(receivedData, i);
absoluteLED(receivedSerialData, i);
//clamp state between 0 and 255
if (bjtState[i] > 255)
//clamp state between 0 and 255 if (bjtState[i] > 255)
{
bjtState[i] = 255;
}
@ -99,11 +103,11 @@ void loop()
analogWrite(bjtPin[i], bjtState[i]);
EEPROM.update(i, bjtState[i]);
lastbtnState[i] = btnState[i];
pendingData += String(bjtState[i]) + ",";
pendingSerialData += String(bjtState[i]) + ",";
}
Serial.println(pendingData);
pendingData = "";
receivedData = "";
Serial.println(pendingSerialData);
pendingSerialData = "";
receivedSerialData = "";
receivedI2cData = "";
}