#include <stdio.h>
#define BUTTON_PIN 13
#define BUTTON_PRESSED 0
#define BUTTON_RELEASED 1
#define LED_PIN 12
#define LED_ON 1
#define LED_OFF 0
int lastButtonState = BUTTON_RELEASED;
char command[10];
int my_putChar(char ch, FILE *f) {
return Serial.write(ch);
}
int my_getChar(FILE *f) {
while (!Serial.available());
return Serial.read();
}
void setup(void) {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LED_OFF);
Serial.begin(9600);
FILE *my_stream = fdevopen(my_putChar, my_getChar);
stdin = stdout = my_stream;
printf("Sistem pornit. Comenzi disponibile:\n");
printf("- led on : pentru a aprinde LED-ul\n");
printf("- led off : pentru a stinge LED-ul\n");
}
void processSerialCommand() {
if (fgets(command, sizeof(command), stdin) != NULL) {
command[strcspn(command, "\n")] = 0;
if (strcmp(command, "led on") == 0) {
digitalWrite(LED_PIN, LED_ON);
printf("Comanda confirmata: LED aprins.\n");
} else if (strcmp(command, "led off") == 0) {
digitalWrite(LED_PIN, LED_OFF);
printf("Comanda confirmata: LED stins.\n");
} else {
printf("Comanda necunoscuta. Incercati 'led on' sau 'led off'.\n");
}
}
}
void loop(void) {
if (Serial.available()) {
processSerialCommand();
}
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == BUTTON_PRESSED) {
digitalWrite(LED_PIN, LED_ON);
printf("Buton apasat - LED aprins.\n");
} else if (buttonState == BUTTON_RELEASED) {
digitalWrite(LED_PIN, LED_OFF);
printf("Buton eliberat - LED stins.\n");
}
lastButtonState = buttonState;
}
delay(50);
}