Explorar o código

aggiunto autospegnimento e riorganizzazione codice, con commenti

jops %!s(int64=8) %!d(string=hai) anos
pai
achega
9d3736027b
Modificáronse 1 ficheiros con 37 adicións e 28 borrados
  1. 37 28
      arduino/rele/rele.ino

+ 37 - 28
arduino/rele/rele.ino

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