#include "mbed.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
DigitalIn start_stop(p11); // start/stop button detection on pin 11
DigitalIn ctmode_toggle(p12); // capture/transit mode toggle button detection on pin 12
PwmOut c_led(p21); // capture LED on PWM pin 21
PwmOut t_led(p22); // transmit LED on PWM pin 22
Serial bt(p13, p14);
AnalogIn inputx(p20); // input pins 20,19,18 for x,y,z axis respectively.
AnalogIn inputy(p19);
AnalogIn inputz(p18);
Serial pc(USBTX, USBRX); //tx, rx
Timer t;
int mode; // capture or transmit
int session; // for indexing runs
int ctr; // counter for toggle
float ax;
float ay;
float az;
float gx;
float gy;
float gz;
int i;
int ts;
int main()
{
pc.printf("Ready to capture\r\n");
pc.baud(9600); //set bluetooth baud rate
mode = 1; // start with capture standby mode
session = 0;
c_led = 4.0f; // turn on capture led
t_led = 0.0f; // transmit led off
t.start();
t.reset();
ctr = 0;
while(1) {
if(start_stop) { // start/stop button press detected
if(mode == 1) { // capture standby mode
mode = 2; // capture mode
session++;
wait(0.5);
t.reset();
c_led = 4.0f; // on
t_led = 0.0f; // off
ctr = 0;
pc.printf("Session %d\r\n", session);
} else if(mode == 2) { // capture mode
mode = 1; // return to capture standby
c_led = 4.0f; // on
t_led = 0.0f; // off
wait(1);
} else if(mode == 3) { // transmit standby mode
mode = 4; // capture mode
t_led = 1.0f; // on
c_led = 0.0f; // off
ctr = 0;
pc.printf("Transmitting...\r\n");
wait(0.5);
} else if(mode == 4) { // transmit mode
mode = 3; // return to transmit standby
t_led = 1.0f; // on
c_led = 0.0f; // off
pc.printf("Ready to transmit\r\n");
wait(1);
}
}
else if(ctmode_toggle) { // mode toggle button press detected
if(mode == 1) { // capture standby -> transmit standby
pc.printf("EOF\r\n");
mode = 3; // return to transmit standby
c_led = 0.0f; // off
t_led = 1.0f; // on
session = 0;
pc.printf("Ready to transmit\r\n");
wait(1);
} else if(mode == 3) { // transmit standby -> capture standby
mode = 1; // return to capture standby
c_led = 4.0f; // on
t_led = 0.0f; // off
pc.printf("Ready to capture\r\n");
wait(1);
}
}
if(mode == 2) {
ctr++;
if (ctr%10 == 0) {
c_led = !c_led;
}
ts = t.read_ms();
ax = inputx.read();
ay = inputy.read();
az = inputz.read();
// calculate # of Gs
gx = 9.615*(ax-0.503);
gy = 8.696*(ay-0.491);
gz = 9.709*(az-0.503);
pc.printf("%d, %f, %f, %f\r\n", ts, gx, gy, gz);
} else if(mode == 4) {
ctr++;
if (ctr%10 == 0) {
t_led = !t_led;
}
pc.printf("Transmit code here\r\n");
mode = 3;
}
}
}
No comments:
Post a Comment