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:
Thu Apr 27 16:45:22 2017 +0000
Revision:
2:cf208b7bb73e
Parent:
1:08f10b3443e4
Final version of 'PV cells on exposure to cosmic radiation'

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 2:cf208b7bb73e 12 DigitalOut Alarm(p21);
utsal 2:cf208b7bb73e 13
utsal 0:00e98c624c5f 14 //UV Sensors are connected to p15 and p16
utsal 0:00e98c624c5f 15 AnalogIn UVSensor1(p15);
utsal 0:00e98c624c5f 16 AnalogIn UVSensor2(p16);
utsal 0:00e98c624c5f 17
utsal 2:cf208b7bb73e 18 //PV cells connected to p20
utsal 2:cf208b7bb73e 19 AnalogIn PV(p20);
utsal 2:cf208b7bb73e 20
utsal 0:00e98c624c5f 21 //Temperature sensor; connected to p28 and p27 (with UVSensor2)
utsal 0:00e98c624c5f 22 I2C tempsensor(p28, p27);
utsal 0:00e98c624c5f 23 const int addr = 0x90;
utsal 0:00e98c624c5f 24 char config_t[3];
utsal 0:00e98c624c5f 25 char temp_read[2];
utsal 0:00e98c624c5f 26 float temp;
utsal 0:00e98c624c5f 27
utsal 0:00e98c624c5f 28 //setting up the timer
utsal 0:00e98c624c5f 29 ExtendedTimer t;
utsal 0:00e98c624c5f 30
utsal 0:00e98c624c5f 31 int main(){
utsal 0:00e98c624c5f 32 t.start();//starts the timer
utsal 2:cf208b7bb73e 33 int alarmCount = 0;
utsal 0:00e98c624c5f 34 //Filesystem setting
utsal 0:00e98c624c5f 35 bool mountFailure = sd.mount();
utsal 0:00e98c624c5f 36 if (mountFailure !=0){
utsal 0:00e98c624c5f 37 pc.printf("Failed to mount the SD card.\r\n");
utsal 0:00e98c624c5f 38 return -1;
utsal 0:00e98c624c5f 39 }
utsal 0:00e98c624c5f 40
JLarkin 1:08f10b3443e4 41 FILE* fp = fopen("/drive/data.txt", "a"); //"a" is for append
utsal 0:00e98c624c5f 42 if (fp == NULL){
utsal 0:00e98c624c5f 43 pc.printf("Failed to open file.\r\n");
utsal 0:00e98c624c5f 44 sd.unmount();
utsal 0:00e98c624c5f 45 return -1;
utsal 0:00e98c624c5f 46 }
utsal 0:00e98c624c5f 47
utsal 0:00e98c624c5f 48 //Temperature Sensor settings
utsal 0:00e98c624c5f 49 config_t[0] = 0x01;
utsal 0:00e98c624c5f 50 config_t[1] = 0x60;
utsal 0:00e98c624c5f 51 config_t[2] = 0xA0;
utsal 0:00e98c624c5f 52 tempsensor.write(addr, config_t, 3);
utsal 0:00e98c624c5f 53 config_t[0] = 0x00;
utsal 0:00e98c624c5f 54 tempsensor.write(addr, config_t, 1);
utsal 0:00e98c624c5f 55
utsal 0:00e98c624c5f 56 //For Red Green Blue values
utsal 0:00e98c624c5f 57 uint16_t rgb1[3];
utsal 0:00e98c624c5f 58 uint16_t rgb2[3];
utsal 0:00e98c624c5f 59
utsal 2:cf208b7bb73e 60 pc.printf("Time\tTemp\tUV1\tUV2\tR1\tG1\tB1\tR2\tG2\tB2\tPV\r\n");//printing out titles
utsal 2:cf208b7bb73e 61 fprintf(fp, "Time\tTemp\tUV1\tUV2\tR1\tG1\tB1\tR2\tG2\tB2\tPV\r\n");
utsal 0:00e98c624c5f 62 while(1){
utsal 0:00e98c624c5f 63 if ((t.read_ms()%1000)==0){
utsal 0:00e98c624c5f 64 tempsensor.read(addr, temp_read, 2);
utsal 0:00e98c624c5f 65 temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4);
utsal 0:00e98c624c5f 66 if(temp>120){
utsal 0:00e98c624c5f 67 temp = temp-256+0.0625;
utsal 0:00e98c624c5f 68 }
utsal 0:00e98c624c5f 69 pc.printf("%d\t%.2f\t", t.read_ms()/1000, temp);
utsal 0:00e98c624c5f 70 fprintf(fp, "%d\t%.2f\t", t.read_ms()/1000, temp);
utsal 0:00e98c624c5f 71 pc.printf("%.4f\t%.4f", UVSensor1.read(), UVSensor2.read());
utsal 0:00e98c624c5f 72 fprintf(fp, "%.4f\t%.4f", UVSensor1.read(), UVSensor2.read());
utsal 0:00e98c624c5f 73 get_rgb1(rgb1);
utsal 0:00e98c624c5f 74 get_rgb2(rgb2);
utsal 2:cf208b7bb73e 75 pc.printf("\t%d\t%d\t%d\t%d\t%d\t%d\t%.4f\r\n", rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2], PV.read());
utsal 2:cf208b7bb73e 76 fprintf(fp, "\t%d\t%d\t%d\t%d\t%d\t%d\t%.4f\r\n", rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2], PV.read());
utsal 0:00e98c624c5f 77
utsal 2:cf208b7bb73e 78 if (alarmCount>36){
utsal 2:cf208b7bb73e 79 while(1)
utsal 2:cf208b7bb73e 80 {
utsal 2:cf208b7bb73e 81 Alarm = 1;
utsal 2:cf208b7bb73e 82 wait(0.1);
utsal 2:cf208b7bb73e 83 Alarm = 0;
utsal 2:cf208b7bb73e 84 wait(0.5);
utsal 2:cf208b7bb73e 85 }
utsal 2:cf208b7bb73e 86 }
utsal 2:cf208b7bb73e 87
utsal 2:cf208b7bb73e 88 int tempTime = t.read_ms()%300000;
utsal 2:cf208b7bb73e 89 if (tempTime < 10000){
utsal 2:cf208b7bb73e 90 alarmCount++;
utsal 0:00e98c624c5f 91 fclose(fp);
utsal 0:00e98c624c5f 92 FILE* fp = fopen("/drive/data.txt", "a");
utsal 0:00e98c624c5f 93 pc.printf("File reopen...\r\n");
utsal 0:00e98c624c5f 94 }
utsal 0:00e98c624c5f 95 }
utsal 0:00e98c624c5f 96 }
utsal 0:00e98c624c5f 97 fclose(fp);
utsal 0:00e98c624c5f 98 sd.unmount();
utsal 0:00e98c624c5f 99 }