Shop OBEX P1 Docs P2 Docs Learn Events
Using a button, LCD, user interface code — Parallax Forums

Using a button, LCD, user interface code

bulkheadbulkhead Posts: 405
edited 2006-08-25 12:35 in General Discussion
I'm trying to display some instructions on my serial LCD, and have them displayed and repated over and over until the user presses a pushbutton switch. I would like each message to be displayed for roughly 2 or 3 seconds (enough time to read) before the next message is displayed, however I would like my program to respond instantly when the pushbutton is pressed. In otherwords, I want my javelin to constantly check the switch's state and respond immediately to any change.

The twist is that the javelin is actually getting the switch data from my BS2 through a Uart. I would like some ideas on how to best implement this user interface so that it is quick and responsive. Here is what I have been considering:

while(true)
{
    LCD.write("message 1")
    **wait period**
    LCD.write("message 2")
    *wait period**
    LCD.write("message 2")
    *wait period**
 }//if no response keep waiting



For **wait period** I was thinking of:
for (int x=0;x<100;x++)
{
   getBS2Data(); //gets switch states and stores as class boolean variable
   if(switchIsPressed)
       break; //intended for while loop above
   CPU.delay(10);
}



however, the "break" breaks away from the IF statement, not the main while loop. I hope this makes sense.

So I'm just trying to implement a simple, user friendly quick responding interface but it is turning out to be pretty complicated. This is my first time doing something like this, any input on how to best implement this would be greatly appreciated, thanks.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-23 05:45
    The best way is to use a Timer object.
    See attachement. Note that I wrote the check routine
    to wait for a specific value to be received within a timeout period.
    You could pass both the specific value and the timeout value
    as parameters to the check routine, to make it more general.

    regards peter

    Post Edited (Peter Verkaik) : 8/23/2006 6:03:12 AM GMT
  • bulkheadbulkhead Posts: 405
    edited 2006-08-23 05:53
    Thanks for the quick response! Using the timer is a great idea, I didn't consider that. The "break" command only exits loops? I always thought it worked for if statements too...I guess in my code it was actually breaking from the for loop.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-23 06:05
    break and continue only work on loops while(){}, do{}while() and for() and on switch(){}.
    regards peter
  • bulkheadbulkhead Posts: 405
    edited 2006-08-24 03:12
    Well, I wrote all the code for my application using the timer and it looked good, until I programmed my javelin and got an exception about VP's. I checked and the Timer is my 7th VP. Here is my method:

    public static boolean waitForResponse(int msTimeout)
      {
        timer.mark();
        while(!timer.timeout(msTimeout))
        {
          getAndSendBS2Data();
          if(pushButton||switch4)  //boolean values received from BS2
            return true;
        }
        return false;
      }
    



    Is there a way to do this without a timer?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-24 07:14
    You could do without the Timer by first doing a testrun with a Timer object
    (comment out some code that uses a VP but does not involve this code)

    public·static·boolean·waitForResponse(int·msTimeout)
    ··{
    ··· int mscount = 0;
    ····timer.mark();
    ····while(!timer.timeout(msTimeout))
    ····{
    ······getAndSendBS2Data();
    ······if(pushButton||switch4)··//boolean·values·received·from·BS2
    ········return·true;
    ······mscount++;
    ····}
    ··· System.out.println(mscount); //display mscount value corresponding to msTimeout
    ····return·false;
    ··}

    Make sure not to press a button so waitForResponse() returns false and displays mscount.
    Then change the code to

    public·static·boolean·waitForResponse(int·mscount)
    ··{
    ····for (int i=0; i<mscount; i++)
    ····{
    ······getAndSendBS2Data();
    ······if(pushButton||switch4)··//boolean·values·received·from·BS2
    ········return·true;
    ····}
    ····return·false;
    ··}


    This only works if the getAndSendBS2Data() never waits for data. If there is data it updates
    some flags, if there is no data (use uart.bytAvailable()) it should return immediately.



    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-24 07:30
    A word about the above way:
    It is better to have the Timer object. You can have as many timers as you
    want and it still uses only 1 VP.
    Transmit uarts can be stopped until needed, as can adc VP for slowly changing
    signals (like monitoring a battery voltage), so there are ways to free VP slots
    which gives you the ability to use more than 6 VP, though only 6 VP
    can be active simultaneously.

    regards peter
  • bulkheadbulkhead Posts: 405
    edited 2006-08-25 06:08
    Peter, thanks so much, I simply stopped one of the VP's that I wasn't using and now my program works great!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-25 12:35
    I usually use the following vp setup:
    vp0: Timer
    vp1: receive uart1 or dac1/pwm1
    vp2: receive uart2 or dac2/pwm2
    vp3: receive uart3 or dac3/pwm3
    vp4: receive uart4 or dac4/pwm4
    vp5: half duplex receive uart5, transmit uarts 1-5, adc for slow signals

    This allows me at least 9 VP's.
    More vp's are possible in case of receive uarts used in half duplex operation
    where the javelin acts as master (eg. nothing is received until some message has been sent)
    The above setup allows me to receive 4 serial streams continuesly.

    regards peter
Sign In or Register to comment.