Beginner in need of help !!


Closed Thread
Results 1 to 40 of 72

Hybrid View

  1. #1
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Quote Originally Posted by HenrikOlsson View Post
    Hi Rob,
    Welcome to the forum! As you can imaging there are several ways to do this (and most other things). Amoque shows you one, here's another (which also tries to take care of the debounce):
    Code:
    LED VAR GPIO.0
    Switch VAR GPIO.1
    
    ANSEL = 0                     ' No analog inputs
    CMCON = 7                     ' Comparators disabled
    TRISIO = %11111110            ' GPIO.0 as output
    
    Main:
      IF Switch = 1 THEN          ' If the button is pressed...
        TOGGLE LED                ' ...we toggle the LED.
        Pause 20                  ' Then we wait for contact bounce on make to die, may need tweaking
        WHILE Switch = 1 : WEND   ' Then we wait for the user to actually release the button
        Pause 20                  ' Finally we wait for contact bounce on release to die, may need tweaking
      ENDIF
    Goto Main
    /Henrik.
    Thanks for this Henrik, I found this the best way. Another question, what if I wanted to run a different command to just turning on the LED, what about if I wanted to start the LED flashing upon button tap? Could I do this with the same method?

    Thanks,
    Rob

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi,

    > what if I wanted to run a different command to just turning on the LED
    Don't understand this one, sorry...

    > what about if I wanted to start the LED flashing upon button tap?
    Well, yes. But as soon as you mention blinking it starts to get a Little bit more complicated. Why?
    Because then you need delays in there, during which (if using PAUSE) the button won't be polled so it'll feel unresponsive. Of course there are ways around this but the correct one depends on what you want to do after we get that LED to blink....

    Try this (I haven't, hope it works....):
    Code:
    LEDState VAR BYTE
    Time VAR WORD
    
    LED_OFF CON 0
    LED_ON CON 1
    LED_FLASH CON 2
    
    Rate CON 250		      ' Time between LED toggles when blinking
    
    Main:
      IF Switch = 1 THEN          ' If the button is pressed...
        LEDState = LEDState + 1
        IF LEDState = 3 THEN LEDState = 0
        Pause 20                  ' Then we wait for contact bounce on make to die, may need tweaking
        WHILE Switch = 1 : WEND   ' Then we wait for the user to actually release the button
        Pause 20                  ' Finally we wait for contact bounce on release to die, may need tweaking
      ENDIF
    
      IF (LEDState = LED_OFF) or (LEDState = LED_ON) THEN
        LED = LEDState            ' Turn LED on or off, solid.
      ENDIF
    
      IF LEDState = LED_FLASH THEN
        Time = Time + 1
        IF Time = Rate THEN
          TOGGLE LED
          Time = 0
        ENDIF
        PAUSEUS 1000
      ENDIF  
      
    Goto Main
    /Henrik.

Similar Threads

  1. Beginner help!
    By Recognize in forum General
    Replies: 14
    Last Post: - 26th April 2012, 15:55
  2. pic24 beginner
    By robertpeach in forum General
    Replies: 23
    Last Post: - 13th August 2009, 12:57
  3. need help! to beginner
    By emilhs in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 6th May 2009, 19:44
  4. Beginner at PB
    By alphahr in forum Off Topic
    Replies: 1
    Last Post: - 21st April 2008, 18:43
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 6th May 2005, 00:29

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts