#include <SoftwareSerial.h>
const int GlassHeater = 13;
#define OnOff 3
#define BluetoothRx 0
#define BluetoothTx 1
byte lastButtonState = LOW;
byte ledState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
SoftwareSerial BTSerial(BluetoothRx, BluetoothTx); // Krijon një lidhje SoftwareSerial për komunikimin Bluetooth
void setup() {
pinMode(GlassHeater, OUTPUT);
pinMode(OnOff, INPUT_PULLUP);
Serial.begin(9600); // Nis komunikimin seris me kompjuterin
BTSerial.begin(9600); // Nis komunikimin Bluetooth me pajisjen tjetër
}
void loop() {
// Kontrollon për ndryshime në gjendjen e butonit dhe dërgon sinjal nëpërmjet Bluetooth
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(OnOff);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(GlassHeater, ledState);
// Dërgon sinjalin për gjendjen e ngrohësit nëpërmjet Bluetooth
BTSerial.print("Ngrohësi është ");
BTSerial.println(ledState == HIGH ? "ndezur." : "fikur.");
}
}
}
// Kontrollon për sinjale të pranuara nga Bluetooth dhe vepron në përputhje
while (BTSerial.available()) {
char receivedChar = (char)BTSerial.read();
if (receivedChar == '1') {
digitalWrite(GlassHeater, HIGH); // Ndiz LED-in (ngrohësin)
} else if (receivedChar == '0') {
digitalWrite(GlassHeater, LOW); // Fik LED-in (ngrohësin)
}
}
}