Pin Trigger handling in ScratchGPIO

I was looking at some of William Bell’s RPiScratchIO scratch code and notice that he had the concept of receiving broadcasts on inputs pins changing state.

So I’ve started implementing this (in dev version)

Basic concept is that if an input changes state – it will send a broadcast message to Scratch.

So if Scratch has a hat block that says

On Trigger received
Say “An input has changed state” for 2 seconds
Broadcast ResetTrigger

then if  any pin changes from 0 to 1 (or 1 to o) then Scratch will run the On Trigger block.

To make sure that only one event is dealt with at a time, my handler will not send any more changes until it receives a ResetTrigger from Scratch which is why that Broadcast ResetTrigger command is at the end.

As well as this global trigger, each pin has its own trigger so to just detect a change in pin7 use

On TriggerPin7 received
Say “Pin7 has changed state” for 2 seconds
Broadcast ResetTriggerPin7

If using an addon that names the inputs (say Switch) then the following would be triggered on a switch press

On TriggerSwitch received
Say “The switch has changed state” for 2 seconds
Broadcast ResetTriggerSwitch

 

You may also like...

Leave a Reply

Your e-mail address will not be published. Required fields are marked *