Who sees my stupid mistake?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Defective breadboard?
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  2. #2
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    In the meantime there is some progress:
    Code:
    CMCON =  7                                       ' Digital mode
    DEFINE OSC 20                                    ' 20 MHZ OSC.                                                                      
    TRISB= %00111111                                 ' PORTB 6/7=Output 0..5=Input
    
    Progstart: 
      if (PORTB & 1) = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif  
      if (PORTB & 2) = 2 then 
        Serout2 PORTB.7, 16384+12, [144, 46, 127]    ' Fast Fwd.
      endif  
      if (PORTB & 4) = 4 then 
        Serout2 PORTB.7, 16384+12, [144, 48, 127]    ' Stop  
      endif  
      if (PORTB & 8) = 8 then 
        Serout2 PORTB.7, 16384+12, [144, 50, 127]    ' Play  
      endif  
      if (PORTB & 16) = 16 then 
        Serout2 PORTB.7, 16384+12, [144, 52, 127]    ' Record
      endif  
    
    Debounce:
      Pause 100                                      ' ? shorter ?
      If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
           goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart
    This works, as long as I comment out these lines:
    Code:
     '  If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
     '     goto Debounce                                ' Button still pressed?
     '  endif
    The idea is of course to prevent that the same data is sent multiple times, so I want to wait until a button is released.
    But the these lines seem to avoid that PORTB is read again.
    Last edited by RuudNL; - 3rd March 2024 at 10:42.

  3. #3
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    It seems the problem is solved. (Although I still don't understand why my first attempt didn't work...)
    Now I use BUTTON as included in Picbasic. I don't like all the labels, but at least this works now!
    Code:
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    '* MIDI Remote for Reaper            20 MHz resonator (HS)           *
    '* PIC16F628                                                         *
    '* PORTB.0 = Rewind                                                  * 
    '* PORTB.1 = FastFwd.                                                *
    '* PORTB.2 = Stop                                                    *
    '* PORTB.3 = Play                                                    *
    '* PORTB.4 = Record                                                  *
    '* PORTB.7 = Output                                                  *
    '* Version 1.0                       03 March 2024                   *
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
            
    CMCON =  7                                       ' Digital mode
    DEFINE OSC 20                                    ' 20 MHZ OSC.       
    'DEFINE BUTTON_PAUSE 50                          ' Is this needed ?                                     
    TRISB= %00111111                                 ' PORTB 6..7=Output 0..5=Input
    
    BTN1 var Byte                                    ' Button variables for internal use 
    BTN2 var byte
    BTN3 var byte
    BTN4 var byte
    BTN5 var byte
    
    BTN1 = 0                                         ' Initialize button variables
    BTN2 = 0
    BTN3 = 0
    BTN4 = 0
    BTN5 = 0
    
    Progstart: 
      BUTTON PORTB.0, 1, 255, 0 , BTN1, 0 , But2
      Serout2 PORTB.7, 16384+12, [144, 44, 127]      ' Rewind
    But2:
      BUTTON PORTB.1, 1, 255, 0 , BTN2, 0 , But3
      Serout2 PORTB.7, 16384+12, [144, 46, 127]      ' Fast Fwd.
    But3:  
      BUTTON PORTB.2, 1, 255, 0 , BTN3, 0 , But4
      Serout2 PORTB.7, 16384+12, [144, 48, 127]      ' Stop  
    But4:  
      BUTTON PORTB.3, 1, 255, 0 , BTN4, 0 , But5
      Serout2 PORTB.7, 16384+12, [144, 50, 127]      ' Play  
    But5:
      BUTTON PORTB.4, 1, 255, 0 , BTN5, 0 , ProgStart
      Serout2 PORTB.7, 16384+12, [144, 52, 127]      ' Record
    Goto Progstart
    The moral: never try to reinvent the wheel!

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,626


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I think the first version should work - but do so quite unreliably.
    The reason: If the button being pressed happens to FIRST be checked in the debounce routine the code will be stuck there until the button is released and no action will be taken.
    One would think that if you have played with this for some time it would at least sometimes be detected at the "correct" place before falling into the debounce though.

    It would probably be a better idea to GOSUB the debounce routine after the SEROUT2 statement or simply:
    Code:
      if (PORTB & 1) = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
         WHILE PORTB.1 : WEND
         PAUSE 10  ' Or whatever
      endif

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,175


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Henrik, I think this does not explain why code in post #4 also does not work as the RuudNL says.

    Ioannis

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,626


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    You mean this:
    Code:
    Progstart: 
      if PORTB.0 = 1 then 
        Toggle PORTB.6
      endif  
    
    Debounce:
      Pause 20                                       ' ? shorter ?
      If PORTB.0=1 then
        goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart
    If so then it's the same potential issue. The PIC runs thru this short program in a couple of microseconds (lets say 20), except for the PAUSE 20 where it spends 99.9% of its time. The likelyhood of the button being pressed during this time and therefor NOT cause the output to toggle is 999 in 1. In the initial code the PAUSE was even longer further decreasing the chanse of the button being polled outside of the debounce routine. Run the code, press the button a few thousand times it "should" toggle the output at least once.

    That's my theory and I'm sticking to it until someone comes up with a better one :-)

    /Henrik.

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Very good reasoning.
    😎
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

Similar Threads

  1. I don't understand my mistake
    By kik1kou in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st January 2015, 13:56
  2. Stupid simple question.....
    By chien_fu in forum mel PIC BASIC
    Replies: 18
    Last Post: - 23rd February 2010, 14:21
  3. Stupid question about LCDOUT
    By Glenn in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 7th October 2008, 22:37
  4. SERIN2 – SEROUT2 and Manchester mistake.
    By RCtech in forum Serial
    Replies: 8
    Last Post: - 4th September 2007, 23:55
  5. Stupid question
    By Meriachee in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 20th July 2007, 06:47

Members who have read this thread : 1

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