#include <SoftwareSerial.h>
const int clk165 = 2;
const int latch165 = 3;
const int data165 = 4;
const int data595 = 5;
const int latch595 = 6;
const int clk595 = 7;
const int rxPin = 8;
const int txPin = 9;
SoftwareSerial LINK(rxPin, txPin);
byte buttonStates = 0;
byte lastButtonStates = 0;
byte feedbackStates = 0;
byte ledState = 0;
void setup() {
Serial.begin(9600);
LINK.begin(2400);
pinMode(clk165, OUTPUT);
pinMode(latch165, OUTPUT);
pinMode(data165, INPUT);
pinMode(data595, OUTPUT);
pinMode(latch595, OUTPUT);
pinMode(clk595, OUTPUT);
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
Serial.println("A1");
digitalWrite(clk165, LOW);
digitalWrite(latch165, HIGH);
//updateLEDs();*/
}
void loop() {
byte pressed = readButtons();
if (pressed) {
debugPressed(pressed);
handlePress(pressed);
}
// ⬅️ aktualizacja LED
receiveBistableState();
//delay(50);
}
// ODCZYT 74165
byte readButtons() {
digitalWrite(latch165, LOW);
delayMicroseconds(5);
digitalWrite(latch165, HIGH);
delayMicroseconds(5);
byte newStates = 0;
for (int i = 0; i < 8; i++) {
if (digitalRead(data165) == HIGH) {
newStates |= (1 << (7 - i));
}
digitalWrite(clk165, HIGH);
delayMicroseconds(5);
digitalWrite(clk165, LOW);
delayMicroseconds(2);
}
byte pressed = newStates & ~buttonStates;
buttonStates = newStates;
if (pressed) {
Serial.print("[HW] PRESS BIN: ");
for (int i = 7; i >= 0; i--) {
Serial.print((pressed >> i) & 1);
}
Serial.println();
}
return pressed;
}
void handlePress(byte pressed) {
Serial.print("[TX] SEND HEX: 0x");
Serial.println(pressed, HEX);
LINK.write(pressed);
delay(5);
}
void debugPressed(byte pressed) {
for (int i = 0; i < 8; i++) {
if (pressed & (1 << i)) {
Serial.print("[HW] BUTTON ");
Serial.print(i + 1);
Serial.println(" PRESSED");
}
}
}
void receiveBistableState() {
if (!LINK.available())
return;
ledState = LINK.read();
Serial.print("[LED] STATE: ");
Serial.println(ledState, BIN);
//writeLEDs(ledState);
write595(ledState);
}
void write595(byte value) {
digitalWrite(latch595, LOW);
delayMicroseconds(1);
shiftOut(data595, clk595, MSBFIRST, value);
delayMicroseconds(1);
digitalWrite(latch595, HIGH);
}
#include <SoftwareSerial.h>
const int clk165 = 2;
const int latch165 = 3;
const int data165 = 4;
const int data595 = 5;
const int latch595 = 6;
const int clk595 = 7;
const int rxPin = 8;
const int txPin = 9;
SoftwareSerial LINK(rxPin, txPin);
byte relayState = 0;
byte bistableState = 0;
byte lastBistableState = 0;
unsigned long relayOffTime = 0;
bool relayActive = false;
void setup() {
Serial.begin(9600);
LINK.begin(2400);
pinMode(data595, OUTPUT);
pinMode(clk595, OUTPUT);
pinMode(latch595, OUTPUT);
Serial.println("A2");
digitalWrite(data595, LOW);
digitalWrite(clk595, LOW);
digitalWrite(latch595, LOW);
digitalWrite(clk165, LOW);
digitalWrite(latch165, HIGH);
}
void loop() {
receiveCommand();
updateRelays();
updateBistableState();
}
void receiveCommand() {
if (!LINK.available())
return;
byte cmd = LINK.read();
Serial.print("[RX] GOT HEX: 0x");
Serial.println(cmd, HEX);
activateRelays(cmd);
}
void activateRelays(byte mask) {
relayState = mask;
write595(relayState);
relayOffTime = millis() + 500;
relayActive = true;
Serial.print("[RX] RELAYS ON: ");
Serial.println(relayState, BIN);
}
void updateRelays() {
if (!relayActive)
return;
if (millis() < relayOffTime)
return;
relayState = 0x00;
write595(relayState);
relayActive = false;
Serial.println("[RX] RELAYS OFF");
}
void write595(byte value) {
digitalWrite(latch595, LOW);
delayMicroseconds(1);
shiftOut(data595, clk595, MSBFIRST, value);
delayMicroseconds(1);
digitalWrite(latch595, HIGH);
}
byte readBistable165() {
digitalWrite(latch165, LOW);
delayMicroseconds(5);
digitalWrite(latch165, HIGH);
delayMicroseconds(5);
byte state = 0;
for (int i = 0; i < 8; i++) {
if (digitalRead(data165) == HIGH) {
state |= (1 << (7 - i));
}
digitalWrite(clk165, HIGH);
delayMicroseconds(5);
digitalWrite(clk165, LOW);
delayMicroseconds(2);
}
return state;
}
void updateBistableState() {
bistableState = readBistable165();
if (bistableState != lastBistableState) {
Serial.print("[165] BISTABLE RAW: ");
Serial.println(bistableState, BIN);
debugBistableChange(lastBistableState, bistableState);
// ⬅️ WYSYŁKA DO A1
LINK.write(bistableState);
lastBistableState = bistableState;
}
}
void debugBistableChange(byte oldState, byte newState) {
byte turnedOn = newState & ~oldState;
byte turnedOff = oldState & ~newState;
for (int i = 0; i < 8; i++) {
if (turnedOn & (1 << i)) {
Serial.print("[165] CHANNEL ");
Serial.print(i + 1);
Serial.println(" ON");
}
if (turnedOff & (1 << i)) {
Serial.print("[165] CHANNEL ");
Serial.print(i + 1);
Serial.println(" OFF");
}
}
}