Add master wheel code

This commit is contained in:
C0d3v 2026-01-15 17:23:15 +01:00
parent 15297dfaec
commit feb7e31cdb
3 changed files with 198 additions and 0 deletions

5
terminal/master/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View File

@ -0,0 +1,14 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:nanoatmega328new]
platform = atmelavr
board = nanoatmega328new
framework = arduino

View File

@ -0,0 +1,179 @@
#include <Wire.h>
const int PIN_POWER_SWITCH_INPUT = 2;
const int PIN_POWER_SWITCH_OUTPUT = 12;
// I2C
const uint8_t ARCHIV_ADDR = 0x10;
// I2C-Befehle
const uint8_t CMD_LED_LOCK_TOGGLE = 0x01;
const uint8_t CMD_ALLES_GESCHAFFT = 0x02;
const uint8_t CMD_AUSWAHLRAD_LINKS = 0x03;
const uint8_t CMD_AUSWAHLRAD_RECHTS = 0x04;
// Randomizer-Ausgänge
const int randomizer_modul1_pin = 7;
const int randomizer_modul2_pin = 8;
const int randomizer_modul3_pin = 9;
// Modul-Eingänge (D3 = Modul 1)
const int slavePins[3] = {3, 4, 5};
// Drehgeber A1 / A2
const int encoderPinA = A1;
const int encoderPinB = A2;
volatile long position = 0;
volatile uint8_t lastState;
const int stepsPerClick = 500;
long lastClickPosition = 0;
// Zustände
bool powerOnState = true;
// Schalterfilter
bool stableSwitchState;
bool lastRawSwitchState;
unsigned long lastChangeTime = 0;
const unsigned long stableTime = 150;
// Lösungsstatus
bool slaveSolved[3] = {false, false, false};
bool allSolvedSent = false;
/* ========================================================= */
void setup() {
Wire.begin();
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(PIN_POWER_SWITCH_INPUT, INPUT_PULLUP);
pinMode(PIN_POWER_SWITCH_OUTPUT, OUTPUT);
digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerOnState);
pinMode(randomizer_modul1_pin, OUTPUT);
pinMode(randomizer_modul2_pin, OUTPUT);
pinMode(randomizer_modul3_pin, OUTPUT);
for (int i = 0; i < 3; i++)
pinMode(slavePins[i], INPUT_PULLUP);
lastState = (PINC >> 1) & 0x03;
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT9) | (1 << PCINT10);
stableSwitchState = digitalRead(PIN_POWER_SWITCH_INPUT);
lastRawSwitchState = stableSwitchState;
powerOnState = (stableSwitchState == HIGH);
}
/* ========================================================= */
ISR(PCINT1_vect) {
if (!powerOnState) return;
uint8_t state = (PINC >> 1) & 0x03;
if ((lastState == 0b00 && state == 0b01) ||
(lastState == 0b01 && state == 0b11) ||
(lastState == 0b11 && state == 0b10) ||
(lastState == 0b10 && state == 0b00))
position++;
else
position--;
lastState = state;
}
/* ========================================================= */
void loop() {
checkPowerState();
checkSlaves();
if (powerOnState)
checkEncoderPosition();
}
/* ========================================================= */
void checkPowerState() {
bool rawState = digitalRead(PIN_POWER_SWITCH_INPUT);
if (rawState != lastRawSwitchState) {
lastChangeTime = millis();
lastRawSwitchState = rawState;
}
if ((millis() - lastChangeTime) > stableTime &&
rawState != stableSwitchState) {
stableSwitchState = rawState;
powerOnState = (stableSwitchState == HIGH);
digitalWrite(PIN_POWER_SWITCH_OUTPUT, powerOnState);
sendI2CCommand(CMD_LED_LOCK_TOGGLE);
}
}
/* ========================================================= */
void sendI2CCommand(uint8_t cmd) {
Wire.beginTransmission(ARCHIV_ADDR);
Wire.write(cmd);
Wire.endTransmission();
}
/* ========================================================= */
void checkSlaves() {
if (allSolvedSent) return;
for (int i = 0; i < 3; i++) {
if (!slaveSolved[i] && digitalRead(slavePins[i]) == HIGH)
slaveSolved[i] = true;
}
if (slaveSolved[0] && slaveSolved[1] && slaveSolved[2]) {
sendI2CCommand(CMD_ALLES_GESCHAFFT);
allSolvedSent = true;
// 🔒 Zyklus beenden → alles zurücksetzen
//for (int i = 0; i < 3; i++)
//slaveSolved[i] = false;
slaveSolved[0] = false;
}
}
/* ========================================================= */
void checkEncoderPosition() {
long diff = position - lastClickPosition;
if (abs(diff) >= stepsPerClick) {
sendRandomizerPulse();
if (diff > 0)
sendI2CCommand(CMD_AUSWAHLRAD_RECHTS);
else
sendI2CCommand(CMD_AUSWAHLRAD_LINKS);
lastClickPosition = position;
}
}
/* ========================================================= */
void sendRandomizerPulse() {
digitalWrite(randomizer_modul1_pin, HIGH);
digitalWrite(randomizer_modul2_pin, HIGH);
digitalWrite(randomizer_modul3_pin, HIGH);
delay(30);
digitalWrite(randomizer_modul1_pin, LOW);
digitalWrite(randomizer_modul2_pin, LOW);
digitalWrite(randomizer_modul3_pin, LOW);
allSolvedSent = false;
}