0

I've run into a problem while coding a speedometer meant for bicycles. The speedometer would require the diameter of the wheel in inches for maximum accuracy, so I'd like to have an adjustable (by potentiometer) numerical prompt appear on two 7 segment LEDs. So, for example, the device would be turned on, it would display "16," the user would turn the potentiometer knob until the 7 segment LED displays "24," then the user would push a button [indicating the beginning of the loop()], and the speedometer would begin measuring and displaying his or her speed.

This would be an easier task to accomplish in the loop section of the code, but, because I only want this series of potentiometer analogReads to occur once, it gets a bit trickier.

Some clarification: Every other component of the speedometer works (such as the 7 segment LEDs via shift register and the actual measurements), so I'm only looking for help with the code for the potentiometer.

Thanks!

2 Answers 2

1

You presumably know that: The easiest and usual way to read a potentiometer is to connect the "track" between V_pot (often = Vcc) and ground and to connect the pot "wiper" to an analog input pin. You then use analogRead(Ax) to read the pot value in the range 0-255.

The use of analogRead() is valid in setup.

However a more "real" method would be to have a "flag" variable that indicates that the speedo initialisation code has or hasn't been run.
Set this flag to false in setup, detect and action it in loop and then set the flag to true so that the code is not repeatedly actioned.


eg

void setup() {

... boolean initialised = false ; ... }

void loop() { if (initialised == false) {
// initialise speedo
// .... initialised = true; }

...

} // end of loop()

}

1

If you really want to use this feature in the setup() then you can do like this:

#define BUTTON 2
boolean not_done = true;

void stopDiameterSetup() {
    // some button debounche code
    ...
    // we are done with setting up the diameter.
    not_done = false;
}
void setup() {
    ...

    // POT reading
    interrupts();
    attachInterrupt();
    while(not_done) {
       attachInterrupt(BUTTON, stopDiameterSetup, CHANGE);
    }       
}
void loop(){
// your loop code
}

If you can't or afraid to use interrupts then just check for a button pressed inside the while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.