Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- oldmanturtle
- Date:
- 2018-04-10
- Revision:
- 6:892ecb5fcfb9
- Parent:
- 5:603c549bfefa
- Child:
- 7:f7368fed0a2f
File content as of revision 6:892ecb5fcfb9:
#include "mbed.h" #include "ExtendedTimer.h" #include "SDFileSystem.h" AnalogIn lightSensor(p20); DigitalOut ledRed(p25); DigitalOut ledBlue(p26); DigitalOut ledError(LED3); DigitalOut powerOn(p16); DigitalOut sdMount(p15); //This is our timer Ticker countClock; ExtendedTimer timeClock; SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board Serial pc(USBTX,USBRX); float checkLightSensor(int n); //Switches the on states of the LEDs in the sphere void ledSwitch(); //This should save the data to the sd card *This isn't working right now* void save(); FILE *fp = NULL; int main() { powerOn = 1; timeClock.start(); sdMount = false; if (sd.mount() != 0) { pc.printf("Failed to mount the SD card.\r\n"); sdMount = true; return -1; // ends program with error status } fp = fopen("/sd/mydir/sdtest.txt", "a"); if(fp == NULL) { sdMount = true; ledError = true; error("Could not open file for write\n"); return -1; } int checkTimes = 60; ledRed = true; ledBlue = false; countClock.attach(&save, 10); fprintf(fp,"\r\n\r\n\r\n\r\nBlue Light, Time Blue Data Taken, Red Light, Time Red Data Taken\n\r\n\r\n\r"); while(true) { //Blue Light ledSwitch(); pc.printf("%.4f, ", checkLightSensor(checkTimes)); pc.printf("%.1f, ", timeClock.read()); fprintf(fp,"%.4f, ", checkLightSensor(checkTimes)); fprintf(fp,"%.1f, ", timeClock.read()); wait(1); //Red Light ledSwitch(); pc.printf("%.4f, ", checkLightSensor(checkTimes)); pc.printf("%.1f\r\n ", timeClock.read()); fprintf(fp,"%.4f, ", checkLightSensor(checkTimes)); fprintf(fp,"%.1f\r\n", timeClock.read()); wait(1); } } // Average n readings of the light sensor float checkLightSensor(int n){ float x; x = 0; for (int i = 0; i<n; i++) x = x + lightSensor; x = x/n; return x; } void ledSwitch(){ ledBlue = !ledBlue; ledRed = !ledRed; } void save(){ ledError = true; sdMount = true; fclose(fp); fp = fopen("/sd/mydir/sdtest.txt", "a"); if(fp == NULL) { error("Could not open file for write\n"); ledError = true; } pc.printf("\n\rSaved\n\r"); ledError = false; sdMount = false; }