Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832 LM75B PWM_Tone_Library mbed Grove_Air_Quality_Sensor_Library
main.cpp@7:9f4b7b55f902, 2017-04-19 (annotated)
- Committer:
- Fleishmachine
- Date:
- Wed Apr 19 09:10:58 2017 +0000
- Revision:
- 7:9f4b7b55f902
- Parent:
- 6:6ae884c9757a
- Child:
- 8:458213f22315
19/04/2017
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ko16aam | 0:e9258aa72a94 | 1 | #include "mbed.h" |
| Fleishmachine | 4:4a1b31edb7b1 | 2 | #include "pwm_tone.h" |
| ko16aam | 0:e9258aa72a94 | 3 | #include "C12832.h" |
| Fleishmachine | 7:9f4b7b55f902 | 4 | |
| Fleishmachine | 7:9f4b7b55f902 | 5 | // Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10 |
| Fleishmachine | 7:9f4b7b55f902 | 6 | Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer |
| ko16aam | 0:e9258aa72a94 | 7 | PwmOut Buzzer(D5); |
| Fleishmachine | 4:4a1b31edb7b1 | 8 | C12832 lcd (p5, p7, p6, p8, p11); |
| Fleishmachine | 7:9f4b7b55f902 | 9 | AnalogIn air(p20); |
| Fleishmachine | 4:4a1b31edb7b1 | 10 | AnalogIn sensor(p17); |
| Fleishmachine | 1:1ed4e4239ea6 | 11 | PwmOut spkr(p26); |
| Fleishmachine | 5:4d8495cf1c10 | 12 | PwmOut r (p23); // red light |
| Fleishmachine | 5:4d8495cf1c10 | 13 | PwmOut g (p24); // green light |
| Fleishmachine | 5:4d8495cf1c10 | 14 | PwmOut b (p25); // blue light |
| Fleishmachine | 5:4d8495cf1c10 | 15 | |
| Fleishmachine | 6:6ae884c9757a | 16 | PwmOut Ctrl1(p21); //relay |
| Fleishmachine | 6:6ae884c9757a | 17 | PwmOut Ctrl2(p22); //relay |
| Fleishmachine | 6:6ae884c9757a | 18 | |
| Fleishmachine | 7:9f4b7b55f902 | 19 | BusOut leds(LED1); // blue LED1 when air quality is low |
| Fleishmachine | 5:4d8495cf1c10 | 20 | |
| Fleishmachine | 5:4d8495cf1c10 | 21 | |
| Fleishmachine | 5:4d8495cf1c10 | 22 | |
| Fleishmachine | 4:4a1b31edb7b1 | 23 | BusIn Up(p15); |
| Fleishmachine | 4:4a1b31edb7b1 | 24 | BusIn Down(p12); |
| Fleishmachine | 4:4a1b31edb7b1 | 25 | BusIn Left(p13); |
| Fleishmachine | 4:4a1b31edb7b1 | 26 | BusIn Right(p16); |
| ko16aam | 0:e9258aa72a94 | 27 | |
| Fleishmachine | 7:9f4b7b55f902 | 28 | |
| Fleishmachine | 7:9f4b7b55f902 | 29 | |
| Fleishmachine | 7:9f4b7b55f902 | 30 | float airValue; |
| ko16aam | 0:e9258aa72a94 | 31 | float multiplier = 50; // this number got me closest to the reading on my multimeter temp probe |
| ko16aam | 0:e9258aa72a94 | 32 | float temp; // calculated temperature |
| Fleishmachine | 1:1ed4e4239ea6 | 33 | int count; // for computing average reading |
| Fleishmachine | 1:1ed4e4239ea6 | 34 | float total; |
| ko16aam | 0:e9258aa72a94 | 35 | float average; |
| Fleishmachine | 4:4a1b31edb7b1 | 36 | float max = 22; |
| Fleishmachine | 4:4a1b31edb7b1 | 37 | float min = 20; |
| Fleishmachine | 7:9f4b7b55f902 | 38 | // int Boiler_max = min + 2; |
| Fleishmachine | 4:4a1b31edb7b1 | 39 | void initialize(); |
| Fleishmachine | 1:1ed4e4239ea6 | 40 | void buzz() |
| Fleishmachine | 7:9f4b7b55f902 | 41 | |
| Fleishmachine | 7:9f4b7b55f902 | 42 | |
| Fleishmachine | 7:9f4b7b55f902 | 43 | { |
| ko16aam | 3:9c4e6163a71b | 44 | for (float i=2000.0; i<7000.0; i+=1000) { |
| ko16aam | 3:9c4e6163a71b | 45 | spkr.period(0.5/i); |
| ko16aam | 3:9c4e6163a71b | 46 | spkr=0.1; |
| Fleishmachine | 1:1ed4e4239ea6 | 47 | wait(0.1); |
| Fleishmachine | 1:1ed4e4239ea6 | 48 | } |
| Fleishmachine | 1:1ed4e4239ea6 | 49 | spkr=0.0; |
| Fleishmachine | 1:1ed4e4239ea6 | 50 | } |
| Fleishmachine | 1:1ed4e4239ea6 | 51 | |
| Fleishmachine | 7:9f4b7b55f902 | 52 | |
| Fleishmachine | 4:4a1b31edb7b1 | 53 | int main() |
| Fleishmachine | 4:4a1b31edb7b1 | 54 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 55 | initialize(); |
| Fleishmachine | 1:1ed4e4239ea6 | 56 | count = 0; |
| Fleishmachine | 1:1ed4e4239ea6 | 57 | total = 0.0; |
| Fleishmachine | 5:4d8495cf1c10 | 58 | r = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 59 | b = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 60 | g = 1; |
| Fleishmachine | 4:4a1b31edb7b1 | 61 | buzz(); |
| Fleishmachine | 4:4a1b31edb7b1 | 62 | |
| Fleishmachine | 7:9f4b7b55f902 | 63 | |
| Fleishmachine | 7:9f4b7b55f902 | 64 | |
| Fleishmachine | 7:9f4b7b55f902 | 65 | |
| Fleishmachine | 7:9f4b7b55f902 | 66 | |
| Fleishmachine | 4:4a1b31edb7b1 | 67 | |
| Fleishmachine | 4:4a1b31edb7b1 | 68 | |
| Fleishmachine | 1:1ed4e4239ea6 | 69 | |
| ko16aam | 0:e9258aa72a94 | 70 | while (1) { |
| Fleishmachine | 7:9f4b7b55f902 | 71 | |
| ko16aam | 0:e9258aa72a94 | 72 | // formula is analog reading * multiplier |
| Fleishmachine | 7:9f4b7b55f902 | 73 | airValue = air.read()* 100; // returns voltage between 0 -> 1 |
| ko16aam | 0:e9258aa72a94 | 74 | temp = sensor.read() * multiplier; |
| ko16aam | 0:e9258aa72a94 | 75 | count++; |
| ko16aam | 0:e9258aa72a94 | 76 | total += temp; |
| ko16aam | 0:e9258aa72a94 | 77 | average = total / count; |
| Fleishmachine | 7:9f4b7b55f902 | 78 | |
| Fleishmachine | 7:9f4b7b55f902 | 79 | |
| Fleishmachine | 7:9f4b7b55f902 | 80 | |
| Fleishmachine | 7:9f4b7b55f902 | 81 | |
| Fleishmachine | 7:9f4b7b55f902 | 82 | |
| Fleishmachine | 4:4a1b31edb7b1 | 83 | // min = min * 1; |
| ko16aam | 0:e9258aa72a94 | 84 | |
| Fleishmachine | 4:4a1b31edb7b1 | 85 | if (Up) |
| Fleishmachine | 4:4a1b31edb7b1 | 86 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 87 | |
| Fleishmachine | 4:4a1b31edb7b1 | 88 | min++; |
| Fleishmachine | 4:4a1b31edb7b1 | 89 | } |
| Fleishmachine | 4:4a1b31edb7b1 | 90 | |
| Fleishmachine | 4:4a1b31edb7b1 | 91 | if (Down) |
| Fleishmachine | 4:4a1b31edb7b1 | 92 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 93 | |
| Fleishmachine | 4:4a1b31edb7b1 | 94 | min--; |
| Fleishmachine | 4:4a1b31edb7b1 | 95 | } |
| Fleishmachine | 4:4a1b31edb7b1 | 96 | if (Left) |
| Fleishmachine | 4:4a1b31edb7b1 | 97 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 98 | |
| Fleishmachine | 4:4a1b31edb7b1 | 99 | max--; |
| Fleishmachine | 4:4a1b31edb7b1 | 100 | } |
| Fleishmachine | 4:4a1b31edb7b1 | 101 | |
| Fleishmachine | 4:4a1b31edb7b1 | 102 | if (Right) |
| Fleishmachine | 4:4a1b31edb7b1 | 103 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 104 | |
| Fleishmachine | 4:4a1b31edb7b1 | 105 | max++; |
| Fleishmachine | 4:4a1b31edb7b1 | 106 | } |
| Fleishmachine | 4:4a1b31edb7b1 | 107 | |
| Fleishmachine | 4:4a1b31edb7b1 | 108 | |
| Fleishmachine | 4:4a1b31edb7b1 | 109 | if(temp > max) |
| ko16aam | 3:9c4e6163a71b | 110 | { |
| Fleishmachine | 5:4d8495cf1c10 | 111 | r = 0; // red |
| Fleishmachine | 5:4d8495cf1c10 | 112 | g = 1; // green |
| Fleishmachine | 5:4d8495cf1c10 | 113 | b = 1; // blue |
| Fleishmachine | 6:6ae884c9757a | 114 | Ctrl1 = 1; |
| Fleishmachine | 6:6ae884c9757a | 115 | Ctrl2 = 0; |
| Fleishmachine | 5:4d8495cf1c10 | 116 | |
| Fleishmachine | 5:4d8495cf1c10 | 117 | // value 0 - max |
| Fleishmachine | 5:4d8495cf1c10 | 118 | // value 1 - zero |
| Fleishmachine | 5:4d8495cf1c10 | 119 | // so values closer to zero 0 brighter than closer to 1 |
| Fleishmachine | 6:6ae884c9757a | 120 | // buzz(); |
| Fleishmachine | 5:4d8495cf1c10 | 121 | |
| ko16aam | 3:9c4e6163a71b | 122 | } |
| ko16aam | 3:9c4e6163a71b | 123 | else |
| ko16aam | 3:9c4e6163a71b | 124 | { |
| Fleishmachine | 5:4d8495cf1c10 | 125 | r = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 126 | g = 0.25; |
| Fleishmachine | 5:4d8495cf1c10 | 127 | b = 1; |
| Fleishmachine | 6:6ae884c9757a | 128 | Ctrl1 = 1; |
| Fleishmachine | 6:6ae884c9757a | 129 | Ctrl2 = 1; |
| ko16aam | 3:9c4e6163a71b | 130 | } |
| ko16aam | 3:9c4e6163a71b | 131 | |
| Fleishmachine | 4:4a1b31edb7b1 | 132 | if(temp < min) |
| ko16aam | 3:9c4e6163a71b | 133 | { |
| Fleishmachine | 5:4d8495cf1c10 | 134 | r = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 135 | g = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 136 | b = 0.25; |
| Fleishmachine | 6:6ae884c9757a | 137 | Ctrl1 = 0; |
| Fleishmachine | 6:6ae884c9757a | 138 | Ctrl2 = 1; |
| Fleishmachine | 5:4d8495cf1c10 | 139 | |
| ko16aam | 3:9c4e6163a71b | 140 | } |
| Fleishmachine | 7:9f4b7b55f902 | 141 | |
| Fleishmachine | 7:9f4b7b55f902 | 142 | if (airValue > 80 ) |
| Fleishmachine | 7:9f4b7b55f902 | 143 | { |
| Fleishmachine | 7:9f4b7b55f902 | 144 | leds =1; |
| Fleishmachine | 7:9f4b7b55f902 | 145 | } |
| Fleishmachine | 7:9f4b7b55f902 | 146 | |
| Fleishmachine | 7:9f4b7b55f902 | 147 | if (airValue < 80 ) |
| Fleishmachine | 7:9f4b7b55f902 | 148 | { |
| Fleishmachine | 7:9f4b7b55f902 | 149 | leds =0; |
| Fleishmachine | 7:9f4b7b55f902 | 150 | } |
| Fleishmachine | 7:9f4b7b55f902 | 151 | |
| ko16aam | 3:9c4e6163a71b | 152 | else |
| ko16aam | 3:9c4e6163a71b | 153 | { |
| Fleishmachine | 5:4d8495cf1c10 | 154 | |
| ko16aam | 3:9c4e6163a71b | 155 | } |
| ko16aam | 3:9c4e6163a71b | 156 | |
| Fleishmachine | 7:9f4b7b55f902 | 157 | |
| Fleishmachine | 7:9f4b7b55f902 | 158 | |
| Fleishmachine | 7:9f4b7b55f902 | 159 | |
| Fleishmachine | 7:9f4b7b55f902 | 160 | |
| ko16aam | 0:e9258aa72a94 | 161 | lcd.cls(); |
| Fleishmachine | 4:4a1b31edb7b1 | 162 | lcd.locate(0,0); |
| Fleishmachine | 4:4a1b31edb7b1 | 163 | lcd.printf("Temp= %6.2f Avg= %5.1f ", temp , average ); |
| Fleishmachine | 7:9f4b7b55f902 | 164 | lcd.locate(0,10); |
| Fleishmachine | 4:4a1b31edb7b1 | 165 | lcd.printf("min= %6.0f max= %6.0f", min, max); |
| Fleishmachine | 4:4a1b31edb7b1 | 166 | |
| Fleishmachine | 7:9f4b7b55f902 | 167 | lcd.locate(0,20); |
| Fleishmachine | 7:9f4b7b55f902 | 168 | lcd.printf("Air Quality %f \n\r",airValue); |
| Fleishmachine | 7:9f4b7b55f902 | 169 | |
| Fleishmachine | 4:4a1b31edb7b1 | 170 | wait(0.5); |
| Fleishmachine | 7:9f4b7b55f902 | 171 | |
| Fleishmachine | 7:9f4b7b55f902 | 172 | pc.printf(" \n\r"); |
| Fleishmachine | 7:9f4b7b55f902 | 173 | pc.printf("Thermostat values\n\r"); |
| Fleishmachine | 7:9f4b7b55f902 | 174 | pc.printf("Temp= %6.2f Avg= %5.1f \n\r", temp , average ); |
| Fleishmachine | 7:9f4b7b55f902 | 175 | pc.printf("min= %6.0f max= %6.0f \n \r", min, max); |
| Fleishmachine | 7:9f4b7b55f902 | 176 | pc.printf("Air Quality %f \n \r",airValue); |
| Fleishmachine | 7:9f4b7b55f902 | 177 | pc.printf(" \n\r"); |
| Fleishmachine | 7:9f4b7b55f902 | 178 | |
| Fleishmachine | 7:9f4b7b55f902 | 179 | |
| Fleishmachine | 7:9f4b7b55f902 | 180 | |
| Fleishmachine | 7:9f4b7b55f902 | 181 | |
| Fleishmachine | 4:4a1b31edb7b1 | 182 | } |
| Fleishmachine | 4:4a1b31edb7b1 | 183 | |
| ko16aam | 0:e9258aa72a94 | 184 | } |
| ko16aam | 0:e9258aa72a94 | 185 | |
| Fleishmachine | 4:4a1b31edb7b1 | 186 | void initialize() |
| Fleishmachine | 4:4a1b31edb7b1 | 187 | { |
| Fleishmachine | 4:4a1b31edb7b1 | 188 | lcd.cls(); |
| Fleishmachine | 4:4a1b31edb7b1 | 189 | lcd.locate(0,0); |
| Fleishmachine | 4:4a1b31edb7b1 | 190 | } |