Intro!
So you want a get a button working with a Pinoccio Scout? Easy! We'll show how to get a button working stat, along with how to make the LED light upon a button press--or anything else you want to do. Let's go!
Attach your button
Hook up your pushbutton like this:
- One pin of the button to pin D7
- Other pin of the button to pin GND
Now we get the ScoutScript code set up!
D7 is the pin our button is connected to? Just enter the following code in the IDE Serial Monitor, or in the HQ console:
pin.save("d7", INPUT_PULLUP);
Now not only is this pin set to input with pull-up enabled, but it will automatically configure this pin on every power up! Very nice!
Let's check that the button works
Let's turn on verbose event handling, so we can see the pin change events happen in real-time whenever we press the button. Run this in the IDE serial monitor or in the HQ console.
events.verbose(1);
Now watch what happens:
The button event is printed every time the button is pressed or released
You'll notice that the second argument in the event handler will change from 0 to 1. It goes to 0 when the button is pressed and back to 1 when released. Cool!
Now let's make things happen when the button is pressed
Great, so event handling is working, our button triggers on both press and release. But what about actually doing something when the button is pressed? That's where event handlers come in.
Every time a digital pin goes from high to low, or from low to high, a ScoutScript command will run--if it is defined.
So let's define this command. It must be named on.d7.low for triggering when the button gets pressed, or on.d7.high for triggering when the button is released. Let's turn the LED red when we press the button, and off when we release it.
function on.d7.low { led.red }
function on.d7.high { led.off }
Define both of those commands by entering them into the serial monitor, just as you see here.
All ScoutScript event handlers can be found here:
https://docs.pinocc.io/scoutcommands.html#event-callbacks
LED driven from a button!
Now press the button, and the LED will turn red. Release it and it turns off.
You can do anything in this event handler command--read a sensor, send a mesh message, or go to sleep. Anything you want to do!
Comments