Robert Hill
/
FRDM_HE_
Hall Effect Demo Program, using a hall effect sensor to calculate pedal speed while riding a bike
HallEffect.cpp@0:b83e5e826d81, 2016-02-24 (annotated)
- Committer:
- roberthill04
- Date:
- Wed Feb 24 17:42:35 2016 +0000
- Revision:
- 0:b83e5e826d81
This is the Hall Effect Demo Program, testing the functionality of the hall effect as a means of finding pedal speed while riding a bike.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roberthill04 | 0:b83e5e826d81 | 1 | #include "mbed.h" |
roberthill04 | 0:b83e5e826d81 | 2 | #include "AnalogIn.h" //not sure if this is necessary |
roberthill04 | 0:b83e5e826d81 | 3 | #include "DigitalIn.h" //^^^ |
roberthill04 | 0:b83e5e826d81 | 4 | |
roberthill04 | 0:b83e5e826d81 | 5 | DigitalOut led_red(LED_RED); |
roberthill04 | 0:b83e5e826d81 | 6 | DigitalOut led_green(LED_GREEN); |
roberthill04 | 0:b83e5e826d81 | 7 | DigitalOut led_blue(LED_BLUE); |
roberthill04 | 0:b83e5e826d81 | 8 | DigitalIn sw2(SW2); |
roberthill04 | 0:b83e5e826d81 | 9 | DigitalIn sw3(SW3); |
roberthill04 | 0:b83e5e826d81 | 10 | Serial pc(USBTX, USBRX); |
roberthill04 | 0:b83e5e826d81 | 11 | |
roberthill04 | 0:b83e5e826d81 | 12 | //LED is ON when set to 0, OFF when set to 1 |
roberthill04 | 0:b83e5e826d81 | 13 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:b83e5e826d81 | 14 | DigitalIn HallEffect(D2); //Intializes Digital input into pin D2 (Hall Effect sensor) |
roberthill04 | 0:b83e5e826d81 | 15 | |
roberthill04 | 0:b83e5e826d81 | 16 | |
roberthill04 | 0:b83e5e826d81 | 17 | int Hall_Counter = 0; //Initialize Counter for Hall Effect |
roberthill04 | 0:b83e5e826d81 | 18 | bool Low_Hall = true; //Initialize Flag for Hall Effect sensor |
roberthill04 | 0:b83e5e826d81 | 19 | bool Button_Pressed = true; //Initialize flag for output on terminal |
roberthill04 | 0:b83e5e826d81 | 20 | Timer Hall_Timer; //Initialize timer for pedal speed calc |
roberthill04 | 0:b83e5e826d81 | 21 | int Pedal_Time; //Intialize int for Time Passed |
roberthill04 | 0:b83e5e826d81 | 22 | int Pedal_Speed; //Initialize int for Pedal Speed |
roberthill04 | 0:b83e5e826d81 | 23 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:b83e5e826d81 | 24 | |
roberthill04 | 0:b83e5e826d81 | 25 | void Hall_Effect_Count(void) |
roberthill04 | 0:b83e5e826d81 | 26 | { |
roberthill04 | 0:b83e5e826d81 | 27 | if (sw3 == 0 && Button_Pressed == true) { |
roberthill04 | 0:b83e5e826d81 | 28 | //led_blue = 0; //Blue LED is ON |
roberthill04 | 0:b83e5e826d81 | 29 | //led_green = 1; |
roberthill04 | 0:b83e5e826d81 | 30 | //led_red = 1; |
roberthill04 | 0:b83e5e826d81 | 31 | pc.printf("Number of times magnet has passed sensor: %d\n", Hall_Counter); //Outputs # of times magnet passed sensor |
roberthill04 | 0:b83e5e826d81 | 32 | Hall_Timer.stop(); //stop the timer |
roberthill04 | 0:b83e5e826d81 | 33 | pc.printf("Time passed (in seconds): %f \n", Hall_Timer.read()); //Outputs time in seconds |
roberthill04 | 0:b83e5e826d81 | 34 | Pedal_Time=Hall_Timer.read()/60; //Divides Time in seconds by 60 so we have minutes for Pedal_Speed (RPM) |
roberthill04 | 0:b83e5e826d81 | 35 | Pedal_Speed=Hall_Counter/(Hall_Timer.read()/60); |
roberthill04 | 0:b83e5e826d81 | 36 | pc.printf("Approximate pedal speed: %d RPM\n", Pedal_Speed); //Outputs pedal speed |
roberthill04 | 0:b83e5e826d81 | 37 | Button_Pressed = false; |
roberthill04 | 0:b83e5e826d81 | 38 | //Hall_Timer.reset(); //idea here is the timer is reset after the program outputs the pedal speed so the demo can be reran |
roberthill04 | 0:b83e5e826d81 | 39 | } |
roberthill04 | 0:b83e5e826d81 | 40 | } |
roberthill04 | 0:b83e5e826d81 | 41 | |
roberthill04 | 0:b83e5e826d81 | 42 | int main() { |
roberthill04 | 0:b83e5e826d81 | 43 | led_blue=1; |
roberthill04 | 0:b83e5e826d81 | 44 | led_red=0; |
roberthill04 | 0:b83e5e826d81 | 45 | led_green=1; |
roberthill04 | 0:b83e5e826d81 | 46 | pc.baud(9600); |
roberthill04 | 0:b83e5e826d81 | 47 | pc.printf("Hello World from FRDM-K64F board. This is the Hall Effect Demo Program. "); |
roberthill04 | 0:b83e5e826d81 | 48 | pc.printf("Press SW3 (Button Near LED on board) to see Pedal Speed.\n "); |
roberthill04 | 0:b83e5e826d81 | 49 | Hall_Timer.start(); //Starts Timer for Pedal Speed Calculation **see if this is better in loop or out** |
roberthill04 | 0:b83e5e826d81 | 50 | //Looks like it works better here (Feb. 10) |
roberthill04 | 0:b83e5e826d81 | 51 | |
roberthill04 | 0:b83e5e826d81 | 52 | |
roberthill04 | 0:b83e5e826d81 | 53 | |
roberthill04 | 0:b83e5e826d81 | 54 | |
roberthill04 | 0:b83e5e826d81 | 55 | while (1){ //infinite loop |
roberthill04 | 0:b83e5e826d81 | 56 | //Hall_Timer.start(); //Start the timer ***See if timer works here or above while loop |
roberthill04 | 0:b83e5e826d81 | 57 | Button_Pressed = true; |
roberthill04 | 0:b83e5e826d81 | 58 | if(HallEffect==0 && Low_Hall==true) { //If Hall Effect Digital Output is low |
roberthill04 | 0:b83e5e826d81 | 59 | Hall_Counter++; //Add one to counter for calc pedal speed |
roberthill04 | 0:b83e5e826d81 | 60 | led_green = 0; //Output Green on LED, simulates wheel rotation "sensed" |
roberthill04 | 0:b83e5e826d81 | 61 | led_red = 1; |
roberthill04 | 0:b83e5e826d81 | 62 | led_blue = 1; |
roberthill04 | 0:b83e5e826d81 | 63 | Low_Hall = false; //flag to avoid errors |
roberthill04 | 0:b83e5e826d81 | 64 | } |
roberthill04 | 0:b83e5e826d81 | 65 | else if(HallEffect==1 && Low_Hall==true){ //Additional logic for accurate readings |
roberthill04 | 0:b83e5e826d81 | 66 | led_green = 1; |
roberthill04 | 0:b83e5e826d81 | 67 | led_red = 0; //Stays red while hall effect outputs digital high |
roberthill04 | 0:b83e5e826d81 | 68 | led_blue = 1; |
roberthill04 | 0:b83e5e826d81 | 69 | } |
roberthill04 | 0:b83e5e826d81 | 70 | else if(HallEffect==0 && Low_Hall==false){ |
roberthill04 | 0:b83e5e826d81 | 71 | led_green = 0; |
roberthill04 | 0:b83e5e826d81 | 72 | led_red = 1; |
roberthill04 | 0:b83e5e826d81 | 73 | led_blue = 1; |
roberthill04 | 0:b83e5e826d81 | 74 | |
roberthill04 | 0:b83e5e826d81 | 75 | } |
roberthill04 | 0:b83e5e826d81 | 76 | else if(HallEffect==1 && Low_Hall==false){ |
roberthill04 | 0:b83e5e826d81 | 77 | led_green = 1; |
roberthill04 | 0:b83e5e826d81 | 78 | led_red = 0; |
roberthill04 | 0:b83e5e826d81 | 79 | led_blue = 1; |
roberthill04 | 0:b83e5e826d81 | 80 | Low_Hall = true; |
roberthill04 | 0:b83e5e826d81 | 81 | } |
roberthill04 | 0:b83e5e826d81 | 82 | Hall_Effect_Count(); |
roberthill04 | 0:b83e5e826d81 | 83 | //wait(5.0f); //LOOK at this, this may be why the button isnt working**** |
roberthill04 | 0:b83e5e826d81 | 84 | } |
roberthill04 | 0:b83e5e826d81 | 85 | } |