A system to help you ride your bike better than you do right now.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed LSM9DS1_Library_cal
Diff: main.cpp
- Revision:
- 4:1928bf053958
- Parent:
- 3:26e0c0d7984f
- Child:
- 5:436b39863099
diff -r 26e0c0d7984f -r 1928bf053958 main.cpp --- a/main.cpp Sun Dec 04 21:42:06 2016 +0000 +++ b/main.cpp Sun Dec 04 23:03:37 2016 +0000 @@ -7,10 +7,23 @@ #define START_S 1480719111 +#define LEFT 0xA +#define RIGHT 0xB +#define STOP 0xC +#define GO 0xD + +int lightState; + DigitalOut led(LED1); + SDFileSystem sd(p11, p12, p13, p14, "sd"); //mosi -> DI, miso <- DO, slck -> sclck, CS -> CS uLCD_4DGL lcd(p28, p27, p29); +// speed sensor +InterruptIn hallSensor(p8); +Timer hallT; +int stopped; + float miles = 0; float speed = 0; float maxSpeed = 0; @@ -18,31 +31,42 @@ void store_trip(); void recall_trips(); +int get_state(); +void pass(); -int main() { - set_time(START_S); // Set RTC time - +int main() { // open the file for reading and appending records // recall last trip recall_trips(); - wait(5); + wait(10); lcd.cls(); // normal operation loop here - bool going = true; + int going = 1; // cyclist is moving + set_time(START_S); // Set RTC time + + hallSensor.fall(&pass); + hallT.start(); // start the hall sensor timer + while(going) { + seconds = time(NULL) - START_S; // return the seconds passed since start + if (hallT.read() > 6.0 && !stopped) { + speed = 0.0; + stopped = 1; + } + + going = (hallT.read() > 20.0 && stopped) ? 0 : 1; + maxSpeed = (speed > maxSpeed) ? speed : maxSpeed; - led = 1; - wait(0.5); - led = 0; - // check current speed - // check curret distance - // check time so far - // display this on the screen - // check if braking, turning left or right - // show that on the screen + lcd.locate(0, 1); + lcd.printf("Distance : %3.1f\n\n", miles); + lcd.printf("Top speed : %2.1f\n\n", speed); + lcd.printf("Time : %1.1f\n\n", (float)seconds / 3600); + + // light states code + lightstates = get_state(); } // store this trip @@ -135,4 +159,14 @@ } file.close(); +} + +void pass(void) { + // interrupt, performed when the hallsensor passes the magnet + stopped = 0; // reset the global + hallT.stop(); + speed = 0.00136364 / (t.read() / 3600); // current speed + miles += 0.00136364; // circumference of the tire in miles + t.reset(); + t.start(); } \ No newline at end of file