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:
- 4:aa82d3dbbe0f
- Parent:
- 3:9e69b148d807
- Child:
- 5:b286f58edcfa
diff -r 9e69b148d807 -r aa82d3dbbe0f main.cpp
--- a/main.cpp Sat Nov 20 04:15:09 2021 +0000
+++ b/main.cpp Sat Nov 20 04:45:50 2021 +0000
@@ -30,7 +30,7 @@
Timer serial_reporting;
Timer t;
-int count = 0; //define count as starting at 0
+int filecount = 0; //define count as starting at 0
int data=0; //dataCount
float pulseWidth=0; //pulse width of servo
@@ -47,17 +47,9 @@
void angle();
-void getAccelerometerValues()
-{
- accX_g = accel.readX();
- accY_g = accel.readY();
- accZ_g = accel.readZ();
-}
-
-
int main()
{
- t.start();
+ t.start(); //start overall timer
//Initialize accelerometer:
accel.init();
@@ -74,7 +66,6 @@
//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) {
@@ -82,8 +73,9 @@
serial_reporting.start();
pc.printf("X,Y,Z[Gs]:%5.2f,%5.2f,%5.2f\r\n",accX_g,accY_g,accZ_g);
}
- if(data>=50) {
- count = count+1;
+ //new file count every ten seconds or 50 data points
+ if(data>=50){
+ filecount=filecount+1;
data=0;
}
}
@@ -93,9 +85,8 @@
{
mkdir("/sd/mydir", 0777);
char file_name[100];
- sprintf(file_name,"/sd/mydir/log_file_%d.txt",count);
- FILE *fp;
- fp = fopen(file_name,"a");
+ sprintf(file_name,"/sd/mydir/log_file_%d.txt",filecount);
+ FILE *fp = fopen(file_name,"a");
if(fp == NULL) {
error("Could not open file for write /n");
}
@@ -103,8 +94,14 @@
fprintf(fp,"$ACC,%f,%f,%f,%f\r\n",t.read(),accX_g,accY_g,accZ_g);
fprintf(fp, "$SERV,%f,%f,%f\r\n",t.read(),(pulseWidth/1000000.000)*5*100,angle_deg);
fclose(fp);
+ data = data+1;
+}
- data = data+1
+void getAccelerometerValues()
+{
+ accX_g = accel.readX();
+ accY_g = accel.readY();
+ accZ_g = accel.readZ();
}
void light()
@@ -132,7 +129,6 @@
void angle()
{
angle_rad=atan((accZ_g)/(accY_g));
- //angle_rad=asin(accY_g);
angle_deg=angle_rad*180/(3.14159265359);
servo_set_angle(angle_deg);
}
@@ -141,6 +137,15 @@
{
float pulseCoeff = 10.0;
float pulseOffset = 400;
+
+ //Check to make sure commanded angle is within min-max bounds:
+ /*if (angle < 0) {
+ angle = 0;
+ } else if (angle > 180) {
+ angle = 180;
+ }*/
+
+ //pc.printf("Current angle: %f\r\n", angle);
pulseWidth =pulseCoeff*angle +pulseOffset;
servo.pulsewidth(pulseWidth/1000000.000);
@@ -148,6 +153,3 @@
-
-
-
