Compare commits

..

2 Commits

Author SHA1 Message Date
5080a004af Simplify drawer archive 2026-01-15 14:11:59 +01:00
a2945bad9a Add code for archiv drawers 2026-01-15 12:43:23 +01:00
3 changed files with 109 additions and 0 deletions

5
drawers/.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

14
drawers/platformio.ini Normal file
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:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

90
drawers/src/main.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <Arduino.h>
#include <Wire.h>
const uint8_t I2C_ADDR = 0x10;
const uint8_t CMD_POWER_OFF = 0x01;
const uint8_t CMD_LIGHT_INDEX = 0x02;
const uint8_t CMD_DRAWER_INDEX = 0x03;
struct Schublade {
int schlossPin;
int ledPin;
};
Schublade schubladen[8] = {
{6, 30},
{9, 32},
{7, 31},
{8, 33},
{2, 29},
{12, 28},
{11, 27},
{10, 26}
};
void turnAllLedsOf() {
for (int i = 0; i < 8; i++) {
digitalWrite(schubladen[i].ledPin, LOW);
}
}
void turnOnSingleLed(uint8_t indexOn) {
if (indexOn < 0 || indexOn >= 8)
return;
turnAllLedsOf();
digitalWrite(schubladen[indexOn].ledPin, HIGH);
}
const int LOCK_THRESHOLD = 750;
unsigned long lockTimer = 0;
bool unlocked = false;
void openLock(uint8_t indexOn) {
unlocked = true;
lockTimer = millis();
digitalWrite(schubladen[indexOn].schlossPin, HIGH);
}
void onI2CReceive(int len) {
while (Wire.available()) {
uint8_t cmd = Wire.read();
switch (cmd) {
case CMD_POWER_OFF:
turnAllLedsOf();
break;
case CMD_LIGHT_INDEX:
uint8_t index = Wire.read();
turnOnSingleLed(index);
break;
case CMD_DRAWER_INDEX:
uint8_t index = Wire.read();
openLock(index);
break;
}
}
}
void setup() {
Wire.begin(I2C_ADDR);
Wire.onReceive(onI2CReceive);
for (int i = 0; i < 8; i++) {
pinMode(schubladen[i].ledPin, OUTPUT);
pinMode(schubladen[i].schlossPin, OUTPUT);
digitalWrite(schubladen[i].ledPin, LOW);
digitalWrite(schubladen[i].schlossPin, LOW);
}
}
void loop() {
if (unlocked && millis() - lockTimer >= LOCK_THRESHOLD) {
for (int i = 0; i < 8; i++) {
digitalWrite(schubladen[i].schlossPin, LOW);
}
unlocked = false;
}
}