attempt to fix posible power issues with the sharp

Dependencies:   ADS1115 BME280 CronoDot SDFileSystem mbed

Fork of Outdoor_UPAS_v1_2_Tboard by scott kelleher

Committer:
scottkelleher
Date:
Tue May 24 23:32:34 2016 +0000
Revision:
60:e434c8c85020
Parent:
59:4221935a12d8
different version to attempt to solve sharp issues

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jelord 0:2cb2b2ea316f 1 #include "mbed.h"
jelord 2:88fcbfadec6a 2 #include "SDFileSystem.h"
jelord 2:88fcbfadec6a 3 #include "Adafruit_ADS1015.h"
jelord 2:88fcbfadec6a 4 #include "BME280.h"
jelord 2:88fcbfadec6a 5 #include "CronoDot.h"
scottkelleher 57:1695e252298d 6
caseyquinn 14:7cdb643da356 7
caseyquinn 56:49387b72460e 8 //Edit for fork
caseyquinn 12:5b4f3245606a 9 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 10 //General Items
caseyquinn 12:5b4f3245606a 11 /////////////////////////////////////////////
caseyquinn 14:7cdb643da356 12 I2C i2c(PB_9, PB_8);//(D14, D15); SDA,SCL
caseyquinn 12:5b4f3245606a 13 Serial pc(USBTX, USBRX);
caseyquinn 12:5b4f3245606a 14 BME280 bmesensor(PB_9, PB_8);//(D14, D15);
caseyquinn 17:3e6dda6e6335 15 CronoDot RTC_UPAS(PB_9, PB_8);//(D14, D15);
caseyquinn 17:3e6dda6e6335 16
caseyquinn 13:455601f62aad 17
caseyquinn 12:5b4f3245606a 18 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 19 //Analog to Digital Converter
caseyquinn 12:5b4f3245606a 20 /////////////////////////////////////////////
scottkelleher 59:4221935a12d8 21 Adafruit_ADS1115 ads(&i2c);
scottkelleher 59:4221935a12d8 22 Adafruit_ADS1115 ads2(&i2c);
caseyquinn 12:5b4f3245606a 23 //DigitalIn ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf
caseyquinn 12:5b4f3245606a 24
caseyquinn 12:5b4f3245606a 25
caseyquinn 23:1ca41779b8ec 26
caseyquinn 12:5b4f3245606a 27 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 28 //SD Card
caseyquinn 12:5b4f3245606a 29 /////////////////////////////////////////////
scottkelleher 57:1695e252298d 30 char filename[] = "/sd/SHARP_LOG00.txt";
scottkelleher 57:1695e252298d 31 SDFileSystem sd(D11, D12, D13, D10, "sd"); // (MOSI, MISO, SCK, SEL)
scottkelleher 57:1695e252298d 32 //DigitalIn sdCD(PA_11, PullUp);
caseyquinn 52:95949e34b1f6 33
caseyquinn 14:7cdb643da356 34
caseyquinn 12:5b4f3245606a 35 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 36 //Callbacks
caseyquinn 12:5b4f3245606a 37 /////////////////////////////////////////////
caseyquinn 8:c4a8f9b67cee 38 Ticker logg; //This is the logging callback object
scottkelleher 57:1695e252298d 39
jelord 3:122bfc998c4c 40
caseyquinn 12:5b4f3245606a 41 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 42 //Varible Definitions
caseyquinn 12:5b4f3245606a 43 /////////////////////////////////////////////
jelord 3:122bfc998c4c 44 uint16_t serial_num = 1; // Default serial/calibration number
jelord 3:122bfc998c4c 45 int RunReady =0;
jelord 3:122bfc998c4c 46
caseyquinn 25:fbf7d44e7da4 47 bool ledOn = 0;
caseyquinn 25:fbf7d44e7da4 48
caseyquinn 4:5d004fd997d5 49 struct tm STtime;
caseyquinn 7:29b01d5812ee 50 char timestr[32];
caseyquinn 43:05aa1fb68946 51 char yrstr[4];
caseyquinn 43:05aa1fb68946 52 char mostr[4];
caseyquinn 43:05aa1fb68946 53 char daystr[4];
caseyquinn 43:05aa1fb68946 54 char hrstr[4];
caseyquinn 43:05aa1fb68946 55 char minstr[4];
caseyquinn 43:05aa1fb68946 56 char secstr[4];
caseyquinn 43:05aa1fb68946 57
caseyquinn 43:05aa1fb68946 58 int stYr, stMo, stDay, stHr, stMin, stSec;
jelord 3:122bfc998c4c 59
caseyquinn 36:2e344db70d35 60
caseyquinn 9:8646fd501832 61 float press;
caseyquinn 9:8646fd501832 62 float temp;
caseyquinn 9:8646fd501832 63 float rh;
scottkelleher 57:1695e252298d 64 float atmoRho; //g/L
scottkelleher 59:4221935a12d8 65 int logInerval = 2;//seconds
jelord 3:122bfc998c4c 66
scottkelleher 57:1695e252298d 67 DigitalOut sharp1LED(D7, 1);
scottkelleher 57:1695e252298d 68 DigitalOut sharp2LED(D6, 1);
scottkelleher 57:1695e252298d 69 DigitalOut sharp3LED(D5, 1);
scottkelleher 57:1695e252298d 70 DigitalOut sharp4LED(D4, 1);
scottkelleher 59:4221935a12d8 71 DigitalOut sharp5LED(D3, 1);
scottkelleher 59:4221935a12d8 72 DigitalOut sharp6LED(D2, 1);
jelord 3:122bfc998c4c 73
scottkelleher 57:1695e252298d 74 int samplingTime = 280;
scottkelleher 57:1695e252298d 75 int deltaTime = 40;
scottkelleher 60:e434c8c85020 76 int sleepTime= 9680;
scottkelleher 59:4221935a12d8 77 int sharp1, sharp2, sharp3, sharp4, sharp5, sharp6;
scottkelleher 59:4221935a12d8 78 float sharpVolt1, sharpVolt2, sharpVolt3, sharpVolt4, sharpVolt5, sharpVolt6; //V
caseyquinn 12:5b4f3245606a 79
caseyquinn 11:aa21628a9b15 80
caseyquinn 11:aa21628a9b15 81
caseyquinn 5:c3252e5d45ca 82
caseyquinn 9:8646fd501832 83 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 84 //SD Logging Function
caseyquinn 9:8646fd501832 85 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 86 void log_data()
caseyquinn 52:95949e34b1f6 87 {
scottkelleher 57:1695e252298d 88 //Get RTC time(s)
scottkelleher 57:1695e252298d 89 ///////////////////////////
scottkelleher 57:1695e252298d 90 RTC_UPAS.get_time();
scottkelleher 57:1695e252298d 91 time_t seconds = time(NULL);
scottkelleher 57:1695e252298d 92 strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
scottkelleher 57:1695e252298d 93 /*
caseyquinn 43:05aa1fb68946 94 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 43:05aa1fb68946 95 stYr = atoi(yrstr);
caseyquinn 43:05aa1fb68946 96
caseyquinn 43:05aa1fb68946 97 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 43:05aa1fb68946 98 stMo = atoi(mostr);
caseyquinn 43:05aa1fb68946 99
caseyquinn 43:05aa1fb68946 100 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 43:05aa1fb68946 101 stDay = atoi(daystr);
caseyquinn 43:05aa1fb68946 102
caseyquinn 43:05aa1fb68946 103 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 43:05aa1fb68946 104 stHr = atoi(hrstr);
caseyquinn 43:05aa1fb68946 105
caseyquinn 43:05aa1fb68946 106 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 43:05aa1fb68946 107 stMin = atoi(minstr);
caseyquinn 43:05aa1fb68946 108
caseyquinn 43:05aa1fb68946 109 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 43:05aa1fb68946 110 stSec = atoi(secstr);
scottkelleher 57:1695e252298d 111 */
caseyquinn 44:075fa63d5df1 112 //pc.printf("%s,%s,%d,%s,%d,%s,%d,%s,%d,%s,%d,%s,%d\r\n", timestr,yrstr,stYr,mostr,stMo,daystr,stDay,hrstr,stHr,minstr,stMin,secstr,stSec);
caseyquinn 36:2e344db70d35 113
caseyquinn 46:99d129bfdbbd 114
caseyquinn 28:42932d3b105d 115 //Get Sensor Data except GPS
caseyquinn 28:42932d3b105d 116 ////////////////////////////
caseyquinn 7:29b01d5812ee 117 press = bmesensor.getPressure();
scottkelleher 58:3233c10a668c 118 temp = bmesensor.getTemperature();
caseyquinn 7:29b01d5812ee 119 rh = bmesensor.getHumidity();
scottkelleher 57:1695e252298d 120 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 35:e0bdd6389a75 121
scottkelleher 59:4221935a12d8 122 //pc.printf("%f,%f,%f,%f\r\n", temp, press, rh, atmoRho);
scottkelleher 59:4221935a12d8 123
scottkelleher 57:1695e252298d 124 sharp1LED = 0;
scottkelleher 59:4221935a12d8 125 wait_us(samplingTime);
scottkelleher 57:1695e252298d 126 sharp1 = ads.readADC_SingleEnded(0, 0xC383); // read channel 1
scottkelleher 57:1695e252298d 127 sharpVolt1 = (sharp1*4.096)/(32768*1);
scottkelleher 60:e434c8c85020 128 wait_us(deltaTime);
scottkelleher 60:e434c8c85020 129 sharp1LED = 1;
scottkelleher 60:e434c8c85020 130 wait_us(sleepTime);
scottkelleher 60:e434c8c85020 131
scottkelleher 60:e434c8c85020 132 sharp2LED = 0;
scottkelleher 60:e434c8c85020 133 wait_us(samplingTime);
scottkelleher 57:1695e252298d 134 sharp2 = ads.readADC_SingleEnded(1, 0xD383); // read channel 1
scottkelleher 57:1695e252298d 135 sharpVolt2 = (sharp2*4.096)/(32768*1);
scottkelleher 60:e434c8c85020 136 wait_us(deltaTime);
scottkelleher 60:e434c8c85020 137 sharp2LED = 1;
scottkelleher 60:e434c8c85020 138 wait_us(sleepTime);
scottkelleher 60:e434c8c85020 139
scottkelleher 60:e434c8c85020 140 sharp3LED = 0;
scottkelleher 60:e434c8c85020 141 wait_us(samplingTime);
scottkelleher 57:1695e252298d 142 sharp3 = ads.readADC_SingleEnded(2, 0xE383); // read channel 1
scottkelleher 57:1695e252298d 143 sharpVolt3 = (sharp3*4.096)/(32768*1);
scottkelleher 60:e434c8c85020 144 wait_us(deltaTime);
scottkelleher 60:e434c8c85020 145 sharp3LED = 1;
scottkelleher 60:e434c8c85020 146 wait_us(sleepTime);
scottkelleher 60:e434c8c85020 147
scottkelleher 60:e434c8c85020 148 sharp4LED = 0;
scottkelleher 60:e434c8c85020 149 wait_us(samplingTime);
scottkelleher 57:1695e252298d 150 sharp4 = ads.readADC_SingleEnded(3, 0xF383); // read channel 1
scottkelleher 57:1695e252298d 151 sharpVolt4 = (sharp4*4.096)/(32768*1);
scottkelleher 60:e434c8c85020 152 wait_us(deltaTime);
scottkelleher 60:e434c8c85020 153 sharp4LED = 1;
scottkelleher 60:e434c8c85020 154 wait_us(sleepTime);
caseyquinn 50:5f1b378e4f64 155
scottkelleher 60:e434c8c85020 156 sharp5LED = 0;
scottkelleher 60:e434c8c85020 157 wait_us(samplingTime);
scottkelleher 59:4221935a12d8 158 sharp5 = ads2.readADC_SingleEnded(0, 0xC383); // read channel 1
scottkelleher 60:e434c8c85020 159 sharpVolt5 = (sharp1*4.096)/(32768*1);
scottkelleher 60:e434c8c85020 160 sharp5LED = 1;
scottkelleher 60:e434c8c85020 161 wait_us(sleepTime);
caseyquinn 50:5f1b378e4f64 162
scottkelleher 60:e434c8c85020 163 sharp6LED = 0;
scottkelleher 60:e434c8c85020 164 wait_us(samplingTime);
scottkelleher 60:e434c8c85020 165 sharp6 = ads2.readADC_SingleEnded(0, 0xD383); // read channel 1
scottkelleher 60:e434c8c85020 166 sharpVolt6 = (sharp1*4.096)/(32768*1);
scottkelleher 59:4221935a12d8 167 sharp6LED = 1;
scottkelleher 60:e434c8c85020 168 wait_us(sleepTime);
caseyquinn 14:7cdb643da356 169
scottkelleher 60:e434c8c85020 170
scottkelleher 60:e434c8c85020 171
scottkelleher 60:e434c8c85020 172
scottkelleher 60:e434c8c85020 173
scottkelleher 60:e434c8c85020 174
scottkelleher 60:e434c8c85020 175
scottkelleher 60:e434c8c85020 176 //pc.printf("%d,%f,%d,%f,\r\n" ,sharp5,sharpVolt5, sharp6, sharpVolt6);
scottkelleher 60:e434c8c85020 177 pc.printf("%d,%d\r\n",sharp1, sharp4);
scottkelleher 59:4221935a12d8 178 //pc.printf("%d,%f,%d,%f,%d,%f,%d,%f\r\n" ,sharp1,sharpVolt1, sharp2, sharpVolt2,sharp3,sharpVolt3, sharp4, sharpVolt4);
scottkelleher 59:4221935a12d8 179 //pc.printf("%2.2f,%4.2f,%2.1f,%1.3f\r\n", temp,press,rh,atmoRho);
scottkelleher 59:4221935a12d8 180 //pc.printf("%d,%f,%d,%f\r\n" ,sharp3,sharpVolt3, sharp4, sharpVolt4);
caseyquinn 27:922f53fa649c 181
scottkelleher 60:e434c8c85020 182 FILE *fp = fopen(filename, "scotttrial");
scottkelleher 60:e434c8c85020 183 fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC_UPAS.year, RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds);
scottkelleher 60:e434c8c85020 184 fprintf(fp, "%s,", timestr);
scottkelleher 59:4221935a12d8 185 fprintf(fp, "%2.2f,%4.2f,%2.1f,%1.3f", temp,press,rh,atmoRho);
scottkelleher 57:1695e252298d 186 fprintf(fp, "%d,%f,%d,%f," ,sharp1,sharpVolt1, sharp2, sharpVolt2);
scottkelleher 59:4221935a12d8 187 fprintf(fp, "%d,%f,%d,%f" ,sharp3,sharpVolt3, sharp4, sharpVolt4);
scottkelleher 60:e434c8c85020 188 fprintf(fp, "%d,%f,%d,%f,\r\n" ,sharp5,sharpVolt5, sharp6, sharpVolt6);
scottkelleher 60:e434c8c85020 189 fclose(fp);
caseyquinn 7:29b01d5812ee 190 free(fp);
caseyquinn 25:fbf7d44e7da4 191
caseyquinn 25:fbf7d44e7da4 192
caseyquinn 14:7cdb643da356 193
caseyquinn 12:5b4f3245606a 194
caseyquinn 25:fbf7d44e7da4 195
caseyquinn 12:5b4f3245606a 196
jelord 3:122bfc998c4c 197 }
caseyquinn 8:c4a8f9b67cee 198
caseyquinn 25:fbf7d44e7da4 199
caseyquinn 9:8646fd501832 200 //////////////////////////////////////////////////////////////
caseyquinn 9:8646fd501832 201 //Main Function
caseyquinn 9:8646fd501832 202 //////////////////////////////////////////////////////////////
jelord 1:9fbb5b665068 203 int main(){
caseyquinn 27:922f53fa649c 204
caseyquinn 29:fd74725294d5 205
scottkelleher 57:1695e252298d 206
caseyquinn 10:06fbb1c9e3bd 207 pc.baud(115200); // set what you want here depending on your terminal program speed
caseyquinn 10:06fbb1c9e3bd 208 pc.printf("\f\n\r-------------Startup-------------\n\r");
jelord 1:9fbb5b665068 209 wait(0.5);
caseyquinn 5:c3252e5d45ca 210
caseyquinn 36:2e344db70d35 211
scottkelleher 57:1695e252298d 212 RTC_UPAS.set_time(0,0,0,1,1,1,16);//sets chronodot RTC
caseyquinn 29:fd74725294d5 213
scottkelleher 57:1695e252298d 214 ///////////////////////
scottkelleher 57:1695e252298d 215 //sets ST RTC
scottkelleher 57:1695e252298d 216 //////////////////////
scottkelleher 57:1695e252298d 217 STtime.tm_sec = 0; // 0-59
scottkelleher 57:1695e252298d 218 STtime.tm_min = 0; // 0-59
scottkelleher 57:1695e252298d 219 STtime.tm_hour = 0; // 0-23
scottkelleher 57:1695e252298d 220 STtime.tm_mday = 1; // 1-31
scottkelleher 57:1695e252298d 221 STtime.tm_mon = 0; // 0-11
scottkelleher 57:1695e252298d 222 STtime.tm_year = 116; // year since 1900 (116 = 2016)
scottkelleher 57:1695e252298d 223 time_t STseconds = mktime(&STtime);
scottkelleher 57:1695e252298d 224 set_time(STseconds); // Set RTC time
caseyquinn 29:fd74725294d5 225
scottkelleher 57:1695e252298d 226
caseyquinn 52:95949e34b1f6 227 RTC_UPAS.get_time();
caseyquinn 46:99d129bfdbbd 228
caseyquinn 52:95949e34b1f6 229
caseyquinn 43:05aa1fb68946 230 time_t seconds = time(NULL);
caseyquinn 43:05aa1fb68946 231 strftime(timestr, 32, "%y-%m-%d-%H=%M=%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 232
caseyquinn 52:95949e34b1f6 233 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 52:95949e34b1f6 234 stYr = atoi(yrstr);
caseyquinn 52:95949e34b1f6 235
caseyquinn 52:95949e34b1f6 236 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 52:95949e34b1f6 237 stMo = atoi(mostr);
caseyquinn 52:95949e34b1f6 238
caseyquinn 52:95949e34b1f6 239 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 52:95949e34b1f6 240 stDay = atoi(daystr);
caseyquinn 52:95949e34b1f6 241
caseyquinn 52:95949e34b1f6 242 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 52:95949e34b1f6 243 stHr = atoi(hrstr);
caseyquinn 52:95949e34b1f6 244
caseyquinn 52:95949e34b1f6 245 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 52:95949e34b1f6 246 stMin = atoi(minstr);
caseyquinn 52:95949e34b1f6 247
caseyquinn 52:95949e34b1f6 248 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 249 stSec = atoi(secstr);
caseyquinn 52:95949e34b1f6 250
caseyquinn 52:95949e34b1f6 251
caseyquinn 52:95949e34b1f6 252
caseyquinn 52:95949e34b1f6 253
scottkelleher 57:1695e252298d 254 // sprintf(filename,"/sd/SHARP_LOG00.txt");
scottkelleher 57:1695e252298d 255
scottkelleher 57:1695e252298d 256 for (uint8_t i = 0; i < 100; i++) {
scottkelleher 57:1695e252298d 257 filename[13] = i/10 + '0';
scottkelleher 57:1695e252298d 258 filename[14] = i%10 + '0';
scottkelleher 57:1695e252298d 259 FILE *fp = fopen(filename, "r");
scottkelleher 57:1695e252298d 260 if (fp == NULL) {
scottkelleher 57:1695e252298d 261 // only open a new file if it doesn't exist
scottkelleher 57:1695e252298d 262 FILE *fp = fopen(filename, "w");
scottkelleher 57:1695e252298d 263 fclose(fp);
scottkelleher 57:1695e252298d 264 break; // leave the loop!
scottkelleher 57:1695e252298d 265 }
scottkelleher 57:1695e252298d 266 }
caseyquinn 36:2e344db70d35 267
caseyquinn 29:fd74725294d5 268
caseyquinn 14:7cdb643da356 269
caseyquinn 46:99d129bfdbbd 270 while(fmod((double)stSec,10)!=0) {
caseyquinn 46:99d129bfdbbd 271 //pc.printf("%f, %f\r\n", floor(secondsD), floor(lastsecondD));
caseyquinn 46:99d129bfdbbd 272 time_t seconds = time(NULL);
caseyquinn 46:99d129bfdbbd 273 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 46:99d129bfdbbd 274 stSec = atoi(secstr);
caseyquinn 46:99d129bfdbbd 275 wait_ms(100);
caseyquinn 46:99d129bfdbbd 276 }
caseyquinn 46:99d129bfdbbd 277
scottkelleher 59:4221935a12d8 278 logg.attach(&log_data, 3);
scottkelleher 57:1695e252298d 279
caseyquinn 35:e0bdd6389a75 280
caseyquinn 35:e0bdd6389a75 281
caseyquinn 30:aa6324845a84 282
jelord 3:122bfc998c4c 283 //** end of initalization **//
jelord 3:122bfc998c4c 284 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 285 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 286 // Main Control Loop
jelord 3:122bfc998c4c 287
caseyquinn 7:29b01d5812ee 288 while (1) {
caseyquinn 7:29b01d5812ee 289 // Do other things...
caseyquinn 46:99d129bfdbbd 290
caseyquinn 46:99d129bfdbbd 291 }
caseyquinn 7:29b01d5812ee 292
caseyquinn 8:c4a8f9b67cee 293
caseyquinn 4:5d004fd997d5 294
jelord 3:122bfc998c4c 295
jelord 0:2cb2b2ea316f 296 }