Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main4.h Source File

main4.h

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "Counter.h"
00004 #include "LIS302.h"
00005 #include "Servo.h"
00006 
00007 Servo myservo(p21);
00008 LIS302 acc (p5,p6,p7,p8);
00009 DigitalIn enable(p20);
00010 DigitalOut LIS302(LIS302);
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalOut led4(LED4);
00015 #define TICK_PERIOD 1.0
00016 LocalFileSystem local("local");
00017 Counter counter(p18);
00018 
00019 
00020 Ticker tick;
00021 
00022 Timer t;
00023 volatile int tick_active = 0;
00024 
00025 void dotick (void) {
00026     tick_active = 1;
00027     led1  = !led1;
00028 }
00029 int rpm_counter=0;
00030 int main() {
00031 
00032     float samples [5] = {0};
00033     int index = 0;
00034     int flipped_right = 1;
00035     int flipped_left = 1;
00036 
00037     TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
00038     FILE *fp = fopen("/local/car data.csv", "w");
00039     fprintf(fp, "RPM,X,Y,Z\n");
00040     tick.attach(dotick,1.0);
00041     while (1) {
00042         if (enable) {
00043 
00044             t.start ();
00045 
00046 
00047             while (enable) {
00048                 led4 = 0;
00049                 while (tick_active == 0) {}
00050                 led2 = !led2;
00051                 rpm_counter = counter.read();
00052                 counter.reset();
00053 
00054                 fprintf(fp,"%d,%.2f,%.2f,%.2f\n",60*rpm_counter,acc.x(),acc.y(),acc.z());
00055 
00056                 tick_active = 0;
00057                 led3 = !led3;
00058 
00059                 samples[index] = acc.y();
00060                 index++;
00061                 if (index >= 5) {
00062                     index = 0;
00063                 }
00064                 int i;
00065 
00066                 flipped_right = 1;
00067                 flipped_left =1;
00068 
00069                 for (i=0; i<5; i++) {
00070                     if (samples[i] > -0.8) {
00071                         flipped_left = 0;
00072                     }
00073 
00074                     if (samples[i] < 0.8) {
00075                         flipped_right = 0;
00076                     }
00077                 }
00078 
00079                 if (flipped_left||flipped_right||!enable) {
00080 
00081 
00082                     myservo = 1;
00083 
00084                     t.stop();
00085 
00086                     fprintf(fp,"this run lasted %f seconds \n", t.read());
00087                     fprintf(fp,"off,off,off,off");
00088                     fclose(fp);
00089                     led4 = !led4;
00090                     led2 = 0;
00091                     led3 = 0;
00092                 }
00093             }
00094         }
00095     }
00096 }