a
Dependencies: mbed Sht31 MAX44009
main.cpp@0:b1b7113a1441, 2020-04-13 (annotated)
- Committer:
- chapman515
- Date:
- Mon Apr 13 08:52:36 2020 +0000
- Revision:
- 0:b1b7113a1441
- Child:
- 1:3889765eb394
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chapman515 | 0:b1b7113a1441 | 1 | #include "mbed.h" |
chapman515 | 0:b1b7113a1441 | 2 | #include "Sht31.h" |
chapman515 | 0:b1b7113a1441 | 3 | #include "MAX44009.h" |
chapman515 | 0:b1b7113a1441 | 4 | #include "mbed_wait_api.h" |
chapman515 | 0:b1b7113a1441 | 5 | #include <list> |
chapman515 | 0:b1b7113a1441 | 6 | |
chapman515 | 0:b1b7113a1441 | 7 | Serial pc(p13,p14); |
chapman515 | 0:b1b7113a1441 | 8 | Sht31 sht31(p9, p10); |
chapman515 | 0:b1b7113a1441 | 9 | MAX44009 myMAX44009 ( p9, p10, MAX44009::MAX44009_ADDRESS_A0_GND, 400000 ); |
chapman515 | 0:b1b7113a1441 | 10 | I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL) |
chapman515 | 0:b1b7113a1441 | 11 | |
chapman515 | 0:b1b7113a1441 | 12 | |
chapman515 | 0:b1b7113a1441 | 13 | |
chapman515 | 0:b1b7113a1441 | 14 | DigitalOut BLE_Can_receive(p11); // 0 = can recieve |
chapman515 | 0:b1b7113a1441 | 15 | |
chapman515 | 0:b1b7113a1441 | 16 | class RGB{ |
chapman515 | 0:b1b7113a1441 | 17 | public : int C; |
chapman515 | 0:b1b7113a1441 | 18 | int R; |
chapman515 | 0:b1b7113a1441 | 19 | int G; |
chapman515 | 0:b1b7113a1441 | 20 | int B; |
chapman515 | 0:b1b7113a1441 | 21 | }; |
chapman515 | 0:b1b7113a1441 | 22 | class RGBf{ |
chapman515 | 0:b1b7113a1441 | 23 | public : float C; |
chapman515 | 0:b1b7113a1441 | 24 | float R; |
chapman515 | 0:b1b7113a1441 | 25 | float G; |
chapman515 | 0:b1b7113a1441 | 26 | float B; |
chapman515 | 0:b1b7113a1441 | 27 | }; |
chapman515 | 0:b1b7113a1441 | 28 | class TandH{ |
chapman515 | 0:b1b7113a1441 | 29 | public : float t; |
chapman515 | 0:b1b7113a1441 | 30 | float h; |
chapman515 | 0:b1b7113a1441 | 31 | }; |
chapman515 | 0:b1b7113a1441 | 32 | |
chapman515 | 0:b1b7113a1441 | 33 | RGB RGBdata[10]; |
chapman515 | 0:b1b7113a1441 | 34 | TandH THdata[10]; |
chapman515 | 0:b1b7113a1441 | 35 | float luxdata[10]; |
chapman515 | 0:b1b7113a1441 | 36 | |
chapman515 | 0:b1b7113a1441 | 37 | int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead; |
chapman515 | 0:b1b7113a1441 | 38 | |
chapman515 | 0:b1b7113a1441 | 39 | bool allfilled = false; |
chapman515 | 0:b1b7113a1441 | 40 | RGBf RGBavg; |
chapman515 | 0:b1b7113a1441 | 41 | TandH THavg; |
chapman515 | 0:b1b7113a1441 | 42 | float luxavg; |
chapman515 | 0:b1b7113a1441 | 43 | |
chapman515 | 0:b1b7113a1441 | 44 | |
chapman515 | 0:b1b7113a1441 | 45 | int main() |
chapman515 | 0:b1b7113a1441 | 46 | { |
chapman515 | 0:b1b7113a1441 | 47 | pc.baud ( 19200 ); |
chapman515 | 0:b1b7113a1441 | 48 | |
chapman515 | 0:b1b7113a1441 | 49 | |
chapman515 | 0:b1b7113a1441 | 50 | while(1) { |
chapman515 | 0:b1b7113a1441 | 51 | counter = counter%10; |
chapman515 | 0:b1b7113a1441 | 52 | //BLE_Can_receive = 0; |
chapman515 | 0:b1b7113a1441 | 53 | //wait_ms(5); |
chapman515 | 0:b1b7113a1441 | 54 | //pc.printf("counter31 = %d\n", counter); |
chapman515 | 0:b1b7113a1441 | 55 | //BLE_Can_receive = 1; |
chapman515 | 0:b1b7113a1441 | 56 | { |
chapman515 | 0:b1b7113a1441 | 57 | int sensor_addr = 41 << 1; |
chapman515 | 0:b1b7113a1441 | 58 | char id_regval[1] = {146}; |
chapman515 | 0:b1b7113a1441 | 59 | char data[1] = {0}; |
chapman515 | 0:b1b7113a1441 | 60 | i2c.write(sensor_addr,id_regval,1, true); |
chapman515 | 0:b1b7113a1441 | 61 | i2c.read(sensor_addr,data,1,false); |
chapman515 | 0:b1b7113a1441 | 62 | |
chapman515 | 0:b1b7113a1441 | 63 | |
chapman515 | 0:b1b7113a1441 | 64 | // Initialize color sensor |
chapman515 | 0:b1b7113a1441 | 65 | |
chapman515 | 0:b1b7113a1441 | 66 | char timing_register[2] = {129,192}; |
chapman515 | 0:b1b7113a1441 | 67 | //char timing_register[2] = {129,0}; |
chapman515 | 0:b1b7113a1441 | 68 | i2c.write(sensor_addr,timing_register,2,false); |
chapman515 | 0:b1b7113a1441 | 69 | |
chapman515 | 0:b1b7113a1441 | 70 | char control_register[2] = {143,0}; |
chapman515 | 0:b1b7113a1441 | 71 | char temp[2]={0,0}; |
chapman515 | 0:b1b7113a1441 | 72 | //char control_register[2] = {143,3}; |
chapman515 | 0:b1b7113a1441 | 73 | i2c.write(sensor_addr,control_register,2,false); |
chapman515 | 0:b1b7113a1441 | 74 | |
chapman515 | 0:b1b7113a1441 | 75 | char enable_register[2] = {128,3}; |
chapman515 | 0:b1b7113a1441 | 76 | i2c.write(sensor_addr,enable_register,2,false); |
chapman515 | 0:b1b7113a1441 | 77 | |
chapman515 | 0:b1b7113a1441 | 78 | // Read data from color sensor (Clear/Red/Green/Blue) |
chapman515 | 0:b1b7113a1441 | 79 | |
chapman515 | 0:b1b7113a1441 | 80 | char clear_reg[1] = {148}; |
chapman515 | 0:b1b7113a1441 | 81 | char clear_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 82 | i2c.write(sensor_addr,clear_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 83 | i2c.read(sensor_addr,clear_data,2, false); |
chapman515 | 0:b1b7113a1441 | 84 | |
chapman515 | 0:b1b7113a1441 | 85 | int clear_value = ((int)clear_data[1] << 8) | clear_data[0]; |
chapman515 | 0:b1b7113a1441 | 86 | |
chapman515 | 0:b1b7113a1441 | 87 | char red_reg[1] = {150}; |
chapman515 | 0:b1b7113a1441 | 88 | char red_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 89 | i2c.write(sensor_addr,red_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 90 | i2c.read(sensor_addr,red_data,2, false); |
chapman515 | 0:b1b7113a1441 | 91 | |
chapman515 | 0:b1b7113a1441 | 92 | int red_value = ((int)red_data[1] << 8) | red_data[0]; |
chapman515 | 0:b1b7113a1441 | 93 | |
chapman515 | 0:b1b7113a1441 | 94 | char green_reg[1] = {152}; |
chapman515 | 0:b1b7113a1441 | 95 | char green_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 96 | i2c.write(sensor_addr,green_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 97 | i2c.read(sensor_addr,green_data,2, false); |
chapman515 | 0:b1b7113a1441 | 98 | |
chapman515 | 0:b1b7113a1441 | 99 | int green_value = ((int)green_data[1] << 8) | green_data[0]; |
chapman515 | 0:b1b7113a1441 | 100 | |
chapman515 | 0:b1b7113a1441 | 101 | char blue_reg[1] = {154}; |
chapman515 | 0:b1b7113a1441 | 102 | char blue_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 103 | i2c.write(sensor_addr,blue_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 104 | i2c.read(sensor_addr,blue_data,2, false); |
chapman515 | 0:b1b7113a1441 | 105 | |
chapman515 | 0:b1b7113a1441 | 106 | int blue_value = ((int)blue_data[1] << 8) | blue_data[0]; |
chapman515 | 0:b1b7113a1441 | 107 | |
chapman515 | 0:b1b7113a1441 | 108 | // print sensor readings |
chapman515 | 0:b1b7113a1441 | 109 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 110 | RGBavg.C = RGBavg.C*10-RGBdata[counter].C; |
chapman515 | 0:b1b7113a1441 | 111 | RGBavg.R = RGBavg.R*10-RGBdata[counter].R; |
chapman515 | 0:b1b7113a1441 | 112 | RGBavg.G = RGBavg.G*10-RGBdata[counter].G; |
chapman515 | 0:b1b7113a1441 | 113 | RGBavg.B = RGBavg.B*10-RGBdata[counter].B; |
chapman515 | 0:b1b7113a1441 | 114 | } |
chapman515 | 0:b1b7113a1441 | 115 | RGBdata[counter].C= clear_value; |
chapman515 | 0:b1b7113a1441 | 116 | RGBdata[counter].R= red_value; |
chapman515 | 0:b1b7113a1441 | 117 | RGBdata[counter].G= green_value; |
chapman515 | 0:b1b7113a1441 | 118 | RGBdata[counter].B= blue_value; |
chapman515 | 0:b1b7113a1441 | 119 | //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value); |
chapman515 | 0:b1b7113a1441 | 120 | } |
chapman515 | 0:b1b7113a1441 | 121 | { |
chapman515 | 0:b1b7113a1441 | 122 | |
chapman515 | 0:b1b7113a1441 | 123 | MAX44009::MAX44009_status_t aux; |
chapman515 | 0:b1b7113a1441 | 124 | MAX44009::MAX44009_vector_data_t myMAX44009_Data; |
chapman515 | 0:b1b7113a1441 | 125 | |
chapman515 | 0:b1b7113a1441 | 126 | aux = myMAX44009.MAX44009_Configuration ( MAX44009::CONFIGURATION_CONT_DEFAULT_MODE, MAX44009::CONFIGURATION_MANUAL_DEFAULT_MODE, MAX44009::CONFIGURATION_CDR_CURRENT_NOT_DIVIDED, MAX44009::CONFIGURATION_TIM_800_MS ); |
chapman515 | 0:b1b7113a1441 | 127 | aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 128 | aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 129 | aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 130 | |
chapman515 | 0:b1b7113a1441 | 131 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 132 | luxavg = luxavg*10-luxdata[counter]; |
chapman515 | 0:b1b7113a1441 | 133 | } |
chapman515 | 0:b1b7113a1441 | 134 | |
chapman515 | 0:b1b7113a1441 | 135 | luxdata[counter] = myMAX44009_Data.lux; |
chapman515 | 0:b1b7113a1441 | 136 | |
chapman515 | 0:b1b7113a1441 | 137 | |
chapman515 | 0:b1b7113a1441 | 138 | } |
chapman515 | 0:b1b7113a1441 | 139 | |
chapman515 | 0:b1b7113a1441 | 140 | { |
chapman515 | 0:b1b7113a1441 | 141 | |
chapman515 | 0:b1b7113a1441 | 142 | //float t = sht31.readTemperature(); |
chapman515 | 0:b1b7113a1441 | 143 | //float h = sht31.readHumidity(); |
chapman515 | 0:b1b7113a1441 | 144 | |
chapman515 | 0:b1b7113a1441 | 145 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 146 | THavg.t = THavg.t*10-THdata[counter].t; |
chapman515 | 0:b1b7113a1441 | 147 | THavg.h = THavg.h*10-THdata[counter].h; |
chapman515 | 0:b1b7113a1441 | 148 | } |
chapman515 | 0:b1b7113a1441 | 149 | THdata[counter].t = sht31.readTemperature(); |
chapman515 | 0:b1b7113a1441 | 150 | THdata[counter].h = sht31.readHumidity(); |
chapman515 | 0:b1b7113a1441 | 151 | |
chapman515 | 0:b1b7113a1441 | 152 | //pc.printf("[TEMP/HUM]"); |
chapman515 | 0:b1b7113a1441 | 153 | |
chapman515 | 0:b1b7113a1441 | 154 | } |
chapman515 | 0:b1b7113a1441 | 155 | //////////////////////////////////////////////////////////////////// |
chapman515 | 0:b1b7113a1441 | 156 | |
chapman515 | 0:b1b7113a1441 | 157 | /*BLE_Can_receive = 0; |
chapman515 | 0:b1b7113a1441 | 158 | wait_ms(5); |
chapman515 | 0:b1b7113a1441 | 159 | pc.printf("counter119\n = %d", counter); |
chapman515 | 0:b1b7113a1441 | 160 | for (int i =0;i<10;i++){ |
chapman515 | 0:b1b7113a1441 | 161 | pc.printf("i=%d ,", i); |
chapman515 | 0:b1b7113a1441 | 162 | pc.printf("%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n", RGBdata[i].C, RGBdata[i].R, RGBdata[i].G, RGBdata[i].B,THdata[i].t, THdata[i].h,luxdata[i]); |
chapman515 | 0:b1b7113a1441 | 163 | } |
chapman515 | 0:b1b7113a1441 | 164 | BLE_Can_receive = 1;*/ |
chapman515 | 0:b1b7113a1441 | 165 | |
chapman515 | 0:b1b7113a1441 | 166 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 167 | RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0); |
chapman515 | 0:b1b7113a1441 | 168 | RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0); |
chapman515 | 0:b1b7113a1441 | 169 | RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0); |
chapman515 | 0:b1b7113a1441 | 170 | RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0); |
chapman515 | 0:b1b7113a1441 | 171 | |
chapman515 | 0:b1b7113a1441 | 172 | THavg.t = (THavg.t+THdata[counter].t)/10; |
chapman515 | 0:b1b7113a1441 | 173 | THavg.h = (THavg.h+THdata[counter].h)/10; |
chapman515 | 0:b1b7113a1441 | 174 | |
chapman515 | 0:b1b7113a1441 | 175 | luxavg = (luxavg+luxdata[counter])/10; |
chapman515 | 0:b1b7113a1441 | 176 | } |
chapman515 | 0:b1b7113a1441 | 177 | else{ |
chapman515 | 0:b1b7113a1441 | 178 | |
chapman515 | 0:b1b7113a1441 | 179 | RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 180 | RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 181 | RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 182 | RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 183 | |
chapman515 | 0:b1b7113a1441 | 184 | THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1); |
chapman515 | 0:b1b7113a1441 | 185 | THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1); |
chapman515 | 0:b1b7113a1441 | 186 | |
chapman515 | 0:b1b7113a1441 | 187 | luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1); |
chapman515 | 0:b1b7113a1441 | 188 | } |
chapman515 | 0:b1b7113a1441 | 189 | |
chapman515 | 0:b1b7113a1441 | 190 | BLE_Can_receive = 0; |
chapman515 | 0:b1b7113a1441 | 191 | wait_ms(5); |
chapman515 | 0:b1b7113a1441 | 192 | pc.printf("%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg); |
chapman515 | 0:b1b7113a1441 | 193 | counter = counter++; |
chapman515 | 0:b1b7113a1441 | 194 | |
chapman515 | 0:b1b7113a1441 | 195 | if (counter == 10) //10 = all element is filled |
chapman515 | 0:b1b7113a1441 | 196 | allfilled = true; |
chapman515 | 0:b1b7113a1441 | 197 | |
chapman515 | 0:b1b7113a1441 | 198 | wait(1); |
chapman515 | 0:b1b7113a1441 | 199 | BLE_Can_receive = 1; |
chapman515 | 0:b1b7113a1441 | 200 | } |
chapman515 | 0:b1b7113a1441 | 201 | } |