This commit is contained in:
GHOSCHT 2021-06-28 17:55:24 +02:00
commit 8d180cff4f

View file

@ -1,41 +1,77 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
//I2C
const int SLAVE_ADDR = 9; const int SLAVE_ADDR = 9;
//Rotary Encoder
const int CLK_PIN = 6; const int CLK_PIN = 6;
const int DATA_PIN = 5; const int DATA_PIN = 5;
//int prevPlus;
const int bjtCount = 4;
const int btnPin[bjtCount] = {7, 8, 9, 10};
int prevPlus = 0; int prevPlus = 0;
//General
const int bjtCount = 4;
const int btnPin[bjtCount] = {7, 8, 9, 10};
byte btnState[bjtCount]; byte btnState[bjtCount];
byte lastbtnState[bjtCount]; byte lastBtnState[bjtCount] = {HIGH, HIGH, HIGH, HIGH};
unsigned long lastDebounceTime[bjtCount] = {0, 0, 0, 0};
unsigned long debounceDelay = 50;
void setup() void setup()
{ {
pinMode(CLK_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
Serial.begin(9600); Serial.begin(9600);
Wire.begin(); Wire.begin();
pinMode(CLK_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
for (int i = 0; i < bjtCount; i++) for (int i = 0; i < bjtCount; i++)
{ {
pinMode(btnPin[i], INPUT_PULLUP); pinMode(btnPin[i], INPUT_PULLUP);
} }
} }
void transmit(String plus) void transmit(String data)
{ {
Wire.beginTransmission(SLAVE_ADDR); Wire.beginTransmission(SLAVE_ADDR);
Wire.write(plus.c_str(), 2); Wire.write(data.c_str(), 2);
Serial.println(plus.c_str()); Serial.println(data.c_str());
Wire.endTransmission(); Wire.endTransmission();
} }
void loop() void loop()
{ {
//Buttons
byte btnReading[bjtCount];
for (int i = 0; i < bjtCount; i++)
{
btnReading[i] = digitalRead(btnPin[i]);
if (btnReading[i] != lastBtnState[i])
{
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay)
{
if (btnReading[i] != btnState[i])
{
btnState[i] = btnReading[i];
if (btnState[i] == LOW)
{
transmit(String(i) + "t");
}
}
}
lastBtnState[i] = btnReading[i];
}
//Rotary Encoder
static uint16_t state = 0; static uint16_t state = 0;
delayMicroseconds(100); delayMicroseconds(100);
@ -49,7 +85,6 @@ void loop()
{ {
if (prevPlus == 1) if (prevPlus == 1)
{ {
Serial.println("+");
transmit("2i"); transmit("2i");
} }
prevPlus = 1; prevPlus = 1;
@ -58,22 +93,9 @@ void loop()
{ {
if (prevPlus == 0) if (prevPlus == 0)
{ {
Serial.println("-");
transmit("2d"); transmit("2d");
} }
prevPlus = 0; prevPlus = 0;
} }
} }
for (int i = 0; i < bjtCount; i++)
{
btnState[i] = digitalRead(btnPin[i]);
if ((btnState[i] == LOW && btnState[i] != lastbtnState[i]))
{
transmit(String(i) + "t");
}
lastbtnState[i] = btnState[i];
}
} }