Final Version
Dependencies: C12832 LM75B PWM_Tone_Library mbed Grove_Air_Quality_Sensor_Library
main.cpp@8:458213f22315, 2018-01-02 (annotated)
- Committer:
- ko16aam
- Date:
- Tue Jan 02 16:12:44 2018 +0000
- Revision:
- 8:458213f22315
- Parent:
- 7:9f4b7b55f902
Placement;
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 | |
ko16aam | 8:458213f22315 | 5 | // Serial xbee1(p9, p10); //Creates a variable for serial communication through pin 9 and 10 |
Fleishmachine | 7:9f4b7b55f902 | 6 | Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer |
Fleishmachine | 4:4a1b31edb7b1 | 7 | C12832 lcd (p5, p7, p6, p8, p11); |
Fleishmachine | 7:9f4b7b55f902 | 8 | AnalogIn air(p20); |
Fleishmachine | 4:4a1b31edb7b1 | 9 | AnalogIn sensor(p17); |
Fleishmachine | 1:1ed4e4239ea6 | 10 | PwmOut spkr(p26); |
Fleishmachine | 5:4d8495cf1c10 | 11 | PwmOut r (p23); // red light |
Fleishmachine | 5:4d8495cf1c10 | 12 | PwmOut g (p24); // green light |
Fleishmachine | 5:4d8495cf1c10 | 13 | PwmOut b (p25); // blue light |
Fleishmachine | 6:6ae884c9757a | 14 | PwmOut Ctrl1(p21); //relay |
Fleishmachine | 6:6ae884c9757a | 15 | PwmOut Ctrl2(p22); //relay |
Fleishmachine | 7:9f4b7b55f902 | 16 | BusOut leds(LED1); // blue LED1 when air quality is low |
Fleishmachine | 4:4a1b31edb7b1 | 17 | BusIn Up(p15); |
Fleishmachine | 4:4a1b31edb7b1 | 18 | BusIn Down(p12); |
Fleishmachine | 4:4a1b31edb7b1 | 19 | BusIn Left(p13); |
Fleishmachine | 4:4a1b31edb7b1 | 20 | BusIn Right(p16); |
ko16aam | 0:e9258aa72a94 | 21 | |
Fleishmachine | 7:9f4b7b55f902 | 22 | |
Fleishmachine | 7:9f4b7b55f902 | 23 | float airValue; |
ko16aam | 8:458213f22315 | 24 | float multiplier = 50; // this number got us closest to the reading on the multimeter temp probe |
ko16aam | 0:e9258aa72a94 | 25 | float temp; // calculated temperature |
Fleishmachine | 1:1ed4e4239ea6 | 26 | int count; // for computing average reading |
Fleishmachine | 1:1ed4e4239ea6 | 27 | float total; |
ko16aam | 0:e9258aa72a94 | 28 | float average; |
Fleishmachine | 4:4a1b31edb7b1 | 29 | float max = 22; |
Fleishmachine | 4:4a1b31edb7b1 | 30 | float min = 20; |
Fleishmachine | 4:4a1b31edb7b1 | 31 | void initialize(); |
Fleishmachine | 1:1ed4e4239ea6 | 32 | |
Fleishmachine | 7:9f4b7b55f902 | 33 | |
Fleishmachine | 4:4a1b31edb7b1 | 34 | int main() |
Fleishmachine | 4:4a1b31edb7b1 | 35 | { |
Fleishmachine | 4:4a1b31edb7b1 | 36 | initialize(); |
Fleishmachine | 1:1ed4e4239ea6 | 37 | count = 0; |
Fleishmachine | 1:1ed4e4239ea6 | 38 | total = 0.0; |
Fleishmachine | 5:4d8495cf1c10 | 39 | r = 1; |
Fleishmachine | 5:4d8495cf1c10 | 40 | b = 1; |
Fleishmachine | 5:4d8495cf1c10 | 41 | g = 1; |
ko16aam | 8:458213f22315 | 42 | |
Fleishmachine | 1:1ed4e4239ea6 | 43 | |
ko16aam | 0:e9258aa72a94 | 44 | while (1) { |
Fleishmachine | 7:9f4b7b55f902 | 45 | |
ko16aam | 0:e9258aa72a94 | 46 | // formula is analog reading * multiplier |
Fleishmachine | 7:9f4b7b55f902 | 47 | airValue = air.read()* 100; // returns voltage between 0 -> 1 |
ko16aam | 0:e9258aa72a94 | 48 | temp = sensor.read() * multiplier; |
ko16aam | 0:e9258aa72a94 | 49 | count++; |
ko16aam | 0:e9258aa72a94 | 50 | total += temp; |
ko16aam | 0:e9258aa72a94 | 51 | average = total / count; |
Fleishmachine | 7:9f4b7b55f902 | 52 | |
Fleishmachine | 7:9f4b7b55f902 | 53 | |
Fleishmachine | 4:4a1b31edb7b1 | 54 | if (Up) |
Fleishmachine | 4:4a1b31edb7b1 | 55 | { |
Fleishmachine | 4:4a1b31edb7b1 | 56 | |
Fleishmachine | 4:4a1b31edb7b1 | 57 | min++; |
Fleishmachine | 4:4a1b31edb7b1 | 58 | } |
Fleishmachine | 4:4a1b31edb7b1 | 59 | |
Fleishmachine | 4:4a1b31edb7b1 | 60 | if (Down) |
Fleishmachine | 4:4a1b31edb7b1 | 61 | { |
Fleishmachine | 4:4a1b31edb7b1 | 62 | |
Fleishmachine | 4:4a1b31edb7b1 | 63 | min--; |
Fleishmachine | 4:4a1b31edb7b1 | 64 | } |
Fleishmachine | 4:4a1b31edb7b1 | 65 | if (Left) |
Fleishmachine | 4:4a1b31edb7b1 | 66 | { |
Fleishmachine | 4:4a1b31edb7b1 | 67 | |
Fleishmachine | 4:4a1b31edb7b1 | 68 | max--; |
Fleishmachine | 4:4a1b31edb7b1 | 69 | } |
Fleishmachine | 4:4a1b31edb7b1 | 70 | |
Fleishmachine | 4:4a1b31edb7b1 | 71 | if (Right) |
Fleishmachine | 4:4a1b31edb7b1 | 72 | { |
Fleishmachine | 4:4a1b31edb7b1 | 73 | |
Fleishmachine | 4:4a1b31edb7b1 | 74 | max++; |
Fleishmachine | 4:4a1b31edb7b1 | 75 | } |
Fleishmachine | 4:4a1b31edb7b1 | 76 | |
Fleishmachine | 4:4a1b31edb7b1 | 77 | |
Fleishmachine | 4:4a1b31edb7b1 | 78 | if(temp > max) |
ko16aam | 3:9c4e6163a71b | 79 | { |
Fleishmachine | 5:4d8495cf1c10 | 80 | r = 0; // red |
Fleishmachine | 5:4d8495cf1c10 | 81 | g = 1; // green |
Fleishmachine | 5:4d8495cf1c10 | 82 | b = 1; // blue |
Fleishmachine | 6:6ae884c9757a | 83 | Ctrl1 = 1; |
Fleishmachine | 6:6ae884c9757a | 84 | Ctrl2 = 0; |
Fleishmachine | 5:4d8495cf1c10 | 85 | |
Fleishmachine | 5:4d8495cf1c10 | 86 | // value 0 - max |
Fleishmachine | 5:4d8495cf1c10 | 87 | // value 1 - zero |
Fleishmachine | 5:4d8495cf1c10 | 88 | // so values closer to zero 0 brighter than closer to 1 |
Fleishmachine | 5:4d8495cf1c10 | 89 | |
ko16aam | 3:9c4e6163a71b | 90 | } |
ko16aam | 3:9c4e6163a71b | 91 | else |
ko16aam | 3:9c4e6163a71b | 92 | { |
Fleishmachine | 5:4d8495cf1c10 | 93 | r = 1; |
Fleishmachine | 5:4d8495cf1c10 | 94 | g = 0.25; |
Fleishmachine | 5:4d8495cf1c10 | 95 | b = 1; |
Fleishmachine | 6:6ae884c9757a | 96 | Ctrl1 = 1; |
Fleishmachine | 6:6ae884c9757a | 97 | Ctrl2 = 1; |
ko16aam | 3:9c4e6163a71b | 98 | } |
ko16aam | 3:9c4e6163a71b | 99 | |
Fleishmachine | 4:4a1b31edb7b1 | 100 | if(temp < min) |
ko16aam | 3:9c4e6163a71b | 101 | { |
Fleishmachine | 5:4d8495cf1c10 | 102 | r = 1; |
Fleishmachine | 5:4d8495cf1c10 | 103 | g = 1; |
Fleishmachine | 5:4d8495cf1c10 | 104 | b = 0.25; |
Fleishmachine | 6:6ae884c9757a | 105 | Ctrl1 = 0; |
Fleishmachine | 6:6ae884c9757a | 106 | Ctrl2 = 1; |
Fleishmachine | 5:4d8495cf1c10 | 107 | |
ko16aam | 3:9c4e6163a71b | 108 | } |
Fleishmachine | 7:9f4b7b55f902 | 109 | |
Fleishmachine | 7:9f4b7b55f902 | 110 | if (airValue > 80 ) |
Fleishmachine | 7:9f4b7b55f902 | 111 | { |
Fleishmachine | 7:9f4b7b55f902 | 112 | leds =1; |
Fleishmachine | 7:9f4b7b55f902 | 113 | } |
Fleishmachine | 7:9f4b7b55f902 | 114 | |
Fleishmachine | 7:9f4b7b55f902 | 115 | if (airValue < 80 ) |
Fleishmachine | 7:9f4b7b55f902 | 116 | { |
Fleishmachine | 7:9f4b7b55f902 | 117 | leds =0; |
Fleishmachine | 7:9f4b7b55f902 | 118 | } |
Fleishmachine | 7:9f4b7b55f902 | 119 | |
ko16aam | 3:9c4e6163a71b | 120 | else |
ko16aam | 3:9c4e6163a71b | 121 | { |
Fleishmachine | 5:4d8495cf1c10 | 122 | |
ko16aam | 3:9c4e6163a71b | 123 | } |
ko16aam | 3:9c4e6163a71b | 124 | |
Fleishmachine | 7:9f4b7b55f902 | 125 | |
ko16aam | 0:e9258aa72a94 | 126 | lcd.cls(); |
Fleishmachine | 4:4a1b31edb7b1 | 127 | lcd.locate(0,0); |
Fleishmachine | 4:4a1b31edb7b1 | 128 | lcd.printf("Temp= %6.2f Avg= %5.1f ", temp , average ); |
Fleishmachine | 7:9f4b7b55f902 | 129 | lcd.locate(0,10); |
Fleishmachine | 4:4a1b31edb7b1 | 130 | lcd.printf("min= %6.0f max= %6.0f", min, max); |
Fleishmachine | 4:4a1b31edb7b1 | 131 | |
Fleishmachine | 7:9f4b7b55f902 | 132 | lcd.locate(0,20); |
Fleishmachine | 7:9f4b7b55f902 | 133 | lcd.printf("Air Quality %f \n\r",airValue); |
Fleishmachine | 7:9f4b7b55f902 | 134 | |
Fleishmachine | 4:4a1b31edb7b1 | 135 | wait(0.5); |
Fleishmachine | 7:9f4b7b55f902 | 136 | |
Fleishmachine | 7:9f4b7b55f902 | 137 | pc.printf(" \n\r"); |
Fleishmachine | 7:9f4b7b55f902 | 138 | pc.printf("Thermostat values\n\r"); |
Fleishmachine | 7:9f4b7b55f902 | 139 | pc.printf("Temp= %6.2f Avg= %5.1f \n\r", temp , average ); |
Fleishmachine | 7:9f4b7b55f902 | 140 | pc.printf("min= %6.0f max= %6.0f \n \r", min, max); |
Fleishmachine | 7:9f4b7b55f902 | 141 | pc.printf("Air Quality %f \n \r",airValue); |
Fleishmachine | 7:9f4b7b55f902 | 142 | pc.printf(" \n\r"); |
Fleishmachine | 7:9f4b7b55f902 | 143 | |
Fleishmachine | 4:4a1b31edb7b1 | 144 | } |
Fleishmachine | 4:4a1b31edb7b1 | 145 | |
ko16aam | 0:e9258aa72a94 | 146 | } |
ko16aam | 0:e9258aa72a94 | 147 | |
Fleishmachine | 4:4a1b31edb7b1 | 148 | void initialize() |
Fleishmachine | 4:4a1b31edb7b1 | 149 | { |
Fleishmachine | 4:4a1b31edb7b1 | 150 | lcd.cls(); |
Fleishmachine | 4:4a1b31edb7b1 | 151 | lcd.locate(0,0); |
Fleishmachine | 4:4a1b31edb7b1 | 152 | } |