SparkFun Forums 

Where electronics enthusiasts find answers.

General MicroView Support area - You have questions, find your answers here.

Moderators: marcus@geekammo, Help@GeekAmmo

By theaddies
#176492
I am attempting to make a simply bike computer for a stationary bike. The microview will display speed and total distance. The "ride" will be started with a push button and then the wheel rotations will be counted by a halleffect sensor that drops low every time the wheel passes with a magnet. I am having trouble with the code below. Is there anything obviously wrong. I apologize for my code as I know it is sloppy.
Code: Select all
#include <MicroView.h>
MicroViewWidget *widget,*widget2;

int buttonPin = A0;     	// push button pin
int halleffect_state = 0, halleffect_value=0;		// variable to store the pushbutton status
int start_pin = A1;     	// push button pin
int start_button = 0;		// variable to store the pushbutton status
unsigned long start_time;
unsigned long run_time;
int wheel_counter=0, wheel_counter_new, wheel_counter_delta;
int wheel_circum=2250;
float bike_speed=0;
float total_distance;

void setup() {
    uView.begin();
    uView.clear(PAGE);
    widget= new MicroViewGauge(32,30,0,20);  // draw Gauge widget at x=32,y=30,min=0, max=100
    widget2= new MicroViewSlider(0,0,0,20);  // draw Slider widget at x=0,y=0,min=0, max=100
    
    pinMode(buttonPin, INPUT);  	// initialize the halleffect sensor pin as an input
    digitalWrite(buttonPin,HIGH);  	// set Internal pull-up
    pinMode(start_pin, INPUT);  	// initialize the pushbutton pin as an input
    digitalWrite(start_button,HIGH);  	// set Internal pull-up
     
}

void loop() {

        start_button = digitalRead(start_pin);  //button to start the ride
 
        if (start_button == LOW) {
          start_time = millis();
          wheel_counter = 0;
        }
        halleffect_state = digitalRead(buttonPin);	// read the state of the wheel counter sensor
        
        if (halleffect_value != halleffect_state && halleffect_state == LOW) {     
            wheel_counter = wheel_counter + 1;

 	}
        halleffect_value = halleffect_state;
        
        if (wheel_counter % 5 == 0) {
          wheel_counter_delta = wheel_counter - wheel_counter_new;
          run_time = millis() - start_time;
          bike_speed = float (wheel_circum * wheel_counter_delta * 3.2805 / 5280. / run_time * 3600.);
          total_distance = float (wheel_circum * wheel_counter * 3.2805 / 5280.);
          wheel_counter_new = wheel_counter;
        }
        widget->setValue(int(total_distance));    // give a value to widget
        widget2->setValue(int(bike_speed));
        uView.display();        // display current page buffer

  }