Defective breadboard?
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!
In the meantime there is some progress:
This works, as long as I comment out these lines: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
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.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
But the these lines seem to avoid that PORTB is read again.
Last edited by RuudNL; - 3rd March 2024 at 10:42.
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!
The moral: never try to reinvent the wheel!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
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
Henrik, I think this does not explain why code in post #4 also does not work as the RuudNL says.
Ioannis
You mean this: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.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
That's my theory and I'm sticking to it until someone comes up with a better one :-)
/Henrik.
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!
Bookmarks