jumper

Dependencies:   MMA7660 Speaker TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Räknar ut tiden det tar från botten till toppen i ett hopp. 
00003 Från det kan man räkna ut hur högt man hoppar, med hjälp av 
00004 h = (g * pow(t,2)) / 2;
00005 
00006 
00007 */
00008 #include "mbed.h"
00009 #include "MMA7660.h"
00010 #include "Speaker.h"
00011 
00012 
00013 //To get the x,y,z movements
00014 MMA7660 MMA(p28, p27);
00015 
00016 //To write to the terminal
00017 Serial pc(USBTX, USBRX); // tx, rx 
00018  
00019 // app board joystick
00020 // down: p12, left: p13, center: p14, up: p15, right: p16
00021 BusIn joystick(p12, p13, p15, p16);
00022 
00023 DigitalIn button(p14);
00024 BusOut leds(LED1, LED2, LED3, LED4);
00025 
00026 DigitalOut  red_(p23);
00027 DigitalOut green_(p24);
00028 
00029 Speaker speaker(p26);
00030 float MAX = 1.0;
00031 float g = 9.82;
00032 float flightTime = 0, height = 0;
00033 Timer t;
00034 
00035 bool up = false;
00036 bool top = false;
00037 
00038 int main() {
00039  
00040  green_ = 1;
00041  pc.printf("Running mBed\n");
00042  if(!MMA.testConnection()) red_ = 0;
00043  
00044  while (true) {
00045     
00046     if(MMA.y() > MAX && !up){        
00047         up = true;
00048         //MAX = MMA.y();
00049         //Never go above 5000Hz, can damage the mbed.
00050         //pc.printf("%f", MMA.y());
00051         
00052     }
00053     if(MMA.y() == 0.0 && !top) {
00054         t.start();
00055         speaker.PlayNote(969.0, 0.5, 1.0);
00056         speaker.PlayNote(800.0, 0.5, 1.0);
00057         top = true;
00058     }
00059     if(MMA.y() == 0.0 && top) {
00060         t.stop();
00061         
00062         printf("The time taken was %f seconds\n", t.read());
00063         top = false;
00064         up = false; 
00065         flightTime = t.read();
00066         height = g * flightTime * flightTime * 0.5;
00067         
00068         printf("The jump height was %f meters\n", height);
00069         
00070         t.reset();
00071     }
00072      
00073     
00074     
00075     if (button){
00076         leds = 0xf; // turns all LEDs on
00077         green_ = 0;
00078     }    
00079     else{
00080         green_ = 1;
00081         red_ = 1;
00082         leds = joystick;
00083     }
00084     wait(0.01);
00085     
00086    
00087  }
00088 }