mbed Starter Kit / mbed Starter Kit Demo Programs
Committer:
ShawnHymel
Date:
Mon Jul 28 20:29:28 2014 +0000
Revision:
0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShawnHymel 0:6a73d3dc037e 1 // Temperature logging demo - record temperatures to SD card and print them to
ShawnHymel 0:6a73d3dc037e 2 // the console every 10 seconds
ShawnHymel 0:6a73d3dc037e 3
ShawnHymel 0:6a73d3dc037e 4 #include "mbed.h"
ShawnHymel 0:6a73d3dc037e 5 #include "SDFileSystem.h"
ShawnHymel 0:6a73d3dc037e 6
ShawnHymel 0:6a73d3dc037e 7 // Analog input (pin 15)
ShawnHymel 0:6a73d3dc037e 8 AnalogIn ain(p15);
ShawnHymel 0:6a73d3dc037e 9
ShawnHymel 0:6a73d3dc037e 10 // USB serial (tx, rx)
ShawnHymel 0:6a73d3dc037e 11 Serial pc(USBTX, USBRX);
ShawnHymel 0:6a73d3dc037e 12
ShawnHymel 0:6a73d3dc037e 13 // SD card (SPI pins)
ShawnHymel 0:6a73d3dc037e 14 SDFileSystem sd(p5, p6, p7, p8, "sd");
ShawnHymel 0:6a73d3dc037e 15
ShawnHymel 0:6a73d3dc037e 16 // Timer for our timestamps
ShawnHymel 0:6a73d3dc037e 17 Timer timer;
ShawnHymel 0:6a73d3dc037e 18
ShawnHymel 0:6a73d3dc037e 19 int main() {
ShawnHymel 0:6a73d3dc037e 20
ShawnHymel 0:6a73d3dc037e 21 FILE *file;
ShawnHymel 0:6a73d3dc037e 22 float voltage_in;
ShawnHymel 0:6a73d3dc037e 23 float degrees_c;
ShawnHymel 0:6a73d3dc037e 24 int i;
ShawnHymel 0:6a73d3dc037e 25 int c;
ShawnHymel 0:6a73d3dc037e 26
ShawnHymel 0:6a73d3dc037e 27 // Start our timer
ShawnHymel 0:6a73d3dc037e 28 timer.start();
ShawnHymel 0:6a73d3dc037e 29
ShawnHymel 0:6a73d3dc037e 30 // Open file for writing
ShawnHymel 0:6a73d3dc037e 31 file = fopen("/sd/temp_data.txt", "w");
ShawnHymel 0:6a73d3dc037e 32 if ( file == NULL ) {
ShawnHymel 0:6a73d3dc037e 33 error("ERROR: Could not open file for writing!\n");
ShawnHymel 0:6a73d3dc037e 34 return -1;
ShawnHymel 0:6a73d3dc037e 35 }
ShawnHymel 0:6a73d3dc037e 36
ShawnHymel 0:6a73d3dc037e 37 // Tell the user we need to wait while we collect some data
ShawnHymel 0:6a73d3dc037e 38 pc.printf("\nCollecting data (Do not remove SD Card!) ...\n");
ShawnHymel 0:6a73d3dc037e 39
ShawnHymel 0:6a73d3dc037e 40 // Collect temperatures with timestamps every second
ShawnHymel 0:6a73d3dc037e 41 for(i = 0; i < 10; i++) {
ShawnHymel 0:6a73d3dc037e 42 voltage_in = ain * 3.3;
ShawnHymel 0:6a73d3dc037e 43 degrees_c = (voltage_in - 0.5) * 100.0;
ShawnHymel 0:6a73d3dc037e 44 fprintf(file, "%2.2fs: %3.1f deg C\n", timer.read(), degrees_c);
ShawnHymel 0:6a73d3dc037e 45 wait(1);
ShawnHymel 0:6a73d3dc037e 46 }
ShawnHymel 0:6a73d3dc037e 47
ShawnHymel 0:6a73d3dc037e 48 // Close file and re-open it for reading
ShawnHymel 0:6a73d3dc037e 49 fclose(file);
ShawnHymel 0:6a73d3dc037e 50 file = fopen("/sd/temp_data.txt", "r");
ShawnHymel 0:6a73d3dc037e 51 if ( file == NULL ) {
ShawnHymel 0:6a73d3dc037e 52 error("ERROR: Could not open file for reading!\n");
ShawnHymel 0:6a73d3dc037e 53 return -1;
ShawnHymel 0:6a73d3dc037e 54 }
ShawnHymel 0:6a73d3dc037e 55
ShawnHymel 0:6a73d3dc037e 56 // Print results to console
ShawnHymel 0:6a73d3dc037e 57 printf("Temperature data:\n");
ShawnHymel 0:6a73d3dc037e 58 while(1) {
ShawnHymel 0:6a73d3dc037e 59 c = fgetc(file);
ShawnHymel 0:6a73d3dc037e 60 if ( c == EOF ) {
ShawnHymel 0:6a73d3dc037e 61 break;
ShawnHymel 0:6a73d3dc037e 62 }
ShawnHymel 0:6a73d3dc037e 63 pc.putc(c);
ShawnHymel 0:6a73d3dc037e 64 }
ShawnHymel 0:6a73d3dc037e 65
ShawnHymel 0:6a73d3dc037e 66 // Close the file and finish
ShawnHymel 0:6a73d3dc037e 67 fclose(file);
ShawnHymel 0:6a73d3dc037e 68 pc.printf("Done!\n");
ShawnHymel 0:6a73d3dc037e 69
ShawnHymel 0:6a73d3dc037e 70 return 0;
ShawnHymel 0:6a73d3dc037e 71 }
ShawnHymel 0:6a73d3dc037e 72