Shop OBEX P1 Docs P2 Docs Learn Events
Display + Button — Parallax Forums

Display + Button

PlaneTeeRPlaneTeeR Posts: 100
edited 2006-05-26 20:53 in General Discussion
Hello everyone,

I now have a LCD display and button, all is working. But now I have to make a selection with the button what I want to display. The LCD has two lines, the upper always shows the speed, and the lower shows 3 things nl: AVG(Average Speed), DST (Distance)·& CHR (Chronometer). How do I make this without much programm to run. I thought of something like this:

int button; //the state of the button

if(CPU.readPin(BT1PIN) == false){
· button++;}
··if(button>2){
····button=0;}

And then selecting with another if else statement which info you want to diplay, is this a good idea, or can it be faster?

Thanks Johnny
·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-25 11:16
    A slightly faster version:

    if (!CPU.readPin(BT1PIN)) {
    · button = (button+1)%3; //result values 0, 1 and 2
    }

    If the code that follows the above takes a bit of time I see no problems.
    But there is also a Button class that allows you to debounce, but it
    probably is not required.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-25 12:39
    Oke, thanks for that peter.

    You have followed my project and I've noticed that, when the speed is formatted and distance or something else on line 2, the javelin mc will not read below 70ms between pulses. When I let out these codes it reads to 1 ms. Is there a way to seperate these two things, so it wil run on the background and the display wil update itself every second or so?

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-25 12:48
    You can comment out the debug statements once your program seem to work.
    That already saves alot of time. You can use another timer for your display update
    section:

    static Timer lcdTimer = new Timer();

    with your lcd update code

    ···· if (lcdTimer.timeout(1000)) { //update lcd approx. once per second
    ······· //your lcd update code
    ······· lcdTimer.mark();
    ···· }


    That leaves you more time to check pulses and update speed,·distance and time variables.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-26 18:23
    I hava the 4x while loop for the bikecomputer and where do i have to put the button code? This is the code:

    ...output omitted...
    while (!CPU.readPin(senPin));
    while (true) {
    while (CPU.readPin(senPin));
    tPulse.mark();
    CPU.writePin(ledPin,true);
    while (!CPU.readPin(senPin));
    ...output omitted...

    When i put it inside these loops it does not·work until i hold the button and there comes a pulse! How should i do this?

    Johnny

    Post Edited (PlaneTeeR) : 5/26/2006 6:30:44 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-26 19:20
    Place it outside the while loops.
    That way the button is scanned once every mainloop.
    You mentioned the mainlooptime varied from 10-50 mSec or so,
    which is sufficient.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-26 19:35
    I forgot about the possible timeouts.
    For a fast response to the button press,
    insert a button test within each while loop.
    Then check the button test result outside the while loops.
    See attachment. I set the button to pin13.
    (buttonpin=low is button pressed, so a pullup resistor is required)

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-26 20:21
    I've done a small test also, i've placed the buttoncode in the forever while loop and this came out:

    low speeds 30 Km/h > press a bit longer than normal
    middle speed 30 -·70 Km/h > press·normal stable button
    fast·70 Km/h or higher > button very unstable, when pressed you can skip a few modes.

    With this i can live. smile.gif

    Also I have all these display format formules in the loop, so when the bike stops i can't get any readings from it. Or i have to place them also outside but that is too much of the good i think. Or can this all be solved with different classes, one class display one processor and so? Can this be done with javelin? So that i create objects where the pulses are constant read and so on?

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-26 20:53
    In the example that I provided, when there are no pulses, but you do press
    the button, the pulse while loops are exited immediately and you then
    enter the button code. No matter if a pulse is being detected, once the
    button is pressed, the current pulse simply is missed. That is no
    problem as long as you calculate distance using·looptime.
    To make the button·more stable at higher speeds,
    wait for the button release,
    Add the following to your button code
    CPU.delay(525); //wait 50 mSec (debounce)
    while (!CPU.readPin(buttonPin) ; //wait for button release
    CPU.delay(525); //wait 50 mSec (debounce, optional)
    This makes sure you have a valid button press.
    Since there is no validPulse if the button has been pressed
    the delay·is not a problem.

    regards peter
Sign In or Register to comment.