Control fan speed by temperature sensor reading. Set point can be adjusted by buttons.

Dependencies:   mbed DebounceIn TextLCD DS1820

Revision:
1:d6ceed323d25
Parent:
0:af20639f9907
--- a/main.cpp	Thu Dec 02 11:43:48 2021 +0000
+++ b/main.cpp	Thu Dec 09 02:06:26 2021 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "DebounceIn.h"
+#include "DS1820.h"
 
 /*
    This basic example just shows how to read the ADC internal channels raw values.
@@ -9,7 +10,7 @@
 */
 
 // Temperature input
-AnalogIn adc_temp1(A0);
+//AnalogIn adc_temp1(A0);
 AnalogIn adc_temp2(A1);
 AnalogIn adc_temp3(A2);
 
@@ -27,27 +28,55 @@
 // PWM
 PwmOut fan(D12);
 
+// Temp
+DS1820 ds1820(A0);
+
 uint8_t tempSetValue = 25; // degree
 bool fanOn = false;
 float fanDutyCycle = 0.0f;
 
+uint8_t ds1820Counter = 0;
+uint8_t temp1 = 0;
+
 //const uint8_t PRESS_DURATION = 5;
 
 int main()
 {
-    // Configure
+    // Configure pull-up
     tempUp.mode(PullUp);
     tempDown.mode(PullUp);
     onOff.mode(PullUp);
+    // 100 ms debounce
+    tempUp.set_samples(100);
+    tempDown.set_samples(100);
+    onOff.set_samples(100);
+    
     fan.period(4.0f); // period
     fan.write(fanDutyCycle); // duty cycle
     
+    if (ds1820.begin()) lcd.printf("DS1820 found!");
+    else lcd.printf("DS1820 not found");
+    wait(1.0);
+    lcd.cls();
     // Loop
     while(1) {
-        float temp1 = (adc_temp1.read()*3.3f*100.0f);
-        lcd.printf("Temp1 = %.1f\n", temp1);
-        lcd.printf("");
-        printf("\033[2A");
+        //uint8_t temp1 = static_cast<uint8_t>(adc_temp1.read()*3.3f*100.0f);
+        
+        if (ds1820Counter == 0) ds1820.startConversion();
+        if(ds1820Counter++ == 50)  {
+            float ds1820Temp = 0.0f;
+            if(ds1820.read(ds1820Temp) == 0) {
+                temp1 = static_cast<uint8_t>(ds1820Temp);
+            }
+            ds1820Counter = 0;
+        }
+        
+        uint8_t temp2 = static_cast<uint8_t>(adc_temp2.read()*3.3f*100.0f);
+        uint8_t temp3 = static_cast<uint8_t>(adc_temp3.read()*3.3f*100.0f);
+        
+        lcd.printf("T1=%02d, T2=%02d\n", temp1, temp2);
+        lcd.printf("T3=%02d, SP=%02d\n", temp3, tempSetValue);
+        
         
         if(tempUp == 0) tempSetValue++;
         if(tempDown == 0) tempSetValue--;