shared version

Dependencies:   ExtendedTimer ISL29125 SDFileSystem mbed

Link to the experiment wiki: http://www.whitworthnearspace.org/wiki/Dependence_of_photovoltaic_performance_on_light_spectrum

Committer:
utsal
Date:
Tue Apr 18 17:47:39 2017 +0000
Revision:
0:00e98c624c5f
Child:
1:08f10b3443e4
version to share

Who changed what in which revision?

UserRevisionLine numberNew contents of line
utsal 0:00e98c624c5f 1 #include "mbed.h"
utsal 0:00e98c624c5f 2 #include "ExtendedTimer.h"
utsal 0:00e98c624c5f 3 #include "SDFileSystem.h"
utsal 0:00e98c624c5f 4 #include "ISL29125.h"
utsal 0:00e98c624c5f 5 #include "RGB_Sensor.h"
utsal 0:00e98c624c5f 6
utsal 0:00e98c624c5f 7 Serial pc(USBTX, USBRX);
utsal 0:00e98c624c5f 8
utsal 0:00e98c624c5f 9 //setting up sd card
utsal 0:00e98c624c5f 10 SDFileSystem sd(p5, p6, p7, p8, "drive");
utsal 0:00e98c624c5f 11
utsal 0:00e98c624c5f 12 //UV Sensors are connected to p15 and p16
utsal 0:00e98c624c5f 13 AnalogIn UVSensor1(p15);
utsal 0:00e98c624c5f 14 AnalogIn UVSensor2(p16);
utsal 0:00e98c624c5f 15
utsal 0:00e98c624c5f 16 //Temperature sensor; connected to p28 and p27 (with UVSensor2)
utsal 0:00e98c624c5f 17 I2C tempsensor(p28, p27);
utsal 0:00e98c624c5f 18 const int addr = 0x90;
utsal 0:00e98c624c5f 19 char config_t[3];
utsal 0:00e98c624c5f 20 char temp_read[2];
utsal 0:00e98c624c5f 21 float temp;
utsal 0:00e98c624c5f 22
utsal 0:00e98c624c5f 23 //setting up the timer
utsal 0:00e98c624c5f 24 ExtendedTimer t;
utsal 0:00e98c624c5f 25
utsal 0:00e98c624c5f 26 int main(){
utsal 0:00e98c624c5f 27 t.start();//starts the timer
utsal 0:00e98c624c5f 28
utsal 0:00e98c624c5f 29 //Filesystem setting
utsal 0:00e98c624c5f 30 bool mountFailure = sd.mount();
utsal 0:00e98c624c5f 31 if (mountFailure !=0){
utsal 0:00e98c624c5f 32 pc.printf("Failed to mount the SD card.\r\n");
utsal 0:00e98c624c5f 33 return -1;
utsal 0:00e98c624c5f 34 }
utsal 0:00e98c624c5f 35
utsal 0:00e98c624c5f 36 FILE* fp = fopen("/drive/data.txt", "w"); //"a" is for append
utsal 0:00e98c624c5f 37 if (fp == NULL){
utsal 0:00e98c624c5f 38 pc.printf("Failed to open file.\r\n");
utsal 0:00e98c624c5f 39 sd.unmount();
utsal 0:00e98c624c5f 40 return -1;
utsal 0:00e98c624c5f 41 }
utsal 0:00e98c624c5f 42
utsal 0:00e98c624c5f 43 //Temperature Sensor settings
utsal 0:00e98c624c5f 44 config_t[0] = 0x01;
utsal 0:00e98c624c5f 45 config_t[1] = 0x60;
utsal 0:00e98c624c5f 46 config_t[2] = 0xA0;
utsal 0:00e98c624c5f 47 tempsensor.write(addr, config_t, 3);
utsal 0:00e98c624c5f 48 config_t[0] = 0x00;
utsal 0:00e98c624c5f 49 tempsensor.write(addr, config_t, 1);
utsal 0:00e98c624c5f 50
utsal 0:00e98c624c5f 51 //For Red Green Blue values
utsal 0:00e98c624c5f 52 uint16_t rgb1[3];
utsal 0:00e98c624c5f 53 uint16_t rgb2[3];
utsal 0:00e98c624c5f 54
utsal 0:00e98c624c5f 55 pc.printf("Time\tTemp\tUV1\tUV2\tR1\tG1\tB1\tR2\tG2\tB2\r\n");//printing out titles
utsal 0:00e98c624c5f 56 fprintf(fp, "Time\tTemp\tUV1\tUV2\tR1\tG1\tB1\tR2\tG2\tB2\r\n");
utsal 0:00e98c624c5f 57 while(1){
utsal 0:00e98c624c5f 58 if ((t.read_ms()%1000)==0){
utsal 0:00e98c624c5f 59 tempsensor.read(addr, temp_read, 2);
utsal 0:00e98c624c5f 60 temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4);
utsal 0:00e98c624c5f 61 if(temp>120){
utsal 0:00e98c624c5f 62 temp = temp-256+0.0625;
utsal 0:00e98c624c5f 63 }
utsal 0:00e98c624c5f 64 pc.printf("%d\t%.2f\t", t.read_ms()/1000, temp);
utsal 0:00e98c624c5f 65 fprintf(fp, "%d\t%.2f\t", t.read_ms()/1000, temp);
utsal 0:00e98c624c5f 66 pc.printf("%.4f\t%.4f", UVSensor1.read(), UVSensor2.read());
utsal 0:00e98c624c5f 67 fprintf(fp, "%.4f\t%.4f", UVSensor1.read(), UVSensor2.read());
utsal 0:00e98c624c5f 68 get_rgb1(rgb1);
utsal 0:00e98c624c5f 69 get_rgb2(rgb2);
utsal 0:00e98c624c5f 70 pc.printf("\t%d\t%d\t%d\t%d\t%d\t%d\r\n", rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2]);
utsal 0:00e98c624c5f 71 fprintf(fp, "\t%d\t%d\t%d\t%d\t%d\t%d\r\n", rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2]);
utsal 0:00e98c624c5f 72
utsal 0:00e98c624c5f 73 if ((t.read_ms()%300000)==0){
utsal 0:00e98c624c5f 74 fclose(fp);
utsal 0:00e98c624c5f 75 FILE* fp = fopen("/drive/data.txt", "a");
utsal 0:00e98c624c5f 76 pc.printf("File reopen...\r\n");
utsal 0:00e98c624c5f 77 }
utsal 0:00e98c624c5f 78 }
utsal 0:00e98c624c5f 79 }
utsal 0:00e98c624c5f 80 fclose(fp);
utsal 0:00e98c624c5f 81 sd.unmount();
utsal 0:00e98c624c5f 82 }