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.
Dependencies: mbed MMA8452Q SDSD
Diff: main.cpp
- Revision:
- 2:8e2c44115ce0
- Parent:
- 1:bee807884a79
- Child:
- 3:9e69b148d807
--- a/main.cpp Tue Nov 16 15:24:46 2021 +0000
+++ b/main.cpp Wed Nov 17 00:14:02 2021 +0000
@@ -1,10 +1,10 @@
-//Example Code for Accelerometer
-//Can be used as a starting point for Assignment #5
-//Stephen Licht
+//Assignment 5 Accelerometer and Servo with Data Logging
+//Starter code from Stephen Licht
//Created 11/9/2021
#include "mbed.h"
#include "MMA8452Q.h"
+#include "SDFileSystem.h"
//Define constants for timing of read and write:
#define READTIME 0.2
@@ -14,6 +14,7 @@
DigitalOut Led2(LED2);
DigitalOut Led3(LED3);
MMA8452Q accel(p28,p27,0x1D);
+SDFileSystem sd(p5, p6, p7, p8, "sd");
Serial pc(USBTX,USBRX);
Ticker accel_reading;
@@ -28,8 +29,12 @@
accZ_g = accel.readZ();
}
+Timer t;
+
int main()
{
+ float count = 1; //define count as starting at 1
+ t.start();
//Initialize accelerometer:
accel.init();
@@ -39,6 +44,7 @@
//Begin serial write timer for the first time:
serial_reporting.start();
+
while(1) {
//Use a timer to manage writing to the serial port (approximate timing):
if(serial_reporting.read()>WRITETIME) {
@@ -60,7 +66,25 @@
Led2=0;
Led3=1;
}
+
}
+
+ mkdir("/sd/mydir", 0777);
+ char file_name[100];
+ sprintf(file_name,"/sd/mydir/%ddd.txt","count");
+ FILE *fp;
+
+ fp = fopen(file_name,"w");
+ if(fp == NULL) {
+ error("Could not open file for write /n");
+ }
+
+ pc.printf("$ACC, Time Since Start %f [MS],X Acceleration %f [G],Y Acceleration %f [G],Z Acceleration %f [G] \r\n", t.read(), accX_g, accY_g, accZ_g);
+ fprintf(fp, "Time Since Start= %f \r\n", t.read());
+ fprintf(fp, "X Acceleration= %f[G], Y Acceleration= %f[G], Z Acceleration= %f[G] \r\n", accX_g, accY_g, accZ_g);
+ fclose(fp);
+
+ count=count+1;
}
}
