Volckens Group Sensors / UPAS_BLE_SD_iOS

Dependencies:   ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822

Fork of UPAS_BLE_and_USB by Volckens Group Sensors

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "Adafruit_ADS1015.h"
00004 #include "MCP40D17.h"
00005 #include "STC3100.h"
00006 #include "LSM303.h"
00007 #include "BME280.h"
00008 #include "SI1145.h"
00009 #include "NCP5623BMUTBG.h"
00010 #include "CronoDot.h"
00011 #include "EEPROM.h"
00012 #include "BLEDevice.h"
00013 #include "Calibration.h"
00014 #include "UPAS_Service.h"
00015 
00016 #define SERIAL_BAUD_RATE    9600
00017 #define SCL         20
00018 #define SDA         22
00019 
00020 
00021 
00022 
00023 
00024 uint8_t startAndEndTime[12] = {0,};
00025 uint8_t logIntervalReadOut[1] = {0,}; 
00026 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
00027 BLEDevice   ble;
00028 UPAS_Service *upasServicePtr;
00029 //BLE_Menu    bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService);
00030 
00031 I2C                 i2c(p22, p20);
00032 Adafruit_ADS1115    ads(&i2c);
00033 MCP40D17            DigPot(&i2c);
00034 BME280              bmesensor(p22, p20);
00035 STC3100             gasG(p22, p20);
00036 Serial              pc(USBTX, USBRX);
00037 DigitalOut          blower(p29, 0);
00038 DigitalOut          pbKill(p18, 1);
00039 LSM303              movementsensor(p22, p20);
00040 SI1145              lightsensor(p22, p20);
00041 NCP5623BMUTBG       RGB_LED(p22, p20);
00042 CronoDot            RTC(p22, p20);
00043 EEPROM              E2PROM(p22, p20);
00044 DigitalOut          GPS_EN(p4,0);       //pin 4 is used to enable and disable the GPS, in order to recive serial communications
00045 Calibration         calibrations(1);     //Default serial/calibration if there are no values for the selected option
00046 
00047 //Timeout         stop;   //This is the stop call back object
00048 //Timeout         logg;   //This is the logging call back object
00049 
00050 
00051 
00052 uint16_t serial_num = 1;                // Default serial/calibration number
00053 int RunReady =0;
00054 
00055 
00056 float press;
00057 float temp;
00058 float rh;
00059 
00060 int uv;
00061 int vis;
00062 int ir;
00063 
00064 
00065 float compass;
00066 float accel_x;
00067 float accel_y;
00068 float accel_z;
00069 float accel_comp;
00070 float angle_x;
00071 float angle_y;
00072 float angle_z;
00073 float mag_x;
00074 float mag_y;
00075 float mag_z;
00076 
00077 
00078 int vInReading;
00079 int vBlowerReading;
00080 int omronDiff;
00081 float omronVolt; //V
00082 int omronReading;
00083 float atmoRho; //g/L
00084 
00085 float massflow; //g/min
00086 float volflow; //L/min
00087 float volflowSet = 1.0; //L/min
00088 int   logInerval = 10; //seconds
00089 double secondsD = 0;
00090 double lastsecondD = 0;
00091 float massflowSet;
00092 float deltaVflow = 0.0;
00093 float deltaMflow = 0.0;
00094 float gainFlow;
00095 float sampledVol; //L, total sampled volume
00096 
00097 int digital_pot_setpoint; //min = 0x7F, max = 0x00
00098 int digital_pot_set;
00099 int digital_pot_change;
00100 int digitalpotMax = 127;
00101 int digitalpotMin = 5;
00102 
00103 int dutyUp;
00104 int dutyDown;
00105 
00106 // variables are only place holders for the US_Menu //
00107 int refreshtime;
00108 float home_lat, home_lon, work_lat, work_lon;
00109 //*************************************************//
00110 
00111 //int refresh_Time = 10;   // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
00112 
00113 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
00114 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)
00115 
00116 
00117 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00118 {
00119     ble.startAdvertising();
00120 }
00121 
00122 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message
00123 {
00124    uint8_t *writeData =  const_cast<uint8_t*>(Handler->data);
00125     // check to see what characteristic was written, by handle
00126     if(Handler->charHandle == upasServicePtr->rtcCharacteristic.getValueAttribute().getHandle()) {
00127 
00128 
00129         
00130         //E2PROM.write(0x00015, writeData+6, 12); This line needs to be omitted for android app to function. Should not have stuck around 
00131         RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
00132    
00133     }else if(Handler->charHandle == upasServicePtr->sampleTimeCharacteristic.getValueAttribute().getHandle()){
00134         
00135         E2PROM.write(0x00015, writeData, 12);
00136     
00137     }else if(Handler->charHandle == upasServicePtr->subjectLabelCharacteristic.getValueAttribute().getHandle()){
00138         E2PROM.write(0x00001,writeData,15);
00139     
00140     }else if(Handler->charHandle == upasServicePtr->runReadyCharacteristic.getValueAttribute().getHandle()){
00141         
00142         uint8_t runData = writeData[0];
00143         E2PROM.write(0x00033,writeData,1);
00144         
00145         if(runData == 0x0A){
00146             RunReady = 10;
00147         }else{
00148             RunReady = 12;
00149         }       
00150     
00151     }else if(Handler->charHandle == upasServicePtr->logIntevalCharacteristic.getValueAttribute().getHandle()){
00152         /* Trigger demo mode*/
00153         //RGB_LED.set_led(3,1,0);
00154         E2PROM.write(0x00014,writeData,1);
00155     
00156     }else if(Handler->charHandle == upasServicePtr->flowRateCharacteristic.getValueAttribute().getHandle()){
00157     //RGB_LED.set_led(3,1,0);
00158         E2PROM.write(0x00010,writeData,4);
00159     
00160         //float volFlowTester = 0.0;
00161         //uint8_t flowRateTestBytes[4] = {0,};
00162         //E2PROM.read(0x00010,flowRateTestBytes,4);
00163         //E2PROM.byteToFloat(flowRateTestBytes, &volFlowTester);
00164         
00165     }else if(Handler->charHandle == upasServicePtr->serialNumCharacteristic.getValueAttribute().getHandle()){
00166         E2PROM.write(0x00034,writeData,2);    
00167     }
00168 
00169 }
00170 
00171 
00172 
00173 /*EEPROM ADDRESSING:
00174     0:Status bit-Unused
00175     1-15:Device Name
00176     16-19:Flow Rate
00177     20: Data Log Interval
00178     21-26: Start Time: ssmmHHddMMyy
00179     27-32: Stop Time: ssmmHHddMMyy
00180     33: Duty Up
00181     34: Duty Down
00182     35-38: Home Latitude
00183     39-42: Home Longitude
00184     43-46: Work Latitude
00185     47-50: Work Longitude
00186     51: Runready: Currently useless, should be 0
00187     52-53: Device Calibration
00188     54: Consider RunReady
00189     55-56: Menu Options
00190     57+ Nothing*/
00191 int main()
00192 {
00193     RGB_LED.set_led(1,1,1);
00194 // Setup and Initialization
00195 //---------------------------------------------------------------------------------------------//
00196     //**************//BLE initialization//**************//
00197     RTC.get_time();
00198     uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
00199     
00200     uint8_t sampleTimePassValues[12] = {0x00,0x00,0x0A,0x01,0x01,0x10,0x00,0x00,0x0A,0x01,0x01,0x10}; //Start 1/1/16 12:00:00 End 1/1/16 12:00:00
00201     uint8_t subjectLabelOriginal[8] = {0x52,0x45,0x53,0x45,0x54,0x5F,0x5F,0x5f}; // RESET___
00202     uint8_t dataLogOriginal[1] = {0x0A,}; //10 seconds
00203     uint8_t flowRateOriginal[4] = {0x00,0x00,0x80,0x3F}; //1.0 LPM
00204     
00205     uint8_t serialNumberAndType[6] = {0x50,0x53};
00206    
00207     //Code/conditional set up to prevent bad values in the EEPROM.  Does not set serial number currently
00208     uint8_t eepromFlag[1] = {0,}; 
00209     E2PROM.read(0x00071,eepromFlag,1); 
00210     if(eepromFlag[0]!= 0x0B){  
00211         E2PROM.write(0x00015, sampleTimePassValues, 12);
00212         E2PROM.write(0x00001, subjectLabelOriginal,8);
00213         E2PROM.write(0x00014,dataLogOriginal,1);
00214         E2PROM.write(0x00010,flowRateOriginal,4);
00215         
00216         eepromFlag[0] = 0x0B;
00217         E2PROM.write(0x00071,eepromFlag,1);
00218     }
00219     
00220     
00221     //Retrieve current eeprom values
00222     E2PROM.read(0x00015, sampleTimePassValues, 12);
00223     E2PROM.read(0x00001, subjectLabelOriginal,8);
00224     E2PROM.read(0x00014,dataLogOriginal,1);
00225     E2PROM.read(0x00010,flowRateOriginal,4);
00226     
00227     
00228     //Following 12 lines of code parses the serial number from a byte id to a readable set of characters for the app user to see
00229     E2PROM.read(0x00034,serialNumberAndType+2,2);
00230     int tempSerialNum = serialNumberAndType[2]+serialNumberAndType[3];
00231     int serialNumDigits[4];
00232     serialNumDigits[0] = tempSerialNum / 1000 % 10;
00233     serialNumDigits[1] = tempSerialNum / 100 % 10;
00234     serialNumDigits[2] = tempSerialNum / 10 % 10;
00235     serialNumDigits[3] = tempSerialNum  % 10;
00236     
00237     serialNumberAndType[2] = serialNumDigits[0]+48;
00238     serialNumberAndType[3] = serialNumDigits[1]+48;
00239     serialNumberAndType[4] = serialNumDigits[2]+48;
00240     serialNumberAndType[5] = serialNumDigits[3]+48;
00241     
00242     uint8_t bleControlByte[1] = {0,};
00243     E2PROM.read(0x00072,bleControlByte,1);
00244     if(bleControlByte[0] == 0x01){
00245         bleControlByte[0] = 0x02; 
00246         ble.init();
00247         ble.onDisconnection(disconnectionCallback);
00248         ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device
00249         UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //Create a GattService that is defined in UPAS_Service.h
00250         upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
00251     
00252         /* setup advertising 
00253         Following lines do the follow:
00254             1:Declare the device as Bluetooth Smart(Low-Energy)
00255             2.Advertise the UPAS service that will send and receive the 113+ bits of settable values in the UPAS EEPROM
00256             3.Advertise the name that will be associated with the UPAS
00257             4.Allow the UPAS to advertise unrestricted (this might change) */
00258             
00259         ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00260         ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00261         ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)serialNumberAndType, sizeof(serialNumberAndType));
00262         ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00263         ble.setAdvertisingInterval(160); /* 160ms. */
00264         ble.startAdvertising();
00265     
00266         //**************//BLE initialization//**************//
00267        
00268        //Logic Loop waiting for responses from the BLE iPhone App.
00269        //Will not break loop without response from the app
00270         
00271         
00272         //Loop waits for response from app. When set to start, UPAS is turned off, but BLE is disabled once UPAS is turned on   
00273         while (1) {
00274             ble.waitForEvent();
00275              
00276             if(RunReady==10){ //Check to see if app is done with configurations         
00277                ble.stopAdvertising();
00278                E2PROM.write(0x00072,bleControlByte,1);
00279                NVIC_SystemReset();
00280                //pbKill = 0;
00281            }
00282            
00283         }
00284     }
00285     bleControlByte[0] = 0x01;
00286     E2PROM.write(0x00072,bleControlByte,1);
00287     
00288     E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
00289     RGB_LED.set_led(1,0,1);
00290     while(!RTC.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) {  // this while waits for the start time by looping until the start time
00291             wait(0.5);
00292             
00293             RTC.get_time(); 
00294 
00295     }
00296     
00297     
00298     RGB_LED.set_led(0,1,0);
00299     
00300     //Get the proper serial number
00301     E2PROM.read(0x00034, flowRateOriginal,2);    
00302     serial_num = ((uint16_t)flowRateOriginal[1] << 8) | flowRateOriginal[0];
00303     //----------------------------------------------
00304     calibrations.initialize(serial_num);
00305     pc.printf("%f\n",calibrations.MF4);
00306     //----------------------------------------------
00307     wait(1);
00308     blower=1;
00309     E2PROM.read(0x00014,logIntervalReadOut,1);
00310     logInerval = logIntervalReadOut[0];
00311      
00312     pc.printf("You're done, you can now disconect the USB cable.\r\n");
00313     RunReady = 0;
00314 
00315 
00316     //stop.attach(&check_stop, 30);    // check if we should shut down every 30 seconds, starting 60s after the start.
00317 
00318     //Use the flow rate value stored in eeprom
00319     E2PROM.read(0x00010,flowRateOriginal,4);
00320     E2PROM.byteToFloat(flowRateOriginal, &volflowSet);
00321     
00322     if(volflowSet<=1.0) {
00323         gainFlow = 100;
00324     } else if(volflowSet>=2.0) {
00325         gainFlow = 25;
00326     } else {
00327         gainFlow = 25;
00328     }
00329 
00330     RGB_LED.set_led(1,0,0);
00331     press = bmesensor.getPressure();
00332     temp = bmesensor.getTemperature();
00333     rh = bmesensor.getHumidity();
00334 
00335     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));
00336     massflowSet = volflowSet*atmoRho;
00337     //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
00338 
00339 
00340     digital_pot_setpoint = (int)floor(calibrations.DP4*pow(massflowSet,4)+calibrations.DP3*pow(massflowSet,3)+calibrations.DP2*pow(massflowSet,2)+calibrations.DP1*massflowSet+calibrations.DP0); //min = 0x7F, max = 0x00
00341 
00342     if(digital_pot_setpoint>=digitalpotMax) {
00343         digital_pot_setpoint = digitalpotMax;
00344     } else if(digital_pot_setpoint<=digitalpotMin) {
00345         digital_pot_setpoint = digitalpotMin;
00346     }
00347 
00348     pc.printf("%d\r\n", digitalpotMin);
00349     DigPot.writeRegister(digital_pot_setpoint);
00350     wait(1);
00351     blower = 1;
00352 
00353     
00354     E2PROM.read(0x00001, subjectLabelOriginal,8);        
00355     //sprintf(filename, "/sd/%c%c%c%c%c%c%c%cLOG_%02d-%02d-%02d_%02d=%02d=%02d.txt",subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7],RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds);
00356     sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%c%c%c%c%c%c%c%c.txt",serial_num,RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]);
00357     FILE *fp = fopen(filename, "w");
00358     fclose(fp);
00359     pc.printf("hello");
00360     //---------------------------------------------------------------------------------------------//
00361     //Following lines are needed to enter into the initiallization flow control loop
00362 
00363     wait(10);
00364 
00365     omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
00366     omronVolt = (omronReading*4.096)/(32768*2);
00367     if(omronVolt<=calibrations.omronVMin) {
00368         massflow = calibrations.omronMFMin;
00369     } else if(omronVolt>=calibrations.omronVMax) {
00370         massflow = calibrations.omronMFMax;
00371     } else {
00372         massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
00373     }
00374     deltaMflow = massflow-massflowSet;
00375     digital_pot_set = digital_pot_setpoint;
00376     wait(5);
00377 
00378     //---------------------------------------------------------------------------------------------//
00379     //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
00380 
00381     while(abs(deltaMflow)>.025) {
00382 
00383         omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
00384         omronVolt = (omronReading*4.096)/(32768*2);
00385         //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
00386         if(omronVolt<=calibrations.omronVMin) {
00387             massflow = calibrations.omronMFMin;
00388         } else if(omronVolt>=calibrations.omronVMax) {
00389             massflow = calibrations.omronMFMax;
00390         } else {
00391             massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
00392         }
00393 
00394         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));
00395         volflow = massflow/atmoRho;
00396         massflowSet = volflowSet*atmoRho;
00397         deltaMflow = massflow-massflowSet;
00398 
00399         digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
00400         if(digital_pot_set>=digitalpotMax) {
00401             digital_pot_set = digitalpotMax;
00402         } else if(digital_pot_set<=digitalpotMin) {
00403             digital_pot_set = digitalpotMin;
00404         }
00405 
00406         wait(2);
00407         DigPot.writeRegister(digital_pot_set);
00408         pc.printf("%d,\r\n", digital_pot_set);
00409         wait(1);
00410 
00411 
00412     }
00413 
00414     sampledVol = 0.0;
00415     RGB_LED.set_led(0,1,0);
00416     
00417   //  logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c
00418 
00419 
00420     while(1){
00421         if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) 
00422             pbKill = 0; // this is were we shut everything down
00423             
00424         wait(logInerval);
00425 //        if(r==0) r=1;
00426 //        else r=0;
00427 //        RGB_LED.set_led(r,g,b);
00428 //        wait(0.5);
00429         
00430         RTC.get_time();
00431         
00432         secondsD = RTC.seconds;
00433     
00434         while(fmod(secondsD,logInerval)!=0 || floor(secondsD)==floor(lastsecondD)) {
00435            //pc.printf("%f, %f\r\n", floor(secondsD), floor(lastsecondD)); 
00436             RTC.get_time();
00437             secondsD = RTC.seconds;
00438             wait_ms(100);
00439         }
00440         lastsecondD = secondsD;
00441             
00442         omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
00443         omronVolt = (omronReading*4.096)/(32768*2);
00444     
00445         if(omronVolt<=calibrations.omronVMin) {
00446             massflow = calibrations.omronMFMin;
00447         } else if(omronVolt>=calibrations.omronVMax) {
00448             massflow = calibrations.omronMFMax;
00449         } else {
00450             massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
00451         }
00452     
00453         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));
00454         volflow = massflow/atmoRho;
00455         sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
00456         deltaVflow = volflow-volflowSet;
00457         massflowSet = volflowSet*atmoRho;
00458         deltaMflow = massflow-massflowSet;
00459         if(abs(deltaMflow)>.025) {
00460             digital_pot_change = (int)(gainFlow*deltaMflow);
00461     
00462     
00463             if(abs(digital_pot_change)>=10) {
00464                 digital_pot_set = (int)(digital_pot_set+ (int)(1*deltaMflow));
00465                 RGB_LED.set_led(1,0,0);
00466             } else {
00467                 digital_pot_set = (digital_pot_set+ digital_pot_change);
00468                 RGB_LED.set_led(1,1,0);
00469             }
00470             
00471             if(digital_pot_set>=digitalpotMax) {
00472                  digital_pot_set = digitalpotMax;
00473                  RGB_LED.set_led(1,0,0);
00474             } else if(digital_pot_set<=digitalpotMin) {
00475                  digital_pot_set = digitalpotMin;
00476                  RGB_LED.set_led(1,0,0);
00477             }
00478     
00479             DigPot.writeRegister(digital_pot_set);
00480                 
00481         } else {
00482             RGB_LED.set_led(0,1,0);
00483         }
00484         
00485         
00486         movementsensor.getACCEL();
00487         movementsensor.getCOMPASS();
00488         compass = movementsensor.getCOMPASS_HEADING();
00489         accel_x = movementsensor.AccelData.x;
00490         accel_y = movementsensor.AccelData.y;
00491         accel_z = movementsensor.AccelData.z;
00492         accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
00493         mag_x = movementsensor.MagData.x;
00494         mag_y = movementsensor.MagData.y;
00495         mag_z = movementsensor.MagData.z;
00496         
00497         vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
00498         vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
00499         omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
00500         press = bmesensor.getPressure();
00501         temp = bmesensor.getTemperature()-5.0;
00502         rh = bmesensor.getHumidity();
00503         uv =  lightsensor.getUV();
00504         vis = lightsensor.getVIS();
00505         ir = lightsensor.getIR();
00506        
00507         
00508         FILE *fp = fopen(filename, "a");
00509         fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds);
00510         fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
00511         fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp);
00512         fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass);
00513         fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps());
00514         fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
00515         fclose(fp);
00516         //free(fp);
00517         
00518     }
00519     
00520 
00521 
00522 }
00523