a
Dependencies: mbed Sht31 MAX44009
Diff: main.cpp
- Revision:
- 0:b1b7113a1441
- Child:
- 1:3889765eb394
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 13 08:52:36 2020 +0000 @@ -0,0 +1,201 @@ +#include "mbed.h" +#include "Sht31.h" +#include "MAX44009.h" +#include "mbed_wait_api.h" +#include <list> + +Serial pc(p13,p14); +Sht31 sht31(p9, p10); +MAX44009 myMAX44009 ( p9, p10, MAX44009::MAX44009_ADDRESS_A0_GND, 400000 ); +I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL) + + + +DigitalOut BLE_Can_receive(p11); // 0 = can recieve + +class RGB{ + public : int C; + int R; + int G; + int B; + }; +class RGBf{ + public : float C; + float R; + float G; + float B; + }; +class TandH{ + public : float t; + float h; + }; + +RGB RGBdata[10]; +TandH THdata[10]; +float luxdata[10]; + +int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead; + +bool allfilled = false; +RGBf RGBavg; +TandH THavg; +float luxavg; + + +int main() +{ + pc.baud ( 19200 ); + + + while(1) { + counter = counter%10; + //BLE_Can_receive = 0; + //wait_ms(5); + //pc.printf("counter31 = %d\n", counter); + //BLE_Can_receive = 1; + { + int sensor_addr = 41 << 1; + char id_regval[1] = {146}; + char data[1] = {0}; + i2c.write(sensor_addr,id_regval,1, true); + i2c.read(sensor_addr,data,1,false); + + + // Initialize color sensor + + char timing_register[2] = {129,192}; + //char timing_register[2] = {129,0}; + i2c.write(sensor_addr,timing_register,2,false); + + char control_register[2] = {143,0}; + char temp[2]={0,0}; + //char control_register[2] = {143,3}; + i2c.write(sensor_addr,control_register,2,false); + + char enable_register[2] = {128,3}; + i2c.write(sensor_addr,enable_register,2,false); + + // Read data from color sensor (Clear/Red/Green/Blue) + + char clear_reg[1] = {148}; + char clear_data[2] = {0,0}; + i2c.write(sensor_addr,clear_reg,1, true); + i2c.read(sensor_addr,clear_data,2, false); + + int clear_value = ((int)clear_data[1] << 8) | clear_data[0]; + + char red_reg[1] = {150}; + char red_data[2] = {0,0}; + i2c.write(sensor_addr,red_reg,1, true); + i2c.read(sensor_addr,red_data,2, false); + + int red_value = ((int)red_data[1] << 8) | red_data[0]; + + char green_reg[1] = {152}; + char green_data[2] = {0,0}; + i2c.write(sensor_addr,green_reg,1, true); + i2c.read(sensor_addr,green_data,2, false); + + int green_value = ((int)green_data[1] << 8) | green_data[0]; + + char blue_reg[1] = {154}; + char blue_data[2] = {0,0}; + i2c.write(sensor_addr,blue_reg,1, true); + i2c.read(sensor_addr,blue_data,2, false); + + int blue_value = ((int)blue_data[1] << 8) | blue_data[0]; + + // print sensor readings + if (allfilled == true){ + RGBavg.C = RGBavg.C*10-RGBdata[counter].C; + RGBavg.R = RGBavg.R*10-RGBdata[counter].R; + RGBavg.G = RGBavg.G*10-RGBdata[counter].G; + RGBavg.B = RGBavg.B*10-RGBdata[counter].B; + } + RGBdata[counter].C= clear_value; + RGBdata[counter].R= red_value; + RGBdata[counter].G= green_value; + RGBdata[counter].B= blue_value; + //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value); + } + { + + MAX44009::MAX44009_status_t aux; + MAX44009::MAX44009_vector_data_t myMAX44009_Data; + + 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 ); + aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data ); + aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data ); + aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data ); + + if (allfilled == true){ + luxavg = luxavg*10-luxdata[counter]; + } + + luxdata[counter] = myMAX44009_Data.lux; + + + } + + { + + //float t = sht31.readTemperature(); + //float h = sht31.readHumidity(); + + if (allfilled == true){ + THavg.t = THavg.t*10-THdata[counter].t; + THavg.h = THavg.h*10-THdata[counter].h; + } + THdata[counter].t = sht31.readTemperature(); + THdata[counter].h = sht31.readHumidity(); + + //pc.printf("[TEMP/HUM]"); + + } +//////////////////////////////////////////////////////////////////// + + /*BLE_Can_receive = 0; + wait_ms(5); + pc.printf("counter119\n = %d", counter); + for (int i =0;i<10;i++){ + pc.printf("i=%d ,", i); + 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]); + } + BLE_Can_receive = 1;*/ + + if (allfilled == true){ + RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0); + RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0); + RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0); + RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0); + + THavg.t = (THavg.t+THdata[counter].t)/10; + THavg.h = (THavg.h+THdata[counter].h)/10; + + luxavg = (luxavg+luxdata[counter])/10; + } + else{ + + RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1); + RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1); + RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1); + RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1); + + THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1); + THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1); + + luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1); + } + + BLE_Can_receive = 0; + wait_ms(5); + 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); + counter = counter++; + + if (counter == 10) //10 = all element is filled + allfilled = true; + + wait(1); + BLE_Can_receive = 1; + } +}