DS18B20 with serial PC

Dependencies:   DS18B20Sensor

Committer:
SereinTT
Date:
Tue Dec 04 16:04:15 2018 +0000
Revision:
2:1573a160c634
Parent:
main.cpp@1:901bf6de682c
DS18B20;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SereinTT 1:901bf6de682c 1 #include "mbed.h"
SereinTT 1:901bf6de682c 2 #include "DS18B20Sensor.h"
SereinTT 1:901bf6de682c 3
SereinTT 1:901bf6de682c 4 // Instantiate sensor with hardware in pin p30
SereinTT 1:901bf6de682c 5 DS18B20Sensor sensor(p30);
SereinTT 1:901bf6de682c 6
SereinTT 1:901bf6de682c 7 // Setup a window to the world
SereinTT 1:901bf6de682c 8 Serial pc(USBTX, USBRX);
SereinTT 1:901bf6de682c 9 //Serial pc(USBTX, USBRX);
SereinTT 1:901bf6de682c 10
SereinTT 1:901bf6de682c 11 DigitalOut myled(LED1);
SereinTT 1:901bf6de682c 12
SereinTT 1:901bf6de682c 13 int main()
SereinTT 1:901bf6de682c 14 {
SereinTT 1:901bf6de682c 15 char sensorBuf[25];
SereinTT 1:901bf6de682c 16 float num;
SereinTT 1:901bf6de682c 17 // count will search for sensors, if not already called
SereinTT 1:901bf6de682c 18 pc.printf("Found %d sensor/s\r", sensor.count());
SereinTT 1:901bf6de682c 19
SereinTT 1:901bf6de682c 20 uint8_t result = sensor.startReading(true); // start sensor readings and wait
SereinTT 1:901bf6de682c 21
SereinTT 1:901bf6de682c 22 for (uint8_t i = 0; i < sensor.count(); i++) {
SereinTT 1:901bf6de682c 23 sensor.getReading(sensorBuf, i); // get result into buf
SereinTT 1:901bf6de682c 24 pc.printf("Sensor %d : %s\r", i+1, sensorBuf); // display it to the world
SereinTT 1:901bf6de682c 25
SereinTT 1:901bf6de682c 26 num= atof (sensorBuf);
SereinTT 2:1573a160c634 27 pc.printf("result: %.2f\r ", atof (sensorBuf));
SereinTT 1:901bf6de682c 28 }
SereinTT 2:1573a160c634 29 if (atof (sensorBuf)>25){
SereinTT 1:901bf6de682c 30 myled=1;
SereinTT 1:901bf6de682c 31 }
SereinTT 1:901bf6de682c 32 else{
SereinTT 1:901bf6de682c 33 myled=0;
SereinTT 1:901bf6de682c 34 }
SereinTT 1:901bf6de682c 35 }