aggiunto autospegnimento e riorganizzazione codice, con commenti

This commit is contained in:
jops 2015-11-30 01:38:17 +01:00
parent 05efd8bba1
commit 9d3736027b

View file

@ -1,10 +1,11 @@
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#define RELAY1 8 #define RELAY1 8
SoftwareSerial mySerial(4, 2); // RX, TX SoftwareSerial mySerial(4, 2); // RX, TX (seriale su cui e' settato il bluetooth)
String command = ""; // Stores response of the HC-06 Bluetooth device String statoRele = 'f'; // salviamo lo stato del rele
char blueToothVal; //value sent over via bluetooth char blueToothVal; //value sent over via bluetooth
int contatoreSecondi = 0;
void setup() { void setup() {
@ -14,42 +15,50 @@ void setup() {
// The HC-06 defaults to 9600 according to the datasheet. // The HC-06 defaults to 9600 according to the datasheet.
mySerial.begin(9600); mySerial.begin(9600);
pinMode(13,OUTPUT); pinMode(13,OUTPUT); //il led
pinMode(RELAY1, OUTPUT); pinMode(RELAY1, OUTPUT); //il rele
contatoreSecondi = 0;
} }
void loop() { void loop() {
// Read device output if available. //Si autospegne se non riceve alcun messaggio per un ora
//if (mySerial.available()) { if (contatoreSecondi==3600){
// while(mySerial.available()) { // While there is more to be read, keep reading. digitalWrite(13,LOW); //spengo il led
// command += (char)mySerial.read(); digitalWrite(RELAY1,HIGH); //spengo il rele
// } statoRele = 'f';
blueToothVal=' ';
// Serial.println(command);
// command = ""; // No repeats }
//
//}
// Read user input if available. // Legge l'input dal raspeberry
if (mySerial.available()){ if (mySerial.available()){
//delay(10); // The delay is necessary to get this working! blueToothVal=(char)mySerial.read();
//mySerial.write(Serial.read());
blueToothVal=(char)mySerial.read(); //read it
} }
if (blueToothVal=='n')
{//if value from bluetooth serial is n
digitalWrite(13,HIGH); //switch on LED if (blueToothVal=='n')//se ricevo n lo accendo
digitalWrite(RELAY1,LOW); {
digitalWrite(13,HIGH); //accendo il led
digitalWrite(RELAY1,LOW); //accendo il rele
mySerial.write('n'); mySerial.write('n');
statoRele = blueToothVal;
blueToothVal=' '; blueToothVal=' ';
} contatoreSecondi = 0;
else if (blueToothVal=='f') }else if (blueToothVal=='f')//se ricevo f lo spengo
{//if value from bluetooth serial is n {
digitalWrite(13,LOW); //turn off LED digitalWrite(13,LOW); //spendo il led
digitalWrite(RELAY1,HIGH); digitalWrite(RELAY1,HIGH); //spengo il rele
mySerial.write('f'); mySerial.write('f');
statoRele = blueToothVal;
blueToothVal=' '; blueToothVal=' ';
contatoreSecondi = 0;
}else if(blueToothVal=='s'){ //richiedo lo stato
mySerial.write(statoRele); //mando lo stato
blueToothVal=' ';
contatoreSecondi = 0;
} }
delay(1000); delay(1000);
contatoreSecondi += 1;
} }