![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Hall Effect Demo Program, using a hall effect sensor to calculate pedal speed while riding a bike
HallEffect.cpp
- Committer:
- roberthill04
- Date:
- 2016-02-24
- Revision:
- 0:b83e5e826d81
File content as of revision 0:b83e5e826d81:
#include "mbed.h" #include "AnalogIn.h" //not sure if this is necessary #include "DigitalIn.h" //^^^ DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); DigitalIn sw2(SW2); DigitalIn sw3(SW3); Serial pc(USBTX, USBRX); //LED is ON when set to 0, OFF when set to 1 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DigitalIn HallEffect(D2); //Intializes Digital input into pin D2 (Hall Effect sensor) int Hall_Counter = 0; //Initialize Counter for Hall Effect bool Low_Hall = true; //Initialize Flag for Hall Effect sensor bool Button_Pressed = true; //Initialize flag for output on terminal Timer Hall_Timer; //Initialize timer for pedal speed calc int Pedal_Time; //Intialize int for Time Passed int Pedal_Speed; //Initialize int for Pedal Speed /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Hall_Effect_Count(void) { if (sw3 == 0 && Button_Pressed == true) { //led_blue = 0; //Blue LED is ON //led_green = 1; //led_red = 1; pc.printf("Number of times magnet has passed sensor: %d\n", Hall_Counter); //Outputs # of times magnet passed sensor Hall_Timer.stop(); //stop the timer pc.printf("Time passed (in seconds): %f \n", Hall_Timer.read()); //Outputs time in seconds Pedal_Time=Hall_Timer.read()/60; //Divides Time in seconds by 60 so we have minutes for Pedal_Speed (RPM) Pedal_Speed=Hall_Counter/(Hall_Timer.read()/60); pc.printf("Approximate pedal speed: %d RPM\n", Pedal_Speed); //Outputs pedal speed Button_Pressed = false; //Hall_Timer.reset(); //idea here is the timer is reset after the program outputs the pedal speed so the demo can be reran } } int main() { led_blue=1; led_red=0; led_green=1; pc.baud(9600); pc.printf("Hello World from FRDM-K64F board. This is the Hall Effect Demo Program. "); pc.printf("Press SW3 (Button Near LED on board) to see Pedal Speed.\n "); Hall_Timer.start(); //Starts Timer for Pedal Speed Calculation **see if this is better in loop or out** //Looks like it works better here (Feb. 10) while (1){ //infinite loop //Hall_Timer.start(); //Start the timer ***See if timer works here or above while loop Button_Pressed = true; if(HallEffect==0 && Low_Hall==true) { //If Hall Effect Digital Output is low Hall_Counter++; //Add one to counter for calc pedal speed led_green = 0; //Output Green on LED, simulates wheel rotation "sensed" led_red = 1; led_blue = 1; Low_Hall = false; //flag to avoid errors } else if(HallEffect==1 && Low_Hall==true){ //Additional logic for accurate readings led_green = 1; led_red = 0; //Stays red while hall effect outputs digital high led_blue = 1; } else if(HallEffect==0 && Low_Hall==false){ led_green = 0; led_red = 1; led_blue = 1; } else if(HallEffect==1 && Low_Hall==false){ led_green = 1; led_red = 0; led_blue = 1; Low_Hall = true; } Hall_Effect_Count(); //wait(5.0f); //LOOK at this, this may be why the button isnt working**** } }