Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]

Dependencies:   DS1820

Revision:
10:739d1a7a4b1b
Parent:
9:1d1c57840c7e
Child:
12:34123e51ac35
--- a/main.cpp	Sun Mar 10 13:20:39 2019 +0000
+++ b/main.cpp	Sun Mar 10 20:59:00 2019 +0000
@@ -1,24 +1,24 @@
 /*
- * Simple DS1820 sensor demo
+ * Simple example program
  *
  * Note: Don't forget to connect a 4.7k Ohm resistor 
  *       between the DS1820's data pin and the +3.3V pin
  *
  */
-/* Single sensor: */
+ 
+/* Single DS1820 sensor: */
 /*
 #include "mbed.h"
 #include "DS1820.h"
 
 Serial      pc(USBTX, USBRX);
 DigitalOut  led(LED1);
-OneWire     oneWire(D8);    // substitute D8 with actual mbed pin name connected 1-wire bus
+DS1820      ds1820(D8);  // substitute D8 with the actual pin name connected to the DS1820 sensor
 float       temp = 0;
 int         result = 0;
 
 int main()
 {
-    pc.printf("\r\n--Starting--\r\n");
     if (ds1820.begin()) {
         while (1) {
             ds1820.startConversion();   // start temperature conversion from analog to digital
@@ -29,11 +29,11 @@
                     pc.printf("temp = %3.1f%cC\r\n", temp, 176);
                     break;
 
-                case 1:                 // no sensor present -> 'temp' is not updated
+                case 1:                 // no sensor present -> 'temp' was not updated
                     pc.printf("no sensor present\n\r");
                     break;
 
-                case 2:                 // CRC error -> 'temp' is not updated
+                case 2:                 // CRC error -> 'temp' was not updated
                     pc.printf("CRC error\r\n");
             }
 
@@ -45,52 +45,53 @@
 }
 */
 
-/*Several sensors connected to the same 1-wire bus:*/
 
+/*Several DS1820 sensors connected to the 1-wire bus:*/
 #include "mbed.h"
 #include "DS1820.h"
 
-#define     SENSORS_COUNT   64      // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
+#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 actual mbed pin name connected 1-wire bus
-DS1820*     ds1820[SENSORS_COUNT];
-int         sensors_found = 0;      // counts the actually found DS1820 sensors
-float       temp = 0;
-int         result = 0;
+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() {
-    int i = 0;
+int main()
+{
+    pc.printf("\r\n--Starting--\r\n");
     
-    pc.printf("\r\n--Starting--\r\n");
     //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
-    for(i = 0; i < SENSORS_COUNT; i++) {
-        ds1820[i] = new DS1820(&oneWire);
-        if(!ds1820[i]->begin()) {
-            delete ds1820[i];
+    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
+        ds1820[sensorsFound] = new DS1820(&oneWire);
+        if (!ds1820[sensorsFound]->begin()) {
+            delete ds1820[sensorsFound];
             break;
         }
     }
-    
-    sensors_found = i;
-    
-    if (sensors_found == 0) {
-        pc.printf("No DS1820 sensor found!\r\n");
-        return -1;
+
+    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
-        pc.printf("Found %d sensors.\r\n", sensors_found);
-    
-    while(1) {
-        pc.printf("-------------------\r\n");
-        for(i = 0; i < sensors_found; i++)
-            ds1820[i]->startConversion();   // start temperature conversion from analog to digital       
-        wait(1.0);                          // let DS1820s complete the temperature conversion
-        for(int i = 0; i < sensors_found; i++) {
-            if(ds1820[i]->isPresent())
-                pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);     // read temperature
+
+    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
         }
     }
 }
-