Buongiorno a tutti mi servirebbe una mano da un programmatore esperto. Il pulsante viene configurato come input con pull-up interno, I pin dei led vengono configurati come output. Il pin di errore viene impostato a LOW. La comunicazione seriale viene inizializzata alla velocità di 9600 baud. Viene attivato il watchdog interno della scheda.
La funzione checkButtonState1() verifica lo stato del pulsante handleButtonPress()
La funzione handleButtonPress() invia un comando alla scheda e attiva un ack (acknowledgement) per indicare comando ricevuto.
La funzione handleWatchdog() controlla il tempo trascorso dall'ultimo reset E' presente anche il bit di parità che viene utilizzato per controllare la correttezza della trasmissione del messaggio.
#include <SoftwareSerial.h>
#include <avr/wdt.h>
#define RX_PIN 0
#define TX_PIN 1
#define BUTTON_PIN 2
#define LED_PIN 3
#define ERROR_LED_PIN 4
#define TIMEOUT 1000
#define WATCHDOG_TIMEOUT 5000
#define DEBOUNCE_TIME 50
SoftwareSerial serial(RX_PIN, TX_PIN);
bool buttonState1 = false;
bool ack1 = false;
bool ack2 = false;
unsigned long lastPress1 = 0;
bool ledState = false;
bool errorState = false;
unsigned long lastWatchdogReset = 0;
void checkButtonState1(unsigned long now);
void checkReceivedMessage();
void toggleLED();
void handleButtonPress();
void handleSerialCommunication();
void handleWatchdog();
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(ERROR_LED_PIN, OUTPUT);
digitalWrite(ERROR_LED_PIN, LOW);
serial.begin(9600);
wdt_enable(WDTO_8S);
}
void loop() {
handleSerialCommunication();
checkReceivedMessage();
unsigned long now = millis();
checkButtonState1(now);
toggleLED();
handleWatchdog();
digitalWrite(LED_PIN, ledState);
digitalWrite(ERROR_LED_PIN, errorState);
}
void checkButtonState1(unsigned long now) {
static bool buttonState1Prev = false;
bool reading1 = !digitalRead(BUTTON_PIN);
if (reading1 != buttonState1Prev) {
buttonState1Prev = reading1;
if (buttonState1Prev) {
lastPress1 = now;
handleButtonPress();
}
}
}
void checkReceivedMessage() {
if (serial.available()) {
char message = serial.read();
bool parityBit = false;
int bitCount = 0;
for (int i = 0; i < 7; i++) {
if (bitRead(message, i) == 1) {
bitCount++;
}
}
if (bitCount % 2 == 0) {
parityBit = true;
}
if (bitRead(message, 7) == parityBit) {
if (message == '1') {
ack1 = true;
} else if (message == '2') {
ack2 = true;
}
}
}
}
void toggleLED() {
if (ack1 && ack2) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
ack1 = false;
ack2 = false;
}
}
void handleButtonPress() {
if (!ack1) {
serial.write('1');
ack1 = true;
} else if (!ack2) {
serial.write('2');
ack2 = true;
}
}
void handleSerialCommunication() {
if (serial.available()) {
char message = serial.read();
bool parityBit = false;
int bitCount = 0;
for (int i = 0; i < 7; i++) {
if (bitRead(message, i) == 1) {
bitCount++;
}
}
if (bitCount % 2 == 0) {
parityBit = true;
}
if (bitRead(message, 7) == parityBit) {
if (message == '1') {
ack1 = true;
} else if (message == '2') {
ack2 = true;
}
}
}
}
void handleWatchdog() {
if (millis() - lastWatchdogReset > WATCHDOG_TIMEOUT) {
wdt_reset();
lastWatchdogReset = millis();
}
}