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
main.cpp@35:bd978e6ccd82, 2015-06-02 (annotated)
- Committer:
- joshuasmth04
- Date:
- Tue Jun 02 20:33:49 2015 +0000
- Revision:
- 35:bd978e6ccd82
- Parent:
- 34:481403146690
- Child:
- 37:838824df3f5f
Removed wire.h library and replaced it with the CronoDot library. Also added the NCP5623BMUTBG library for control over RGB LEDs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caseyquinn | 0:14d46ef4b6cb | 1 | #include "mbed.h" |
caseyquinn | 0:14d46ef4b6cb | 2 | #include "SDFileSystem.h" |
caseyquinn | 0:14d46ef4b6cb | 3 | #include "Adafruit_ADS1015.h" |
caseyquinn | 1:37babeb68ab9 | 4 | #include "MCP40D17.h" |
caseyquinn | 7:a24d7156bc02 | 5 | #include "STC3100.h" |
caseyquinn | 10:f9cb61b29340 | 6 | #include "LSM303.h" |
caseyquinn | 12:8c00a7f5d483 | 7 | #include "BME280.h" |
caseyquinn | 14:ad550174db8b | 8 | #include "SI1145.h" |
joshuasmth04 | 35:bd978e6ccd82 | 9 | #include "NCP5623BMUTBG.h" |
joshuasmth04 | 35:bd978e6ccd82 | 10 | #include "CronoDot.h" |
caseyquinn | 1:37babeb68ab9 | 11 | |
caseyquinn | 1:37babeb68ab9 | 12 | #define SERIAL_BAUD_RATE 9600 |
joshuasmth04 | 35:bd978e6ccd82 | 13 | #define SCL 20 |
caseyquinn | 7:a24d7156bc02 | 14 | #define SDA 22 |
caseyquinn | 0:14d46ef4b6cb | 15 | |
caseyquinn | 0:14d46ef4b6cb | 16 | I2C i2c(p22, p20); |
caseyquinn | 0:14d46ef4b6cb | 17 | Adafruit_ADS1115 ads(&i2c); |
caseyquinn | 1:37babeb68ab9 | 18 | MCP40D17 DigPot(&i2c); |
caseyquinn | 12:8c00a7f5d483 | 19 | BME280 bmesensor(p22, p20); |
caseyquinn | 7:a24d7156bc02 | 20 | STC3100 gasG(p22, p20); |
caseyquinn | 2:e596e685eb39 | 21 | Serial pc(USBTX, USBRX); |
caseyquinn | 8:204c21adf693 | 22 | DigitalOut blower(p29, 0); |
caseyquinn | 17:1baf7cab694e | 23 | DigitalOut pbKill(p18, 1); |
caseyquinn | 10:f9cb61b29340 | 24 | LSM303 movementsensor(p22, p20); |
caseyquinn | 14:ad550174db8b | 25 | SI1145 lightsensor(p22, p20); |
joshuasmth04 | 35:bd978e6ccd82 | 26 | NCP5623BMUTBG RGBLED(p22, p20); |
joshuasmth04 | 35:bd978e6ccd82 | 27 | CronoDot RTC(p22, p20); |
caseyquinn | 0:14d46ef4b6cb | 28 | |
caseyquinn | 20:ad9883973d86 | 29 | float press; |
caseyquinn | 20:ad9883973d86 | 30 | float temp; |
caseyquinn | 20:ad9883973d86 | 31 | float rh; |
caseyquinn | 4:69bd7e8a994c | 32 | |
caseyquinn | 20:ad9883973d86 | 33 | int uv; |
caseyquinn | 20:ad9883973d86 | 34 | int vis; |
caseyquinn | 20:ad9883973d86 | 35 | int ir; |
caseyquinn | 8:204c21adf693 | 36 | |
caseyquinn | 10:f9cb61b29340 | 37 | float accel_x; |
caseyquinn | 10:f9cb61b29340 | 38 | float accel_y; |
caseyquinn | 10:f9cb61b29340 | 39 | float accel_z; |
caseyquinn | 10:f9cb61b29340 | 40 | float mag_x; |
caseyquinn | 10:f9cb61b29340 | 41 | float mag_y; |
caseyquinn | 10:f9cb61b29340 | 42 | float mag_z; |
caseyquinn | 10:f9cb61b29340 | 43 | |
caseyquinn | 20:ad9883973d86 | 44 | |
caseyquinn | 0:14d46ef4b6cb | 45 | int vInReading; |
caseyquinn | 0:14d46ef4b6cb | 46 | int vBlowerReading; |
caseyquinn | 0:14d46ef4b6cb | 47 | int omronDiff; |
caseyquinn | 20:ad9883973d86 | 48 | |
caseyquinn | 20:ad9883973d86 | 49 | int omronReading; |
caseyquinn | 20:ad9883973d86 | 50 | float omronVolt; //V |
caseyquinn | 20:ad9883973d86 | 51 | float atmoRho; //g/L |
caseyquinn | 20:ad9883973d86 | 52 | float massflow; //g/min |
caseyquinn | 20:ad9883973d86 | 53 | float volflow; //L/min |
caseyquinn | 20:ad9883973d86 | 54 | float volflowSet = 1.0; //L/min |
joshuasmth04 | 35:bd978e6ccd82 | 55 | float massflowSet; |
caseyquinn | 20:ad9883973d86 | 56 | float deltaflow; |
caseyquinn | 34:481403146690 | 57 | float gainFlow = 1.0; |
caseyquinn | 20:ad9883973d86 | 58 | |
caseyquinn | 22:8dd7e0d4c21c | 59 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
caseyquinn | 34:481403146690 | 60 | int digital_pot_setpointCK; |
lionberg | 33:bc73a2493821 | 61 | char filename[] = "/sd/UPAS0005LOG000000000000.txt"; |
caseyquinn | 7:a24d7156bc02 | 62 | |
joshuasmth04 | 35:bd978e6ccd82 | 63 | //TwoWire Wire = TwoWire(NRF_TWI0); |
caseyquinn | 7:a24d7156bc02 | 64 | 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) |
caseyquinn | 7:a24d7156bc02 | 65 | |
joshuasmth04 | 35:bd978e6ccd82 | 66 | char * RTCtime; |
joshuasmth04 | 35:bd978e6ccd82 | 67 | |
caseyquinn | 11:2297d364af61 | 68 | double secondsD = 0; |
caseyquinn | 7:a24d7156bc02 | 69 | |
caseyquinn | 20:ad9883973d86 | 70 | |
caseyquinn | 0:14d46ef4b6cb | 71 | int main() |
caseyquinn | 0:14d46ef4b6cb | 72 | { |
joshuasmth04 | 35:bd978e6ccd82 | 73 | |
joshuasmth04 | 35:bd978e6ccd82 | 74 | RGBLED.set_led(31, 0, 0); // (R, G, B) max of 31 (five bits) |
joshuasmth04 | 35:bd978e6ccd82 | 75 | |
caseyquinn | 22:8dd7e0d4c21c | 76 | press = bmesensor.getPressure(); |
caseyquinn | 22:8dd7e0d4c21c | 77 | temp = bmesensor.getTemperature(); |
caseyquinn | 22:8dd7e0d4c21c | 78 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 35:bd978e6ccd82 | 79 | |
caseyquinn | 22:8dd7e0d4c21c | 80 | 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)); |
caseyquinn | 22:8dd7e0d4c21c | 81 | massflowSet = volflowSet*atmoRho; |
lionberg | 26:9cf8e76d8274 | 82 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
lionberg | 33:bc73a2493821 | 83 | 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 |
joshuasmth04 | 35:bd978e6ccd82 | 84 | |
joshuasmth04 | 35:bd978e6ccd82 | 85 | DigPot.writeRegister(digital_pot_setpoint); |
joshuasmth04 | 35:bd978e6ccd82 | 86 | //Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// |
joshuasmth04 | 35:bd978e6ccd82 | 87 | wait(1); |
joshuasmth04 | 35:bd978e6ccd82 | 88 | blower = 1; |
joshuasmth04 | 35:bd978e6ccd82 | 89 | |
joshuasmth04 | 35:bd978e6ccd82 | 90 | RTCtime = RTC.get_time(); |
joshuasmth04 | 35:bd978e6ccd82 | 91 | sprintf(filename, "/sd/UPAS0005LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",RTCtime[5],RTCtime[4],RTCtime[3],RTCtime[2],RTCtime[1],RTCtime[0]); |
caseyquinn | 17:1baf7cab694e | 92 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 17:1baf7cab694e | 93 | fclose(fp); |
caseyquinn | 2:e596e685eb39 | 94 | |
joshuasmth04 | 35:bd978e6ccd82 | 95 | |
joshuasmth04 | 35:bd978e6ccd82 | 96 | while(1) { |
joshuasmth04 | 35:bd978e6ccd82 | 97 | |
joshuasmth04 | 35:bd978e6ccd82 | 98 | RTCtime = RTC.get_time(); |
joshuasmth04 | 35:bd978e6ccd82 | 99 | secondsD = RTCtime[0]; |
joshuasmth04 | 35:bd978e6ccd82 | 100 | |
joshuasmth04 | 35:bd978e6ccd82 | 101 | if(fmod(secondsD,2)==0) { |
joshuasmth04 | 35:bd978e6ccd82 | 102 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 35:bd978e6ccd82 | 103 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 35:bd978e6ccd82 | 104 | massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; |
joshuasmth04 | 35:bd978e6ccd82 | 105 | 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 |
joshuasmth04 | 35:bd978e6ccd82 | 106 | 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)); |
joshuasmth04 | 35:bd978e6ccd82 | 107 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 35:bd978e6ccd82 | 108 | 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 |
joshuasmth04 | 35:bd978e6ccd82 | 109 | deltaflow = massflow-massflowSet; |
joshuasmth04 | 35:bd978e6ccd82 | 110 | |
joshuasmth04 | 35:bd978e6ccd82 | 111 | if(abs(deltaflow)>.025*massflowSet) { |
joshuasmth04 | 35:bd978e6ccd82 | 112 | digital_pot_setpoint = digital_pot_setpoint+gainFlow*(digital_pot_setpointCK-digital_pot_setpoint); |
joshuasmth04 | 35:bd978e6ccd82 | 113 | DigPot.writeRegister(digital_pot_setpoint); |
joshuasmth04 | 35:bd978e6ccd82 | 114 | } |
joshuasmth04 | 35:bd978e6ccd82 | 115 | |
joshuasmth04 | 35:bd978e6ccd82 | 116 | |
caseyquinn | 34:481403146690 | 117 | } |
joshuasmth04 | 35:bd978e6ccd82 | 118 | |
joshuasmth04 | 35:bd978e6ccd82 | 119 | if(fmod(secondsD,10)==0) { |
joshuasmth04 | 35:bd978e6ccd82 | 120 | |
joshuasmth04 | 35:bd978e6ccd82 | 121 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 35:bd978e6ccd82 | 122 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 35:bd978e6ccd82 | 123 | massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; |
joshuasmth04 | 35:bd978e6ccd82 | 124 | 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)); |
joshuasmth04 | 35:bd978e6ccd82 | 125 | volflow = massflow/atmoRho; |
joshuasmth04 | 35:bd978e6ccd82 | 126 | deltaflow = volflow-volflowSet; |
joshuasmth04 | 35:bd978e6ccd82 | 127 | |
caseyquinn | 20:ad9883973d86 | 128 | |
joshuasmth04 | 35:bd978e6ccd82 | 129 | movementsensor.getACCEL(); |
joshuasmth04 | 35:bd978e6ccd82 | 130 | movementsensor.getCOMPASS(); |
joshuasmth04 | 35:bd978e6ccd82 | 131 | accel_x = movementsensor.AccelData.x; |
joshuasmth04 | 35:bd978e6ccd82 | 132 | accel_y = movementsensor.AccelData.y; |
joshuasmth04 | 35:bd978e6ccd82 | 133 | accel_z = movementsensor.AccelData.z; |
joshuasmth04 | 35:bd978e6ccd82 | 134 | mag_x = movementsensor.MagData.x; |
joshuasmth04 | 35:bd978e6ccd82 | 135 | mag_y = movementsensor.MagData.y; |
joshuasmth04 | 35:bd978e6ccd82 | 136 | mag_z = movementsensor.MagData.z; |
joshuasmth04 | 35:bd978e6ccd82 | 137 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 35:bd978e6ccd82 | 138 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
joshuasmth04 | 35:bd978e6ccd82 | 139 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
joshuasmth04 | 35:bd978e6ccd82 | 140 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
joshuasmth04 | 35:bd978e6ccd82 | 141 | press = bmesensor.getPressure(); |
joshuasmth04 | 35:bd978e6ccd82 | 142 | temp = bmesensor.getTemperature()-3; |
joshuasmth04 | 35:bd978e6ccd82 | 143 | rh = bmesensor.getHumidity(); |
joshuasmth04 | 35:bd978e6ccd82 | 144 | uv = lightsensor.getUV(); |
joshuasmth04 | 35:bd978e6ccd82 | 145 | vis = lightsensor.getVIS(); |
joshuasmth04 | 35:bd978e6ccd82 | 146 | ir = lightsensor.getIR(); |
joshuasmth04 | 35:bd978e6ccd82 | 147 | |
joshuasmth04 | 35:bd978e6ccd82 | 148 | |
joshuasmth04 | 35:bd978e6ccd82 | 149 | //Mount the filesystem |
joshuasmth04 | 35:bd978e6ccd82 | 150 | sd.mount(); |
joshuasmth04 | 35:bd978e6ccd82 | 151 | FILE *fp = fopen(filename, "a"); |
joshuasmth04 | 35:bd978e6ccd82 | 152 | 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); |
joshuasmth04 | 35:bd978e6ccd82 | 153 | fclose(fp); |
joshuasmth04 | 35:bd978e6ccd82 | 154 | //Unmount the filesystem |
joshuasmth04 | 35:bd978e6ccd82 | 155 | sd.unmount(); |
joshuasmth04 | 35:bd978e6ccd82 | 156 | |
joshuasmth04 | 35:bd978e6ccd82 | 157 | wait(1); |
joshuasmth04 | 35:bd978e6ccd82 | 158 | |
joshuasmth04 | 35:bd978e6ccd82 | 159 | } |
joshuasmth04 | 35:bd978e6ccd82 | 160 | |
caseyquinn | 0:14d46ef4b6cb | 161 | } |
caseyquinn | 0:14d46ef4b6cb | 162 | } |
caseyquinn | 0:14d46ef4b6cb | 163 | |
caseyquinn | 0:14d46ef4b6cb | 164 | |
caseyquinn | 0:14d46ef4b6cb | 165 |