// variabili globali per tenere i parametri di ogni led G giorno T tramont N notte A alba
// L Light il tempo di salita, O il tempo di output, F fading il tempo di discesa a 0
volatile int LG = 0, LT = 0, LN = 0, LA = 0;
volatile int OG = 0, OT = 0, ON = 0, OA = 0;
volatile int FG = 0, FT = 0, FN = 0, FA = 0;
// variabili globali per la seriale
volatile char messageBuffer[10];
volatile uint8_t bufferIndex = 0;
volatile bool messageReady = false;
void setupUSART9600NoParity() {
// Set baud rate to 9600
uint16_t ubrr_value = 103; // (16,000,000 / (16 * 9600)) - 1 = 103
UBRR0H = (uint8_t)(ubrr_value >> 8); // High byte of the baud rate
UBRR0L = (uint8_t)(ubrr_value); // Low byte of the baud rate
// Enable receiver, transmitter, and RX complete interrupt
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
// Set frame format: 8 data bits, 1 stop bit, no parity (8N1)
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // UCSZ01:0 = 11 for 8 data bits, no parity (UPM01 = 0, UPM00 = 0)
// Enable global interrupts
sei();
}
ISR(USART_RX_vect) {
// ISR to handle the RX Complete interrupt
char c = UDR0; // Read the received character from the USART data register
if (c == '\n' || c == '\r') { // End of message
messageBuffer[bufferIndex] = '\0'; // Null-terminate the string
messageReady = true; // Set the flag to indicate a complete message
bufferIndex = 0; // Reset buffer index for the next message
} else {
if (bufferIndex < sizeof(messageBuffer) - 1) {
messageBuffer[bufferIndex++] = c; // Store the character and increment the buffer index
}
}
}
bool parseAndValidateMessage(const char* message) {
// Extract the prefix and number from the message
char prefix[3] = {message[0], message[1], '\0'};
int value = atoi(&message[2]);
// Check that the value is less than 100
if (value >= 100) {
return false; // Invalid value, reject the message
}
// Assign the value to the appropriate variable based on the prefix
if (strcmp(prefix, "LG") == 0) {
LG = value;
} else if (strcmp(prefix, "LT") == 0) {
LT = value;
} else if (strcmp(prefix, "LN") == 0) {
LN = value;
} else if (strcmp(prefix, "LA") == 0) {
LA = value;
} else if (strcmp(prefix, "OG") == 0) {
OG = value;
} else if (strcmp(prefix, "OT") == 0) {
OT = value;
} else if (strcmp(prefix, "ON") == 0) {
ON = value;
} else if (strcmp(prefix, "OA") == 0) {
OA = value;
} else if (strcmp(prefix, "FG") == 0) {
FG = value;
} else if (strcmp(prefix, "FT") == 0) {
FT = value;
} else if (strcmp(prefix, "FN") == 0) {
FN = value;
} else if (strcmp(prefix, "FA") == 0) {
FA = value;
} else {
return false; // Invalid prefix, reject the message
}
return true; // Message is valid
}
void sendVariableStatus() {
// Create a string to hold the status message
char statusMessage[128];
snprintf(statusMessage, sizeof(statusMessage),
"LG=%d, LT=%d, LN=%d, LA=%d, OG=%d, OT=%d, ON=%d, OA=%d, FG=%d, FT=%d, FN=%d, FA=%d\n",
LG, LT, LN, LA, OG, OT, ON, OA, FG, FT, FN, FA);
// Print the status message on the serial monitor
for (uint8_t i = 0; statusMessage[i] != '\0'; i++) {
while (!(UCSR0A & (1 << UDRE0))); // Wait until the transmit buffer is empty
UDR0 = statusMessage[i]; // Transmit the character
}
}
void sendInvalidMessage() {
// Send the "message not valid" message
const char* invalidMessage = "message not valid\n";
for (uint8_t i = 0; invalidMessage[i] != '\0'; i++) {
while (!(UCSR0A & (1 << UDRE0))); // Wait until the transmit buffer is empty
UDR0 = invalidMessage[i]; // Transmit the character
}
}
void setup() {
// Initialize USART with interrupt and no parity bit
setupUSART9600NoParity();
}
void loop() {
static unsigned long lastUpdateTime = 0;
unsigned long currentTime = millis();
if (messageReady) {
messageReady = false; // Clear the flag
if (parseAndValidateMessage(messageBuffer)) {
sendVariableStatus(); // Send the status of all variables if a valid message is received
} else {
sendInvalidMessage(); // Send an error message if parsing fails
}
}
// Send the status every 5 seconds
if (currentTime - lastUpdateTime >= 5000) {
lastUpdateTime = currentTime;
sendVariableStatus();
}
// Other code can go here, the main loop is not blocked by serial communication
}