Juan Manuel Rodriguez Aguado
/
Ejer01
SensorCooler
main.cpp@1:fe12bf2ad337, 2015-03-26 (annotated)
- Committer:
- hudakz
- Date:
- Thu Mar 26 20:58:32 2015 +0000
- Revision:
- 1:fe12bf2ad337
- Parent:
- 0:77a366f9ba45
- Child:
- 2:a680194ce6df
Sensor array sample added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:77a366f9ba45 | 1 | /* |
hudakz | 0:77a366f9ba45 | 2 | * Simple DS1820 sensor demo |
hudakz | 1:fe12bf2ad337 | 3 | * |
hudakz | 1:fe12bf2ad337 | 4 | * Note: Don't forget to connect a 4.7k Ohm resistor |
hudakz | 1:fe12bf2ad337 | 5 | * between the DS1820's data pin and the +3.3V pin |
hudakz | 1:fe12bf2ad337 | 6 | * |
hudakz | 0:77a366f9ba45 | 7 | */ |
hudakz | 0:77a366f9ba45 | 8 | |
hudakz | 0:77a366f9ba45 | 9 | #include "mbed.h" |
hudakz | 0:77a366f9ba45 | 10 | #include "DS1820.h" |
hudakz | 0:77a366f9ba45 | 11 | |
hudakz | 0:77a366f9ba45 | 12 | Serial serial(USBTX, USBRX); |
hudakz | 1:fe12bf2ad337 | 13 | |
hudakz | 0:77a366f9ba45 | 14 | int main() { |
hudakz | 1:fe12bf2ad337 | 15 | DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin |
hudakz | 1:fe12bf2ad337 | 16 | |
hudakz | 0:77a366f9ba45 | 17 | if(ds1820.begin()) { |
hudakz | 1:fe12bf2ad337 | 18 | ds1820.startConversion(); |
hudakz | 1:fe12bf2ad337 | 19 | wait(1.0); |
hudakz | 0:77a366f9ba45 | 20 | while(1) { |
hudakz | 1:fe12bf2ad337 | 21 | serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature |
hudakz | 1:fe12bf2ad337 | 22 | ds1820.startConversion(); // start temperature conversion |
hudakz | 1:fe12bf2ad337 | 23 | wait(1.0); // let DS1820 complete the temperature conversion |
hudakz | 0:77a366f9ba45 | 24 | } |
hudakz | 0:77a366f9ba45 | 25 | } else |
hudakz | 0:77a366f9ba45 | 26 | serial.printf("No DS1820 sensor found!\r\n"); |
hudakz | 0:77a366f9ba45 | 27 | } |
hudakz | 1:fe12bf2ad337 | 28 | |
hudakz | 1:fe12bf2ad337 | 29 | |
hudakz | 1:fe12bf2ad337 | 30 | // You can create an array of sensors: |
hudakz | 1:fe12bf2ad337 | 31 | |
hudakz | 1:fe12bf2ad337 | 32 | //int main() { |
hudakz | 1:fe12bf2ad337 | 33 | // DS1820 ds1820[3] = {DS1820(PA_8), DS1820(PA_9), DS1820(PC_7)}; |
hudakz | 1:fe12bf2ad337 | 34 | // |
hudakz | 1:fe12bf2ad337 | 35 | // for(int i = 0; i < 3; i++) { |
hudakz | 1:fe12bf2ad337 | 36 | // if(!ds1820[i].begin()) { |
hudakz | 1:fe12bf2ad337 | 37 | // serial.printf("Couldn't find sensor %d", i); |
hudakz | 1:fe12bf2ad337 | 38 | // } else |
hudakz | 1:fe12bf2ad337 | 39 | // ds1820[i].startConversion(); |
hudakz | 1:fe12bf2ad337 | 40 | // } |
hudakz | 1:fe12bf2ad337 | 41 | // |
hudakz | 1:fe12bf2ad337 | 42 | // wait(1.0); // let DS1820s complete the temperature conversion |
hudakz | 1:fe12bf2ad337 | 43 | // |
hudakz | 1:fe12bf2ad337 | 44 | // while(1) { |
hudakz | 1:fe12bf2ad337 | 45 | // for(int i = 0; i < 3; i++) { |
hudakz | 1:fe12bf2ad337 | 46 | // serial.printf("temp%d = %3.1f\r\n", i, ds1820[i].read()); // read temperature |
hudakz | 1:fe12bf2ad337 | 47 | // ds1820[i].startConversion(); // start temperature conversion |
hudakz | 1:fe12bf2ad337 | 48 | // } |
hudakz | 1:fe12bf2ad337 | 49 | // wait(1.0); // let DS1820s complete the temperature conversion |
hudakz | 1:fe12bf2ad337 | 50 | // } |
hudakz | 1:fe12bf2ad337 | 51 | //} |
hudakz | 1:fe12bf2ad337 | 52 | |
hudakz | 1:fe12bf2ad337 | 53 | |
hudakz | 1:fe12bf2ad337 | 54 | |
hudakz | 1:fe12bf2ad337 | 55 | |
hudakz | 1:fe12bf2ad337 | 56 |