Forráskód Böngészése

aggiunta pulsante accezione

jops 7 éve
szülő
commit
a4615b8133
1 módosított fájl, 76 hozzáadás és 16 törlés
  1. 76 16
      arduino/rele/rele.ino

+ 76 - 16
arduino/rele/rele.ino

@@ -7,6 +7,22 @@ char statoRele = 'n'; // salviamo lo stato del rele
 char blueToothVal;           //value sent over via bluetooth
 int contatoreSecondi = 0;
 
+const int buttonPin = 11;     //pin del pulsante
+int buttonState = 0;  // stato del pulsante
+
+const int ledPin = 13;     //pin del led stato caldaia
+
+void startCaldaia(){
+    digitalWrite(ledPin,HIGH);      //accendo il led
+    digitalWrite(RELAY1,HIGH); //accendo il rele
+    statoRele = '1';
+}
+
+void stopCaldaia(){
+    digitalWrite(ledPin,LOW);      //spengo il led
+    digitalWrite(RELAY1,LOW);   //spengo il rele
+    statoRele = '0';
+}
 
 void setup() {
   // Open serial communications:
@@ -15,17 +31,18 @@ void setup() {
   
   // The HC-06 defaults to 9600 according to the datasheet.
   mySerial.begin(9600);
-  pinMode(13,OUTPUT); //il led
+  
+  pinMode(ledPin,OUTPUT); //il led
   pinMode(RELAY1, OUTPUT);  //il rele
+  pinMode(buttonPin, INPUT);  //pulsante
+  
   contatoreSecondi = 0;
 }
 
 void loop() {
   //Si autospegne se non riceve alcun messaggio per un ora
   if (contatoreSecondi > 3600){   
-    digitalWrite(13,HIGH);      //accendo il led
-    digitalWrite(RELAY1,LOW);   //accendo il rele
-    statoRele = 'f';
+    stopCaldaia();
     blueToothVal=' ';
 
   }
@@ -36,36 +53,79 @@ void loop() {
   }
   
   
-  if (blueToothVal=='n')//se ricevo n lo spengo
+  if (blueToothVal=='0')//se ricevo 0 lo spengo
   {
-    digitalWrite(13,HIGH);      //accendo il led
-    digitalWrite(RELAY1,LOW);   //accendo il rele
-    mySerial.write('n\n');
-    statoRele = blueToothVal;
+    stopCaldaia();
+    mySerial.write('0\n');
     blueToothVal=' ';
     contatoreSecondi = 0;
-  }else if (blueToothVal=='f')//se ricevo f lo accendo
+  }
+  else if (blueToothVal=='1') //se ricevo 1 lo accendo
   {
-    digitalWrite(13,LOW);      //spendo il led
-    digitalWrite(RELAY1,HIGH); //spengo il rele
-    mySerial.write('f\n');
-    statoRele = blueToothVal;
+    startCaldaia();
+    mySerial.write('1\n');
     blueToothVal=' ';
     contatoreSecondi = 0;
-  }else if(blueToothVal=='s'){  //richiedo lo stato
+  }
+  else if(blueToothVal=='s'){  //richiedo lo stato e azzero il contatore
     mySerial.write(statoRele);  //mando lo stato
     mySerial.write("\n");
     blueToothVal=' ';
     contatoreSecondi = 0;
-  }else if(blueToothVal=='c'){  //info sul contatore
+  }
+  else if(blueToothVal=='t'){  //info sul contatore
     String myString = String(contatoreSecondi); 
     char charBuf[50];
     myString.toCharArray(charBuf, 50);    
     strcat(charBuf, "\n");
     mySerial.write(charBuf);  //mando lo stato
   }
+  
+  switch (blueToothVal) {
+    case '0'://se ricevo 0 lo spengo
+      stopCaldaia();
+      mySerial.write('0\n');
+      blueToothVal=' ';
+      contatoreSecondi = 0;
+      break;
+    case '1'://se ricevo 1 lo accendo
+      startCaldaia();
+      mySerial.write('1\n');
+      blueToothVal=' ';
+      contatoreSecondi = 0;
+      break;
+    case 's'://richiedo lo stato e azzero il contatore
+      mySerial.write(statoRele);  //mando lo stato
+      mySerial.write("\n");
+      blueToothVal=' ';
+      contatoreSecondi = 0;
+      break;
+    
+    default:
+      // if nothing else matches, do the default
+      // default is optional
+    break;
+  }
 
+
+  //Parte pulsante
+  buttonState = digitalRead(buttonPin);
+  
+  if (buttonState == HIGH) {
+    if(statoRele=='0'){
+      startCaldaia();
+      blueToothVal=' ';
+      contatoreSecondi = 0;
+    }else{
+      stopCaldaia();
+      blueToothVal=' ';
+      contatoreSecondi = 0;
+    }
+  }
+  //fine parte pulsant
+  
   delay(1000);
+    
   contatoreSecondi += 1;
 
 }