Project D - Weather Station
Dependencies: BMP180 N5110 beep mbed
main.cpp@0:e0e85d08133d, 2015-05-10 (annotated)
- Committer:
- oe386961252
- Date:
- Sun May 10 00:12:30 2015 +0000
- Revision:
- 0:e0e85d08133d
Project D - Weather Station
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oe386961252 | 0:e0e85d08133d | 1 | #include "mbed.h" |
oe386961252 | 0:e0e85d08133d | 2 | #include "N5110.h" |
oe386961252 | 0:e0e85d08133d | 3 | #include "beep.h" |
oe386961252 | 0:e0e85d08133d | 4 | #include "BMP180.h" |
oe386961252 | 0:e0e85d08133d | 5 | |
oe386961252 | 0:e0e85d08133d | 6 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
oe386961252 | 0:e0e85d08133d | 7 | Beep buzzer(p21); |
oe386961252 | 0:e0e85d08133d | 8 | BusOut leds(LED4,LED3,LED2,LED1); |
oe386961252 | 0:e0e85d08133d | 9 | BMP180 bmp180(p28,p27); |
oe386961252 | 0:e0e85d08133d | 10 | InterruptIn button(p15); |
oe386961252 | 0:e0e85d08133d | 11 | DigitalOut led(p24); |
oe386961252 | 0:e0e85d08133d | 12 | |
oe386961252 | 0:e0e85d08133d | 13 | #define up 1 |
oe386961252 | 0:e0e85d08133d | 14 | #define down 0 |
oe386961252 | 0:e0e85d08133d | 15 | |
oe386961252 | 0:e0e85d08133d | 16 | int state = up; |
oe386961252 | 0:e0e85d08133d | 17 | |
oe386961252 | 0:e0e85d08133d | 18 | int buttonFlag = 0; |
oe386961252 | 0:e0e85d08133d | 19 | |
oe386961252 | 0:e0e85d08133d | 20 | void buttonPressed() |
oe386961252 | 0:e0e85d08133d | 21 | { |
oe386961252 | 0:e0e85d08133d | 22 | state = ! state; |
oe386961252 | 0:e0e85d08133d | 23 | } |
oe386961252 | 0:e0e85d08133d | 24 | |
oe386961252 | 0:e0e85d08133d | 25 | |
oe386961252 | 0:e0e85d08133d | 26 | |
oe386961252 | 0:e0e85d08133d | 27 | |
oe386961252 | 0:e0e85d08133d | 28 | int main() |
oe386961252 | 0:e0e85d08133d | 29 | { |
oe386961252 | 0:e0e85d08133d | 30 | lcd.init(); |
oe386961252 | 0:e0e85d08133d | 31 | bmp180.init(); |
oe386961252 | 0:e0e85d08133d | 32 | |
oe386961252 | 0:e0e85d08133d | 33 | //startup picture |
oe386961252 | 0:e0e85d08133d | 34 | //the project name is Weather Station, print it on screen |
oe386961252 | 0:e0e85d08133d | 35 | lcd.printString("Weather",1,1); |
oe386961252 | 0:e0e85d08133d | 36 | lcd.printString("Station",1,2); |
oe386961252 | 0:e0e85d08133d | 37 | |
oe386961252 | 0:e0e85d08133d | 38 | wait(2.0); //keep the startup picture for 2 seconds |
oe386961252 | 0:e0e85d08133d | 39 | lcd.clear(); //clear the screen |
oe386961252 | 0:e0e85d08133d | 40 | buzzer.beep(2000,1.0); //the buzzer beeps at 2000Hz for 1 second |
oe386961252 | 0:e0e85d08133d | 41 | Measurement measurement; //start to measure temperature and atmosphere pressure |
oe386961252 | 0:e0e85d08133d | 42 | button.rise(&buttonPressed); //check the button rised |
oe386961252 | 0:e0e85d08133d | 43 | |
oe386961252 | 0:e0e85d08133d | 44 | |
oe386961252 | 0:e0e85d08133d | 45 | while(1) |
oe386961252 | 0:e0e85d08133d | 46 | { |
oe386961252 | 0:e0e85d08133d | 47 | measurement = bmp180.readValues(); |
oe386961252 | 0:e0e85d08133d | 48 | char celsius[14];//create a bufffer named celsius |
oe386961252 | 0:e0e85d08133d | 49 | int length = sprintf(celsius,"T = %.2f C",measurement.temperature); //The value measured by the sensor is put into the variable "celsius". And it is displayed on screen. |
oe386961252 | 0:e0e85d08133d | 50 | |
oe386961252 | 0:e0e85d08133d | 51 | char pressure[14];//create a bufffer named pressure |
oe386961252 | 0:e0e85d08133d | 52 | |
oe386961252 | 0:e0e85d08133d | 53 | //The value measured by the sensor is put into the variable "pressure". And it is displayed on screen. |
oe386961252 | 0:e0e85d08133d | 54 | length = sprintf(pressure,"P = %.2f hPa",measurement.pressure); |
oe386961252 | 0:e0e85d08133d | 55 | |
oe386961252 | 0:e0e85d08133d | 56 | float f = (measurement.temperature + 32)*1.8; //the formula to change unit between Celsius and Fahrenheit. |
oe386961252 | 0:e0e85d08133d | 57 | char fahrenheit[14]; |
oe386961252 | 0:e0e85d08133d | 58 | |
oe386961252 | 0:e0e85d08133d | 59 | //display values on screen |
oe386961252 | 0:e0e85d08133d | 60 | length = sprintf(fahrenheit,"T = %.2f F",f); |
oe386961252 | 0:e0e85d08133d | 61 | |
oe386961252 | 0:e0e85d08133d | 62 | //All words will stay on screen for 1 second in case that the numbers changes too fast to read. |
oe386961252 | 0:e0e85d08133d | 63 | wait(1.0); |
oe386961252 | 0:e0e85d08133d | 64 | lcd.clear(); |
oe386961252 | 0:e0e85d08133d | 65 | |
oe386961252 | 0:e0e85d08133d | 66 | |
oe386961252 | 0:e0e85d08133d | 67 | |
oe386961252 | 0:e0e85d08133d | 68 | switch(state) |
oe386961252 | 0:e0e85d08133d | 69 | { |
oe386961252 | 0:e0e85d08133d | 70 | case up: |
oe386961252 | 0:e0e85d08133d | 71 | state = 1; |
oe386961252 | 0:e0e85d08133d | 72 | lcd.printString("Celsius",0,0); //Celsius is shown in the first line on the screen. The default unit of temperature is Celsius. |
oe386961252 | 0:e0e85d08133d | 73 | |
oe386961252 | 0:e0e85d08133d | 74 | //Atmosphere Pressure is shown in the 3rd and 4th line on screen. |
oe386961252 | 0:e0e85d08133d | 75 | lcd.printString("Atmosphere",0,3); |
oe386961252 | 0:e0e85d08133d | 76 | lcd.printString("Pressure",0,4); |
oe386961252 | 0:e0e85d08133d | 77 | |
oe386961252 | 0:e0e85d08133d | 78 | //display the values of temperature and air pressure |
oe386961252 | 0:e0e85d08133d | 79 | lcd.printString(celsius,0,1); |
oe386961252 | 0:e0e85d08133d | 80 | lcd.printString(pressure,0,5); |
oe386961252 | 0:e0e85d08133d | 81 | break; |
oe386961252 | 0:e0e85d08133d | 82 | |
oe386961252 | 0:e0e85d08133d | 83 | case down: |
oe386961252 | 0:e0e85d08133d | 84 | state = 0; |
oe386961252 | 0:e0e85d08133d | 85 | lcd.printString("Fahrenheit",0,0); //Fahrenheit is shown in the first line on the screen. The default unit of temperature is Fahrenheit. |
oe386961252 | 0:e0e85d08133d | 86 | |
oe386961252 | 0:e0e85d08133d | 87 | //Atmosphere Pressure is shown in the 3rd and 4th line on screen. |
oe386961252 | 0:e0e85d08133d | 88 | lcd.printString("Atmosphere",0,3); |
oe386961252 | 0:e0e85d08133d | 89 | lcd.printString("Pressure",0,4); |
oe386961252 | 0:e0e85d08133d | 90 | |
oe386961252 | 0:e0e85d08133d | 91 | //display the values of temperature and air pressure |
oe386961252 | 0:e0e85d08133d | 92 | lcd.printString(fahrenheit,0,1); |
oe386961252 | 0:e0e85d08133d | 93 | lcd.printString(pressure,0,5); |
oe386961252 | 0:e0e85d08133d | 94 | default: |
oe386961252 | 0:e0e85d08133d | 95 | break; |
oe386961252 | 0:e0e85d08133d | 96 | } |
oe386961252 | 0:e0e85d08133d | 97 | |
oe386961252 | 0:e0e85d08133d | 98 | |
oe386961252 | 0:e0e85d08133d | 99 | |
oe386961252 | 0:e0e85d08133d | 100 | //set a range of suitable temperature |
oe386961252 | 0:e0e85d08133d | 101 | //if temperature is greater than 28 degrees,the buzzer will beep at 3000Hz for 0.5 seconds and the LED will work. |
oe386961252 | 0:e0e85d08133d | 102 | if (measurement.temperature >= 28.00) |
oe386961252 | 0:e0e85d08133d | 103 | { |
oe386961252 | 0:e0e85d08133d | 104 | buzzer.beep(3000,0.5); //the buzzer beeps at 3000Hz for 0.5 seconds |
oe386961252 | 0:e0e85d08133d | 105 | led = 1; //the LED works |
oe386961252 | 0:e0e85d08133d | 106 | } |
oe386961252 | 0:e0e85d08133d | 107 | |
oe386961252 | 0:e0e85d08133d | 108 | //if temperature is less than 25 degrees, the buzzer will beep at 2000Hz for 1 second and LED will not work. |
oe386961252 | 0:e0e85d08133d | 109 | else if (measurement.temperature <= 25.00) |
oe386961252 | 0:e0e85d08133d | 110 | { |
oe386961252 | 0:e0e85d08133d | 111 | buzzer.beep(2000,1.0); //the buzzer beeps at 2000Hz for 1 second |
oe386961252 | 0:e0e85d08133d | 112 | led = 0; //LED does not work |
oe386961252 | 0:e0e85d08133d | 113 | } |
oe386961252 | 0:e0e85d08133d | 114 | |
oe386961252 | 0:e0e85d08133d | 115 | //if temperature is between 25 to 28 degrees, neither of the buzzer and the LED works. |
oe386961252 | 0:e0e85d08133d | 116 | else |
oe386961252 | 0:e0e85d08133d | 117 | { |
oe386961252 | 0:e0e85d08133d | 118 | buzzer.nobeep(); //buzzer no beeping |
oe386961252 | 0:e0e85d08133d | 119 | led =0; //LED off |
oe386961252 | 0:e0e85d08133d | 120 | } |
oe386961252 | 0:e0e85d08133d | 121 | } |
oe386961252 | 0:e0e85d08133d | 122 | |
oe386961252 | 0:e0e85d08133d | 123 | } |
oe386961252 | 0:e0e85d08133d | 124 |