Solar Cell Powered - Periodic logging of sensor data into SD card

Dependencies:   mbed

Committer:
gsundaresan3
Date:
Mon Feb 28 20:05:48 2011 +0000
Revision:
0:248aa51eeb12

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsundaresan3 0:248aa51eeb12 1 #include "string.h"
gsundaresan3 0:248aa51eeb12 2 #include "mbed.h"
gsundaresan3 0:248aa51eeb12 3 #include "SDFileSystem.h"
gsundaresan3 0:248aa51eeb12 4 #include "PowerControl.h"
gsundaresan3 0:248aa51eeb12 5 #include "EthernetPowerControl.h"
gsundaresan3 0:248aa51eeb12 6 AnalogIn ain(p20);
gsundaresan3 0:248aa51eeb12 7 DigitalOut sensor(p15);
gsundaresan3 0:248aa51eeb12 8 Serial pc(p28, p27);
gsundaresan3 0:248aa51eeb12 9 SDFileSystem sd(p5, p6, p7, p8, "sd");
gsundaresan3 0:248aa51eeb12 10 #define USR_POWERDOWN (0x104)
gsundaresan3 0:248aa51eeb12 11
gsundaresan3 0:248aa51eeb12 12 int semihost_powerdown() {
gsundaresan3 0:248aa51eeb12 13 uint32_t arg;
gsundaresan3 0:248aa51eeb12 14 return __semihost(USR_POWERDOWN, &arg);
gsundaresan3 0:248aa51eeb12 15 }
gsundaresan3 0:248aa51eeb12 16 Ticker sense; // Ticker to Interrupt the sleeping mode
gsundaresan3 0:248aa51eeb12 17 Timer t;
gsundaresan3 0:248aa51eeb12 18
gsundaresan3 0:248aa51eeb12 19 void sd_write_read() {
gsundaresan3 0:248aa51eeb12 20 char data[100];
gsundaresan3 0:248aa51eeb12 21 int time_stamp;
gsundaresan3 0:248aa51eeb12 22 time_stamp=t.read();
gsundaresan3 0:248aa51eeb12 23 sensor=1; /* Power up Sensor only when necessary */
gsundaresan3 0:248aa51eeb12 24 pc.printf("\n Writing into SD card... \n");
gsundaresan3 0:248aa51eeb12 25 pc.printf("\n Writing Sensor Data... \n");
gsundaresan3 0:248aa51eeb12 26 mkdir("/sd/mydir", 0777);
gsundaresan3 0:248aa51eeb12 27 /* Following code does a conditional check on analog in to determine the distance from IR Sensor */
gsundaresan3 0:248aa51eeb12 28 if (ain < 0.3) {
gsundaresan3 0:248aa51eeb12 29 strcpy(data,"Its approximately 30 cm away");
gsundaresan3 0:248aa51eeb12 30 } else if (ain > 0.3&&ain < 0.5) {
gsundaresan3 0:248aa51eeb12 31 strcpy(data,"Its approximately 20 cm away");
gsundaresan3 0:248aa51eeb12 32 } else if (ain>0.5 && ain < 0.6) {
gsundaresan3 0:248aa51eeb12 33 strcpy(data,"Its approximately 15 cm away");
gsundaresan3 0:248aa51eeb12 34 } else {
gsundaresan3 0:248aa51eeb12 35 strcpy(data,"Its approximately 10 cm away");
gsundaresan3 0:248aa51eeb12 36 }
gsundaresan3 0:248aa51eeb12 37
gsundaresan3 0:248aa51eeb12 38 /* Open the File for writing into the SD Card */
gsundaresan3 0:248aa51eeb12 39 FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
gsundaresan3 0:248aa51eeb12 40 if (fp == NULL) {
gsundaresan3 0:248aa51eeb12 41 error("Could not open file for write\n");
gsundaresan3 0:248aa51eeb12 42 }
gsundaresan3 0:248aa51eeb12 43 fprintf(fp, "%d : %s \n",time_stamp,data); /* Record Sensor data along with time stamp */
gsundaresan3 0:248aa51eeb12 44 fclose(fp);
gsundaresan3 0:248aa51eeb12 45 sensor=0; /* Power down Sensor after data is logged */
gsundaresan3 0:248aa51eeb12 46 }
gsundaresan3 0:248aa51eeb12 47
gsundaresan3 0:248aa51eeb12 48 int main() {
gsundaresan3 0:248aa51eeb12 49 int result;
gsundaresan3 0:248aa51eeb12 50 PHY_PowerDown(); /* PHY Powerdown */
gsundaresan3 0:248aa51eeb12 51 result = semihost_powerdown(); /* Semihost Powerdown */
gsundaresan3 0:248aa51eeb12 52 sense.attach(&sd_write_read, 5); /* Excecute sd_write_read function every 5s */
gsundaresan3 0:248aa51eeb12 53 t.start(); /* Start of timer to record Time Stamp */
gsundaresan3 0:248aa51eeb12 54 while (1) {
gsundaresan3 0:248aa51eeb12 55 Sleep(); /* Sleep mode to save power */
gsundaresan3 0:248aa51eeb12 56 }
gsundaresan3 0:248aa51eeb12 57 }
gsundaresan3 0:248aa51eeb12 58
gsundaresan3 0:248aa51eeb12 59