Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017
Dependencies: ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822
Fork of UPAS_BLE_and_USB by
Diff: main.cpp
- Revision:
- 35:bd978e6ccd82
- Parent:
- 34:481403146690
- Child:
- 37:838824df3f5f
diff -r 481403146690 -r bd978e6ccd82 main.cpp --- a/main.cpp Sun May 31 22:03:37 2015 +0000 +++ b/main.cpp Tue Jun 02 20:33:49 2015 +0000 @@ -2,16 +2,16 @@ #include "SDFileSystem.h" #include "Adafruit_ADS1015.h" #include "MCP40D17.h" -#include "wire.h" //Used with the RTC in this code. Will need to try and remove this, and also create the correct library/update addresses and registers for the RTC. #include "STC3100.h" #include "LSM303.h" #include "BME280.h" #include "SI1145.h" +#include "NCP5623BMUTBG.h" +#include "CronoDot.h" #define SERIAL_BAUD_RATE 9600 -#define SCL 20 +#define SCL 20 #define SDA 22 -#define Crono 0xD0 //D0 for the chronoDot I2C i2c(p22, p20); Adafruit_ADS1115 ads(&i2c); @@ -23,6 +23,8 @@ DigitalOut pbKill(p18, 1); LSM303 movementsensor(p22, p20); SI1145 lightsensor(p22, p20); +NCP5623BMUTBG RGBLED(p22, p20); +CronoDot RTC(p22, p20); float press; float temp; @@ -50,7 +52,7 @@ float massflow; //g/min float volflow; //L/min float volflowSet = 1.0; //L/min -float massflowSet; +float massflowSet; float deltaflow; float gainFlow = 1.0; @@ -58,139 +60,104 @@ int digital_pot_setpointCK; char filename[] = "/sd/UPAS0005LOG000000000000.txt"; -TwoWire Wire = TwoWire(NRF_TWI0); +//TwoWire Wire = TwoWire(NRF_TWI0); SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14) -uint8_t Seconds = 0;//Seconds -uint8_t Minutes = 0;//Minutes -uint8_t Hour = 0;//Hour -uint8_t Date = 0;//Date -uint8_t Month = 0;//Month -uint8_t Year = 0;//Year +char * RTCtime; + double secondsD = 0; -void get_time() -{ - Wire.beginTransmission(Crono); // address DS3231 - Wire.write(0x0E); // select register - Wire.write(0x1C); // write register bitmap, bit 7 is /EOSC - Wire.endTransmission(); - wait(0.05); - Wire.beginTransmission(Crono); - Wire.write(0x00); - Wire.endTransmission(); - Wire.requestFrom(Crono+1, 7); - - while( Wire.available() > 0 ) - { - Seconds = Wire.read(); - Minutes = Wire.read(); - Hour = Wire.read(); - uint8_t day =Wire.read(); // we don't uses this, it's just day of the week - Date = Wire.read(); - Month = Wire.read(); - Year = Wire.read(); - } - Year = ((Year&0xF0)>>4)*10 + (Year&0x0F); //Year - Month = ((Month&0x10)>>4)*10 + (Month&0x0F); //Month - Date = ((Date&0x30)>>4)*10 + (Date&0x0F); //Date - Hour = ((Hour&0x30)>>4)*10 + (Hour&0x0F); //Hour - Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F); //Minutes - Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F); //Seconds -} - int main() { + + RGBLED.set_led(31, 0, 0); // (R, G, B) max of 31 (five bits) + press = bmesensor.getPressure(); temp = bmesensor.getTemperature(); rh = bmesensor.getHumidity(); - + atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); massflowSet = volflowSet*atmoRho; //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx digital_pot_setpoint = (int)floor(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00 - - DigPot.writeRegister(digital_pot_setpoint); - Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// - wait(1); - blower = 1; - - get_time(); - sprintf(filename, "/sd/UPAS0005LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",Year,Month,Date,Hour,Minutes,Seconds); + + DigPot.writeRegister(digital_pot_setpoint); + //Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// + wait(1); + blower = 1; + + RTCtime = RTC.get_time(); + sprintf(filename, "/sd/UPAS0005LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",RTCtime[5],RTCtime[4],RTCtime[3],RTCtime[2],RTCtime[1],RTCtime[0]); FILE *fp = fopen(filename, "w"); fclose(fp); - - while(1){ - - get_time(); - secondsD = Seconds; - - if(fmod(secondsD,2)==0){ - omronVolt = (omronReading*4.096)/(32768*2); - //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx - massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; - digital_pot_setpointCK = (int)floor(9.2456*pow(massflow,4)-89.538*pow(massflow,3)+329.03*pow(massflow,2)-566.12*massflow+412.72); //min = 0x7F, max = 0x00 - atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); - massflowSet = volflowSet*atmoRho; - digital_pot_setpoint = (int)floor(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00 - deltaflow = massflow-massflowSet; - - if(abs(deltaflow)>.025*massflowSet){ - digital_pot_setpoint = digital_pot_setpoint+gainFlow*(digital_pot_setpointCK-digital_pot_setpoint); - DigPot.writeRegister(digital_pot_setpoint); + + while(1) { + + RTCtime = RTC.get_time(); + secondsD = RTCtime[0]; + + if(fmod(secondsD,2)==0) { + omronVolt = (omronReading*4.096)/(32768*2); + //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx + massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; + digital_pot_setpointCK = (int)floor(9.2456*pow(massflow,4)-89.538*pow(massflow,3)+329.03*pow(massflow,2)-566.12*massflow+412.72); //min = 0x7F, max = 0x00 + atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); + massflowSet = volflowSet*atmoRho; + digital_pot_setpoint = (int)floor(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00 + deltaflow = massflow-massflowSet; + + if(abs(deltaflow)>.025*massflowSet) { + digital_pot_setpoint = digital_pot_setpoint+gainFlow*(digital_pot_setpointCK-digital_pot_setpoint); + DigPot.writeRegister(digital_pot_setpoint); + } + + } - - - } - - if(fmod(secondsD,10)==0){ - - omronVolt = (omronReading*4.096)/(32768*2); - //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx - massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; - atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); - volflow = massflow/atmoRho; - deltaflow = volflow-volflowSet; - - - movementsensor.getACCEL(); - movementsensor.getCOMPASS(); - accel_x = movementsensor.AccelData.x; - accel_y = movementsensor.AccelData.y; - accel_z = movementsensor.AccelData.z; - mag_x = movementsensor.MagData.x; - mag_y = movementsensor.MagData.y; - mag_z = movementsensor.MagData.z; - omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V - vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 - vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 - omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 - press = bmesensor.getPressure(); - temp = bmesensor.getTemperature()-3; - rh = bmesensor.getHumidity(); - uv = lightsensor.getUV(); - vis = lightsensor.getVIS(); - ir = lightsensor.getIR(); + + if(fmod(secondsD,10)==0) { + + omronVolt = (omronReading*4.096)/(32768*2); + //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx + massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; + atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15)); + volflow = massflow/atmoRho; + deltaflow = volflow-volflowSet; + - - //Mount the filesystem - sd.mount(); - FILE *fp = fopen(filename, "a"); - fprintf(fp, "%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", Year,Month,Date,Hour,Minutes,Seconds,omronVolt,atmoRho,volflow,massflow,temp,press,rh,accel_x, accel_y, accel_z, mag_x, mag_y, mag_z, uv, vis, ir, omronReading,vInReading, vBlowerReading, omronDiff, gasG.getAmps(), gasG.getVolts(), gasG.getCharge(), digital_pot_setpoint); - fclose(fp); - //Unmount the filesystem - sd.unmount(); - - wait(1); - - } - - - - - + movementsensor.getACCEL(); + movementsensor.getCOMPASS(); + accel_x = movementsensor.AccelData.x; + accel_y = movementsensor.AccelData.y; + accel_z = movementsensor.AccelData.z; + mag_x = movementsensor.MagData.x; + mag_y = movementsensor.MagData.y; + mag_z = movementsensor.MagData.z; + omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V + vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 + vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 + omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 + press = bmesensor.getPressure(); + temp = bmesensor.getTemperature()-3; + rh = bmesensor.getHumidity(); + uv = lightsensor.getUV(); + vis = lightsensor.getVIS(); + ir = lightsensor.getIR(); + + + //Mount the filesystem + sd.mount(); + FILE *fp = fopen(filename, "a"); + fprintf(fp, "%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", RTCtime[5],RTCtime[4],RTCtime[3],RTCtime[2],RTCtime[1],RTCtime[0],omronVolt,atmoRho,volflow,massflow,temp,press,rh,accel_x, accel_y, accel_z, mag_x, mag_y, mag_z, uv, vis, ir, omronReading,vInReading, vBlowerReading, omronDiff, gasG.getAmps(), gasG.getVolts(), gasG.getCharge(), digital_pot_setpoint); + fclose(fp); + //Unmount the filesystem + sd.unmount(); + + wait(1); + + } + } }