Help with button push detection

Ed Straker

Active member
I'm still very much in the early learning stages with Picaxe, as such, I am using Blockly for visual learning so I can see the relationship between Block and actual code.
With that said, here is my issue:

Trying to figure out how to get a momentary push button detection on the 08M2 (I assume it would be the same with any Picaxe)

Power up -> do nothing ->first btn push -> turn on pinX -> next btn push -> turn off pinX.
(all actions from a single button)

A Blockly solution would be most helpful at this point if at all possible so I can continue to build on from there.

Thanks for any help you folks can provide.
 

Technoman

Senior Member
Use a custom Basic block in the Advanced menu, then paste the basic command BUTTON with the necessary setup into it.

Seems no defined block is using this command.
 

Buzby

Senior Member
I don't use Blockly.
I also don't like 'blocking' code.

The 'blocking' code is in the lines with 'WaitFor' in them.
Your code is 'blocked' from doing anything else while it's waiting for a button push.

This might be acceptable if the only thing the whole program does is turn a single LED on and off, but usually a program needs to do more.
The program might have two buttons and two LEDs. In this case 'blocking' code will prevent you from triggering the action for each button independantly.

The program might need to respond to a temperature alarm. It can't do that if it's waiting for you to press a button !.

This is how I would approach the problem :

Rich (BB code):
symbol PushButton = pinC.0
symbol  LampioDIR = C.1
symbol       Lamp = pinC.1 

symbol    PBcopy = bit0
symbol     PBmem = bit1
symbol   PBredge = bit2

low LampioDIR ' Set pin as output, and drive it low

do    ' Detect rising edge of pushbutton
      PBcopy = PushButton
      PBredge = PBcopy andnot PBmem
      PBmem = PBcopy

      ' If rising edge detected, change lamp state
      if PBredge = 1 then
            Lamp = not Lamp
      endif

      ;
      ; Room here for more code 
      ; which can run all the time
      ;

loop
 

Ed Straker

Active member
This might be acceptable if the only thing the whole program does is turn a single LED on and off, but usually a program needs to do more.
The program might have two buttons and two LEDs. In this case 'blocking' code will prevent you from triggering the action for each button independantly.
That was the original intention but I am very much in the beginning stage of understanding code and how they interact. You also have code there that has no reference to published info i.e. PBredge, PBmem, PBcopy..???

Use a custom Basic block in the Advanced menu, then paste the basic command BUTTON with the necessary setup into it.
There is no "Advanced Menu" in PE6 w/Blockly, or at least I can't find it if there is??
 

Buzby

Senior Member
'PBmem, PBredge, PBcopy, Lamp, PushButton' are symbols that I made to represent PICAXE variables. ( See Manual 1, Page 75 )
It's easier to follow the code when symbols are used, they are essential when the code is hundreds of lines long and uses lots of variables.
( I don't use Blockly, but I would expect it to have a similar abilty to use symbols. )

Latching or non-latching ?.
From a simulation point of view, they're both the same !.
One 'click' of a pin sets it 'On', a second 'click'turns it 'Off'.
The first click represents when you press the button, and the second click is when you release it.
The simulator behaves like this because if the pins were 'momentary', how could you press more than one at a time ?.

( The simulator could be given the option to use pins as momentary, maybe with an option in the pin's 'right-click' menu. Unfortunately this would need a change to PE6, and Rev-Ed are notoriously slow at implementing user requested changes. )

Keep going, you'll get the hang of PICAXE soon !.

Cheers,

Buzby
 

papaof2

Senior Member
And when you are comfortable with the PICAXE, you'll find that the hardware chip just keeps on working.

I have a themally controlled fan (that's controlled by an 08M) in an A/V cabinet and it's been running since September of 2006. The fan has been replaced several times, the A/V cabinet has been replaced, the DVD recorder/player has been replaced and the TV has been replaced but the PICAXE-based controller just keeps running, holding the green LED on while it's reading the temperatrure (DS18B20) about once a second so you know what the controller is doing. Even the quietest of fans produces a little noise - probably more noticed for its constant hum, purr, buzz or roar (depending on its size and speed ;-) so you know when the fan is on - if the room is very quiet.
 

hippy

Technical Support
Staff member
Thanks for that, but in sim this action is for a latching P-Btn not a Mom P-Btn of am I missing something?
The code will work with a momentary push button but the simulated input pins are click to make high, click to make low. If it wasn't this way it would be impossible to simulate two momentary push buttons being pressed at the same time.

One way of considering it is that it will ultimately use a momentary push button but you are having to use a toggle switch to simulate that until one arrives in the post.
 

Ed Straker

Active member
Based on the examples you you have kindly provided, these are the solutions I have come up with. Your evaluation of these solutions would be greatly appreciated. This actually leads me to followup question below:

Part One:

The first one is based on Hippy's solution and was my attempt to expand it to 2 or more with that method:

25686


This second one was my attempt on multiple output control with toggle switches. Again with intention to expand to more. I just gave it simple led flash procedures to give it something to do:

25687

Ideally I would like to combine both multiple Mom detect with call procedures. But one thing at a time.

Which brings me to Part Two:

Regarding the input switch hardware, Manual3 has two examples of input control. Both have what appears to be 10k pulldowns. However; one has a 1k on the input pin and the other does not. Both seem to be doing the same thing. Question, why the 1k and is it necessary.

25688

Sorry about this rather long post and thanks for looking.
 

Attachments

hippy

Technical Support
Staff member
Question, why the 1k and is it necessary.
It's not absolutely necessary but is often recommended.

The 1K provides protection should the Pin be set Output Low and the button pushed which would create a V+ to 0V short through the pin which could potentially damage the pin or chip.

One comment on your "Part One" - You now have "call Chk Btn 1" checking two buttons. I would have put each in their own procedure routine and then called both in the main loop. Alternatively a naming change to "Check Buttons" or similar makes what it's doing match its naming.
 

Ed Straker

Active member
Thanks for clearing that up, I think. So if I understand your meaning would this be a correct assumption :

25699

BTW: was my solution to the multi-toggle switches right? Or is there a more efficient way of doing that procedure?

Thanks again.
 

hippy

Technical Support
Staff member
BTW: was my solution to the multi-toggle switches right? Or is there a more efficient way of doing that procedure?
Sorry; forgot to say it looked right to me but I haven't tested it.

As to there being a more efficient way; perhaps, but I would work on the principle that simplicity and clarity is often better than efficiency. And it doesn't stand out as being inefficient.

Wow, that Blockly is confusing. IMO you'd be much better off learning BASIC. You're gonna have to bite that bullet sooner or later.
I wouldn't say it was particularly confusing. It's just different, and verbose. What does get confusing is when someone creates a Blockly program which is one monolithic forever loop with everything nested deeply within that, no procedures so it's hard to tell what bits do what and what each part is actually doing - Imagine that code with the subroutines code dragged into where the subroutine calls are to save on calls. Such a wall of code is often impenetrable when one first looks at it.

I wasn't convinced by Blockly to start with, couldn't see the point or any advantage. I changed my mind when I started to use MIT App Inventor, which is also Blockly based. to churn out code for my Android phones.. I guess it's a case of having to walk in its shoes to get an appreciation of its virtues.
 

Ed Straker

Active member
I really appreciate all of you helping an old dog learn new tricks.
Sorry; forgot to say it looked right to me but I haven't tested it.
I cobbled together a test breadboard and shot both codes into the MCU and it went without a hitch and seems to work as intended.
IMO you'd be much better off learning BASIC
This probably true but considering I am dumb as a stump working with MCU's, and I am very much a more efficient visual learner. Blocky is helping me understand not just the code but how it is read and how they interact.
 

Technoman

Senior Member
There is no "Advanced Menu" in PE6 w/Blockly, or at least I can't find it if there is??
In Picaxe editor 6.1.0.0, I got this menu with the "Advanced" line and the Extension, to handle Grove modules.
25702
Here is some code you can paste into a Basic block, using the Button command :
A variable as to be declared (B0 or ...) although the auto-repeat mode is not used here.
Input : C.0 ; Output B.7 for test.

Code:
; jump to pushed when C.0 = 1
button C.0,1,0,0,b0,1,pushed   
goto notpushed
pushed:    toggle B.7        ; output B.7 on/off
notpushed:
Test file included.
 

Attachments

hippy

Technical Support
Staff member
Here is some code you can paste into a Basic block, using the Button command :
While it can work I'm not a great fan of using Basic Blocks to add stuff into Blockly unless it's to do something which Blockly can't do itself. It can lead to all sorts of conflicts and unpredictable behaviour, particularly 'b0' and other variables conflicting with 'varX' use in Blockly.

In most cases I would say it's should be either Blockly or Basic. Using the 'Convert' toolbar button to see the underlying Basic code the Blockly code represents can be good way of understanding how a Blockly program would be when written in Basic.
 

Ed Straker

Active member
It can lead to all sorts of conflicts and unpredictable behaviour, particularly 'b0' and other variables conflicting with 'varX' use in Blockly.
I noticed that when I tested the code. I tried to modify its behavior just trying to follow along with the code only and failed miserably. Thanks for the effort though.
 

Technoman

Senior Member
.. and failed miserably
As hippy was pointed it out VarA is b0, VarB is b1, ... Still you can rename a variable eg VarA to Cpt.
It could be seen on page 24 of Picaxe manual 5 (https://picaxe.com/docs/picaxe_manual5.pdf).

@hippy : How could we turn on the display of the code explorer as figured in the manual?

The Basic block is a convenient way to put in some basic code for eg initialisation needs. A students could then start from an xml file containing one or several blocks.

In France, we were not allowed to teach Basic before 14-15 and not to everyone, may be due to some messy programming in the eighties. Teaching for several years with flowcharts, later in 2015, as the students were using Scratch during some maths classes, we decided to switch to Blockly . After 15, Processing, HTML, PHP, C (robotics) are commonly taught. Basic?

I noticed than lot of students were having difficulties to understand nested blocks. IMHO, as the primary idea is to focus on algorithms, flowcharting would be a good start, (then Blockly) then a structured and documented basic code.
 

Ed Straker

Active member
How could we turn on the display of the code explorer as figured in the manual?
Click the CONVERT button on the top menu and it will create a .bas file in a second window showing you what the code looks like that you just blocked. Very neat feature but to bad it doesn't work the other way around. That would be MOST useful.

25711
 

Technoman

Senior Member
I still can't turn on the code explorer as shown on manual with bx, wx and Varx.
After opening a basic file, bx and wx are on display. After opening an xml file, only Varx is shown. Back on the basic code tab, Varx is on display but not bx and wx...
 

Technical

Technical Support
Staff member
right click over the variables area and display the variables column (it's hidden by default in blockly to simplify the view)

25712
 

Technical

Technical Support
Staff member
The Varx labels will be updated every time you compile, so do a syntax check or download to refresh them.

Blockly deliberately has a simplified look/feel for younger students, and so as constants and labels are not generally used within Blockly programs they are not displayed.
 

hippy

Technical Support
Staff member
As hippy was pointed it out VarA is b0, VarB is b1, ... Still you can rename a variable eg VarA to Cpt.
It's a bit more complicated than that. All 'varX' in Blockly are word variables and may not always be assigned as 'varA=w0' and so on.

The best way to assign variables within Basic Blocks is to assign them with highest and descending numbers, 'w27', 'b55:b54', first, then work down which should ensure less chance of Basic Block variables clashing with what Blockly itself has assigned.
 

hippy

Technical Support
Staff member
I noticed than lot of students were having difficulties to understand nested blocks. IMHO, as the primary idea is to focus on algorithms, flowcharting would be a good start, (then Blockly) then a structured and documented basic code.
While flowcharting is useful, has its place, is what most of us started with in times gone by, the biggest problem is that it leads to unstructured or 'spaghetti programming' with program links and jumps going all over the place.

That works but has now been recognised as a programming practice which shouldn't be encouraged because it can lead to problems in debugging and maintenance, especially as code is added and more 'spaghetti' is added on top. Many programming languages like Blockly don't even allow these jumps - usually implemented as GOTO - to avoid the problem.

I have never been a great fan of on-screen flowcharting as it's just too much dragging and dropping for me, even more so than Blockly. Because of the 'bad practices' it encourages I would suggest most students are better off starting with Blockly.
 

Technoman

Senior Member
While flowcharting is useful, has its place, is what most of us started with in times gone by, the biggest problem is that it leads to unstructured or 'spaghetti programming' with program links and jumps going all over the place....
As a start, if your goal is to get a student being able to run a small program which process inputs and display outputs, a flowchart is, IMO, the quickest way, but don't look at the converted code! May be a bit of AI could change that... At this point, you don't look for a reliable code and can't call it coding ; as they are on a 'no-code side', I don't think that would lead to 'bad practices' for sure. Later, the teaching of structured programming before/during coding will be necessary.
Clearly, Blockly is delivering a better code but requires an increased learning curve.
 

hippy

Technical Support
Staff member
On figure in page 24 of Picaxe manual 5, varX is seen as a byte variable. An old version?
I would guess so. Converting that "read analogue C.1 to varA" shows as "symbol varA = w0" in the converted code and the debug screen shows "w0" as "varA" rather than "b0".

if your goal is to get a student being able to run a small program which process inputs and display outputs, a flowchart is, IMO, the quickest way
I can agree with that, and it's probably one of the reason why flowcharting still gets used.

but don't look at the converted code!
I would say that illustrates why it's not great, because that code is the representation of what the code would be if done that way, but I will accept it is easier to comprehend on-screen, and can see your argument that you are not teaching students coding at this stage.

It is perhaps mostly that having links at that stage leads to the 'bad practice' of doing similar when students move to actually coding, ending up with equivalent GOTO all over the place, which makes things less obvious and more difficult to follow in a non-graphical programming environment.

I don't see the difficulty in "DO [something] LOOP", "IF <this> THEN [something] ELSE [something]" and so on, where that 'something' is any other block or sequence of blocks, nesting them as you go. But I accept I'm not a student having to get to grips with that, while 'connect this to that' saves having to explain it.

Perhaps the answer is that there is no right answer. It's definitely good to hear from a different perspective..
 

hippy

Technical Support
Staff member
Will flowchart continue to be supported in future revisions of the software?
I would expect so, can't see why they wouldn't be supported. Many schools do teach using flowcharts, have courses and classes structured around those, and I can't see we would want to alienate those users.
 
Top