Super Drama!
Button platform
Button
Lights
Notes on the code
Normally, when you have only a single LED controlled by a pin, you'll want to add a resistor so you don't burn out the pin on the Scout. However, this button has a resistor inside it to handle this. There's much more detail on this on the SparkFun product page.
function startup { pin.write("d2", HIGH); pin.makeinput("d3", INPUT_PULLUP); };
function on.d3.low { message.group(1); };
The button's startup function tells digital pin 2 (the LED) to turn on when the board powers up. Then, it sets up digital pin 3 as a button. The Scout includes a pullup resistor on this pin, so you don't have to worry about managing the logic behind this.
This Scout's second function is an event handler that says, "when the button is pushed, send a message to all Scouts in group 1." Since all Scouts are automatically part of group 1 in their troop, this means everyone. Here's a great explanation of Pinoccio's mesh networking, which does a great job of explaining this, further down the page: Mesh All the Things!
function on.message.group { if (arg(2) == 7) { led.red(2000); n = n+1; }; };
The receiver function, saved to the other Scouts, checks the ID of the Scout that sent the message. In this case, the Scout inside my button was scout #7 in the troop. The "if" part isn't strictly necessary; in fact, you could simplify this function a lot and still have the button interaction:
function on.message.group { led.red(2000); };
But checking the argument allows you to expand with MORE buttons! You could add a big green button for Contestant #2 to hit, on a different Scout, and respond to that one by turning the LED green instead:
function on.message.group { if (arg(2) == 7) { led.red(2000); n = n+1; }; if (arg(2) == 2) { led.green(2000); }; };
You can also expand the functionality by making the Scout print the current counter value to Arduino's serial monitor:
function on.message.group { led.red(2000); n = n+1; print n; };
Or to Pinoccio's online HQ:
function on.message.group { led.red(2000); n = n+1; hq.print(key(n)); };
Comments
Please log in or sign up to comment.