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/Console/src/main.cpp

80 lines
1.5 KiB
C++
Raw Normal View History

2020-12-27 16:55:38 +01:00
#include <Arduino.h>
2020-12-27 19:18:30 +01:00
#include <Wire.h>
const int SLAVE_ADDR = 9;
2021-01-01 23:10:04 +01:00
const int CLK_PIN = 6;
const int DATA_PIN = 5;
2020-12-27 19:18:30 +01:00
//int prevPlus;
const int bjtCount = 4;
const int btnPin[bjtCount] = {7, 8, 9, 10};
2021-01-01 23:10:04 +01:00
int prevPlus = 0;
2020-12-27 19:18:30 +01:00
byte btnState[bjtCount];
byte lastbtnState[bjtCount];
2020-12-27 16:55:38 +01:00
void setup()
2020-12-27 19:18:30 +01:00
{
pinMode(CLK_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
Serial.begin(9600);
Wire.begin();
for (int i = 0; i < bjtCount; i++)
{
pinMode(btnPin[i], INPUT_PULLUP);
}
}
void transmit(String plus)
{
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(plus.c_str(), 2);
Serial.println(plus.c_str());
Wire.endTransmission();
}
void loop()
{
2021-01-01 23:10:04 +01:00
static uint16_t state = 0;
2020-12-27 19:18:30 +01:00
2021-01-01 23:10:04 +01:00
delayMicroseconds(100);
2020-12-27 19:18:30 +01:00
2021-01-01 23:10:04 +01:00
state = (state << 1) | digitalRead(CLK_PIN) | 0xe000;
2020-12-27 19:18:30 +01:00
2021-01-01 23:10:04 +01:00
if (state == 0xf000)
{
state = 0x0000;
if (digitalRead(DATA_PIN))
{
if (prevPlus == 1)
{
Serial.println("+");
transmit("2i");
}
prevPlus = 1;
}
else
{
if (prevPlus == 0)
{
Serial.println("-");
transmit("2d");
}
prevPlus = 0;
}
}
2020-12-27 16:55:38 +01:00
2020-12-27 19:18:30 +01:00
for (int i = 0; i < bjtCount; i++)
{
btnState[i] = digitalRead(btnPin[i]);
2020-12-27 16:55:38 +01:00
2020-12-27 19:18:30 +01:00
if ((btnState[i] == LOW && btnState[i] != lastbtnState[i]))
{
transmit(String(i) + "t");
}
2020-12-27 16:55:38 +01:00
2020-12-27 19:18:30 +01:00
lastbtnState[i] = btnState[i];
}
}