Implement PWM dimming via COM

This commit is contained in:
GHOSCHT 2020-12-19 14:35:14 +01:00
parent 4d066c6b02
commit 88cf657869

View file

@ -25,22 +25,8 @@ void setup()
}
}
void loop()
void toggleLED(int i)
{
while (Serial.available())
{
receivedData = Serial.readString(); //FORMAT: 0123 -> toggles all bjts | 12 -> toggles only bjts with the index 1 and 2
Serial.flush();
}
for (int i = 0; i < bjtCount; i++)
{
btnState[i] = digitalRead(btnPin[i]);
if ((btnState[i] == LOW && btnState[i] != lastbtnState[i]) || (receivedData.indexOf(String(i)) >= 0))
{
receivedData.replace(String(i), "");
if (bjtState[i] != 0)
{
bjtState[i] = 0;
@ -51,15 +37,64 @@ void loop()
}
}
if (receivedData == "off")
void absoluteLED(String data, int i)
{
if (data == "off")
{
bjtState[i] = 0;
}
if (receivedData == "on")
if (data == "on")
{
bjtState[i] = 255;
}
}
void loop()
{
while (Serial.available())
{
receivedData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t
Serial.flush();
}
for (int i = 0; i < bjtCount; i++)
{
btnState[i] = digitalRead(btnPin[i]);
if ((btnState[i] == LOW && btnState[i] != lastbtnState[i]))
{
toggleLED(i);
}
if (receivedData.indexOf(String(i)) == 0 && receivedData.length() == 2)
{
if (receivedData.indexOf("t") >= 0)
{
toggleLED(i);
}
else if (receivedData.indexOf("i") >= 0)
{
bjtState[i] += 5;
}
else if (receivedData.indexOf("d") >= 0)
{
bjtState[i] -= 5;
}
}
absoluteLED(receivedData, i);
//clamp state between 0 and 255
if (bjtState[i] > 255)
{
bjtState[i] = 255;
}
if (bjtState[i] < 0)
{
bjtState[i] = 0;
}
analogWrite(bjtPin[i], bjtState[i]);
EEPROM.update(i, bjtState[i]);