Gonzalo Martinez / Mbed 2 deprecated TPTemp

Dependencies:   mbed DS1820

Files at this revision

API Documentation at this revision

Comitter:
gonzaloMS
Date:
Wed Jun 19 12:41:27 2019 +0000
Parent:
11:1be668e096a2
Child:
13:00b2578c91be
Commit message:
TP del Cooler y Sensor de Temperatura DS1820

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 04 20:48:03 2019 +0000
+++ b/main.cpp	Wed Jun 19 12:41:27 2019 +0000
@@ -7,91 +7,196 @@
  */
  
 /* Single DS1820 sensor: */
-/*
+
 #include "mbed.h"
 #include "DS1820.h"
 
-Serial      pc(USBTX, USBRX);
+enum{LC,LC2,LA1,LA2,MT,PRESET,VM};
+
+InterruptIn rpm(D2);
+PwmOut velocidadMotor(A2);
+AnalogIn poter(A1);
+DigitalIn pulsonator(D5);
+
+Ticker tiempo;
+
+int tempo=0;
+
+void tiempoFuncion();
+void select();
+void worker();
+void cuentaRpm();
+
+int lala=0;
+int valorRpm=0;
+int lc=1,la=2,does=la,previously=0;
+float t=0,p=0,vem=0;
+int pres;
+int estadoSelect, estadoWorker;
+float algo;
+
 DigitalOut  led(LED1);
-DS1820      ds1820(D8);  // substitute D8 with the actual pin name connected to the DS1820 sensor
+DS1820      ds1820(A0);  // substitute D8 with the actual pin name connected to the DS1820 sensor
 float       temp = 0;
 int         result = 0;
+int pulsator;
 
 int main()
 {
+    tiempo.attach(&tiempoFuncion,0.01);
+    rpm.rise(&cuentaRpm);
     if (ds1820.begin()) {
         while (1) {
+            velocidadMotor=vem;
+            pulsator=pulsonator;
+            algo=poter.read();
+            select();
+            worker();
             ds1820.startConversion();   // start temperature conversion from analog to digital
-            wait(1.0);                  // let DS1820 complete the temperature conversion
+            wait(1);                  // let DS1820 complete the temperature conversion
             result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
             switch (result) {
                 case 0:                 // no errors -> 'temp' contains the value of measured temperature
-                    pc.printf("temp = %3.1f%cC\r\n", temp, 176);
+                    printf("temp = %3.1f%cC\r\n", temp, 176);
+                    printf("Velocidad Motor (0-1): %1.2f \n",vem);
                     break;
 
                 case 1:                 // no sensor present -> 'temp' was not updated
-                    pc.printf("no sensor present\n\r");
+                    printf("no sensor present\n\r");
                     break;
 
                 case 2:                 // CRC error -> 'temp' was not updated
-                    pc.printf("CRC error\r\n");
+                    printf("CRC error\r\n");
             }
 
             led = !led;
         }
     }
     else
-        pc.printf("No DS1820 sensor found!\r\n");
+        printf("No DS1820 sensor found!\r\n");
 }
-*/
-
-
-/*Several DS1820 sensors connected to the 1-wire bus:*/
-#include "mbed.h"
-#include "DS1820.h"
 
-#define     MAX_SENSOSRS   32   // max number of DS1820 sensors to be connected to the 1-wire bus (max 256)
-
-DS1820*     ds1820[MAX_SENSOSRS];
-Serial      pc(USBTX, USBRX);
-DigitalOut  led(LED1);
-OneWire     oneWire(D8);        // substitute D8 with the actual pin name connected to the 1-wire bus
-int         sensorsFound = 0;   // counts the actually found DS1820 sensors
-
-int main()
+void tiempoFuncion()
 {
-    pc.printf("\r\n--Starting--\r\n");
-    
-    //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
-    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
-        ds1820[sensorsFound] = new DS1820(&oneWire);
-        if (!ds1820[sensorsFound]->begin()) {
-            delete ds1820[sensorsFound];
-            break;
-        }
+    if(lala!=0)
+    {
+        tempo++;   
     }
-
-    switch (sensorsFound) {
-        case 0:
-            pc.printf("No DS1820 sensor found!\r\n");
-            return -1;
-
-        case 1:
-            pc.printf("One DS1820 sensor found.\r\n");
-            break;
-
-        default:
-            pc.printf("Found %d DS1820 sensors.\r\n", sensorsFound);
+    else
+    {
+        valorRpm=0;    
     }
-
-    while (1) {
-        pc.printf("----------------\r\n");
-        for (int i = 0; i < sensorsFound; i++)
-            ds1820[i]->startConversion();       // start temperature conversion from analog to digital
-        wait(1.0);                              // let DS1820 sensors complete the temperature conversion
-        for (int i = 0; i < sensorsFound; i++) {
-            if (ds1820[i]->isPresent())
-                pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
-        }
+    if(tempo/100==1)
+    {
+        valorRpm=lala*60;
+        lala=0;
+        tempo=0;
     }
 }
+
+void cuentaRpm()
+{
+    lala++;
+}
+
+void select(){
+    switch(estadoSelect)
+    {
+        case LC:
+        does=lc;
+        if(pulsonator==0)
+        {
+            estadoSelect=LC;
+        }else
+        {
+            estadoSelect=LA1;
+        }
+        break;
+        
+        case LA1:
+        does=la;
+        if(pulsonator==1)
+        {
+            estadoSelect=LA1;
+        }else
+        {
+            estadoSelect=LA2;
+        }
+        break;
+        
+        case LA2:
+        does=la;
+        if(pulsonator==0)
+        {
+            estadoSelect=LA2;
+        }else
+        {
+            estadoSelect=LC2;
+        }
+        break;
+        
+        case LC2:
+        does=lc;
+        if(pulsonator==1)
+        {
+            estadoSelect=LC2;   
+        }
+        else
+        {
+            estadoSelect=LC;   
+        }
+        break;  
+    }
+}
+
+void worker(){    
+    if(does==lc)
+    {
+        printf("Lazo cerrado \n");   
+    }
+    else if(does==la)
+    {
+        printf("Lazo abierto \n");      
+    }
+    printf("percentage: %3.3f%%\n", algo*100.0f);
+    printf("RPM: %d \n\n",valorRpm);
+    switch(estadoWorker)
+    {
+        default:
+        case MT:
+        if(does==lc)
+        {
+            t=temp;
+            previously=0;
+            estadoWorker=VM;
+        }else
+        {
+            estadoWorker=PRESET;
+        }
+        break;
+        
+        case PRESET:
+        if(does==la)
+        {  
+            previously=1;
+            estadoWorker=VM;
+        }else
+        {
+            estadoWorker=MT;
+        }
+        break;
+        
+        case VM:
+        if(previously==0)
+        {
+            vem=t/float(100);
+            estadoWorker=MT;
+        }else
+        {
+            vem=algo; 
+            estadoWorker=PRESET;
+        }
+        break;
+        
+    }
+}
\ No newline at end of file