Code for collecting sensor data
Dependencies: SX1276Lib_inAir mbed
Fork of Sensors by
main.cpp@2:804e04f4f217, 2016-09-01 (annotated)
- Committer:
- Razorfoot
- Date:
- Thu Sep 01 08:12:24 2016 +0000
- Revision:
- 2:804e04f4f217
- Parent:
- 1:059293827555
Door functionality added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Razorfoot | 1:059293827555 | 1 | #include <stdio.h> |
Razorfoot | 0:b4b76706c8c3 | 2 | #include "mbed.h" |
Razorfoot | 0:b4b76706c8c3 | 3 | #include "sx1276-inAir.h" |
Razorfoot | 0:b4b76706c8c3 | 4 | |
Razorfoot | 2:804e04f4f217 | 5 | #define STRING_LENGTH 30 |
Razorfoot | 1:059293827555 | 6 | #define SUPPLY_VOLTAGE 3.3 |
Razorfoot | 1:059293827555 | 7 | |
Razorfoot | 0:b4b76706c8c3 | 8 | Serial pc(USBTX, USBRX); //Create a serial connection to the PC |
Razorfoot | 0:b4b76706c8c3 | 9 | AnalogIn ain(PA_0); //Configure pin PA0 as an analog input for the temperature sensor |
Razorfoot | 2:804e04f4f217 | 10 | //DigitalIn din(PB_10); //Configure pin PC2 as a digital input for the reed switch |
Razorfoot | 2:804e04f4f217 | 11 | InterruptIn door(PB_10); |
Razorfoot | 0:b4b76706c8c3 | 12 | |
Razorfoot | 0:b4b76706c8c3 | 13 | float reading_float; |
Razorfoot | 2:804e04f4f217 | 14 | bool reading_bool; |
Razorfoot | 0:b4b76706c8c3 | 15 | |
Razorfoot | 0:b4b76706c8c3 | 16 | //Return the temperature from the sensor, in degrees celsius |
Razorfoot | 2:804e04f4f217 | 17 | float getTemp() { |
Razorfoot | 0:b4b76706c8c3 | 18 | float reading = ain.read(); |
Razorfoot | 0:b4b76706c8c3 | 19 | float output_voltage = reading * SUPPLY_VOLTAGE; |
Razorfoot | 0:b4b76706c8c3 | 20 | return (output_voltage - 0.25) / 0.028; |
Razorfoot | 0:b4b76706c8c3 | 21 | } |
Razorfoot | 0:b4b76706c8c3 | 22 | |
Razorfoot | 2:804e04f4f217 | 23 | //Return the door state. If True, the door is closed. If False, the door is open. |
Razorfoot | 2:804e04f4f217 | 24 | bool getDoorState() { |
Razorfoot | 2:804e04f4f217 | 25 | return door.read(); |
Razorfoot | 2:804e04f4f217 | 26 | } |
Razorfoot | 2:804e04f4f217 | 27 | |
Razorfoot | 2:804e04f4f217 | 28 | //Code that executes on a rising edge |
Razorfoot | 2:804e04f4f217 | 29 | //For reasons unknown, this executes twice instead of once when the magnet connects |
Razorfoot | 2:804e04f4f217 | 30 | //i.e. there are two falling edges. Could implement debouncing, but that takes effort |
Razorfoot | 2:804e04f4f217 | 31 | void transmitDoorClosing() { |
Razorfoot | 2:804e04f4f217 | 32 | printf("The door has been closed\r\n"); |
Razorfoot | 2:804e04f4f217 | 33 | } |
Razorfoot | 2:804e04f4f217 | 34 | |
Razorfoot | 2:804e04f4f217 | 35 | //Code that executes on a falling edge |
Razorfoot | 2:804e04f4f217 | 36 | void transmitDoorOpening() { |
Razorfoot | 2:804e04f4f217 | 37 | printf("The door has been opened\r\n"); |
Razorfoot | 2:804e04f4f217 | 38 | } |
Razorfoot | 2:804e04f4f217 | 39 | |
Razorfoot | 0:b4b76706c8c3 | 40 | |
Razorfoot | 0:b4b76706c8c3 | 41 | int main() { |
Razorfoot | 0:b4b76706c8c3 | 42 | |
Razorfoot | 0:b4b76706c8c3 | 43 | //Configure the serial connection (baud rate = 19200, 8 data bits, 1 stop bit) |
Razorfoot | 0:b4b76706c8c3 | 44 | pc.baud(9600); |
Razorfoot | 0:b4b76706c8c3 | 45 | pc.format(8, SerialBase::None, 1); |
Razorfoot | 2:804e04f4f217 | 46 | |
Razorfoot | 2:804e04f4f217 | 47 | door.rise(transmitDoorClosing); //Rising edge occurs when the "door opens" |
Razorfoot | 2:804e04f4f217 | 48 | door.fall(transmitDoorOpening); //Falling edge occurs when the "door closes" |
Razorfoot | 0:b4b76706c8c3 | 49 | |
Razorfoot | 0:b4b76706c8c3 | 50 | while(1) { |
Razorfoot | 0:b4b76706c8c3 | 51 | |
Razorfoot | 2:804e04f4f217 | 52 | char reading_string[STRING_LENGTH]; |
Razorfoot | 2:804e04f4f217 | 53 | reading_float = getTemp(); |
Razorfoot | 2:804e04f4f217 | 54 | sprintf(reading_string, " Temp: %.8f\r\n", reading_float); |
Razorfoot | 2:804e04f4f217 | 55 | pc.printf("Temperature = %s\r\n", reading_string); |
Razorfoot | 0:b4b76706c8c3 | 56 | |
Razorfoot | 0:b4b76706c8c3 | 57 | wait_ms(500); |
Razorfoot | 0:b4b76706c8c3 | 58 | |
Razorfoot | 0:b4b76706c8c3 | 59 | } |
Razorfoot | 0:b4b76706c8c3 | 60 | } |