Skip to main content

Bottle party game

Introduction

Why play Spin the Bottle with a bottle if all you need is a smart box? Set your Start Set to draw a random member of your group. Whether it be at a party, while drawing a winner or deciding who will clean up. In this project, you will learn how to set up the box so it draws a random member of your group. 😱

You will need the box with the button and the USB dongle. All you need is a basic HARDWARIO kit, the Start Set.

Get it started in the Node-RED

  1. Start Set put together and pair.
  2. In the Playground, click on the Functions tab where the programming desktop is located.
  3. Let’s make it happen. 🤞 Place the MQTT node from the Input section in the desktop. Double click on the node and set the key feature - clicking the button. Copy the following line into the Topic field:

node/x-axis-detector:0/accelerometer/-/event-count

Edit mqtt in node dialog with the accelerometer event-count topic filled into the highlighted Topic field

Confirm it with the Done button.

Set up a random option

  1. You can programme a random option following a simple javascript. But don’t worry, we’ll help you. First, place the Function node from the Function section next to the MQTT.
Function node highlighted in the palette and placed next to the accelerometer MQTT node
  1. Double click on the node to open it. In the Name line, name the node (for example, Random option). In the Function line, copy this code exactly as you see it in the picture. With this code, one of the participants will be drawn.
var rand = Math.round( Math.random() * (flow.get("numberOfContestants") - 1));
msg.payload = flow.get("contestantArr")[rand];
return msg;
Edit function node dialog named Random pick with the random-draw JavaScript on the On Message tab

Confirm it with the Done button.

  1. Add another node next to the Random option, the Delay one (also found in the Function section). Thanks to this node, the response will be delayed a little and it will create an atmosphere of tension. Boo! 😲
Delay node highlighted in the palette, with a delay 5s node placed after the Random pick function
  1. Inside the node, make it even more random, set up a random delay. Click on random delay and select a time between 2 and 4 seconds. That should be just right for keeping the tension in the air.
Edit delay node dialog set to a random delay between 2 and 4 seconds

Confirm it with the Done button.

  1. Above all this, place a node that will set up a message that shows up when the box is busy picking. Use the Change node from the same section to do it.
Change node highlighted in the palette, with a set msg.payload node placed above the draw flow
  1. Open the node with a double-click and type here your message. For example, Picking...
Edit change node dialog setting msg.payload to the text Picking...

Set the participants

  1. Your lottery won’t do without a table reset button. You need it to continue playing. Under the MQTT place the Button node, this time from the Dashboard section.
Button node highlighted in the dashboard palette and placed below the MQTT node
  1. Double click on the node and in the Label line, name it Reset.
Edit button node dialog with the Label field set to Reset

Confirm it with the Done button.

  1. We are moving on. Now set all your buddies who will take part in the game. Anonymously, for now. Place them on the desktop using the Text input node in the Dashboard section. As many pieces as many you are.
Text input node highlighted in the palette, with five text input nodes placed on the canvas
  1. Within each node, rewrite the Label line to Participant + a number from one to as many as you are. So it will go Participant 1, Participant 2… you know the rest. In the Delay field, fill in the number 0. Uncheck the box just bellow it to really reset the fields after the reset. Do all this with all nodes with participants.
Edit text input node dialog with Label Participant 2, Delay set to 0 and the pass-through box unchecked

Confirm it with the Done button.

  1. Set up another javascript next to the participants. This javascript assigns the names of the participants to the corresponding place. Again, you will insert it as a Function node.

  2. Double-click on the node to open its settings. In the Label line, fill in the name of the node and copy this code into the Function field:

var contestants = flow.get("numberOfContestants") || 0;
var contestantArray = flow.get("contestantArr") || [msg.payload];
contestants++;
flow.set("numberOfContestants", contestants);

if(contestants != 1)
{
contestantArray.push(msg.payload);
}

flow.set("contestantArr", contestantArray);

Make sure the Output is really just one. ❗

Edit function node dialog named Fate, choose one of them with the code that stores each participant

Confirm it with the Done button.

  1. Don’t worry, we are almost finished. 🙌 Place the Change node on the desktop. This will ensure that everything gets restored to its original state when resetting.🖖
Second set msg.payload change node highlighted below the participant nodes
  1. In this node’s settings, fill in two Rules as you can see in the picture. The first one is Delete | flow | ContestantArr. To add another rule, click on the small +Add button below the field. In this Rule, set Delete | flow | numberOfContestants.
Edit change node dialog with two Delete rules clearing contestantArr and numberOfContestants

Confirm it with the Done button.

Only one can be selected

  1. Put the last node behind all this on the desktop. It will tell everyone who has been chosen. 🙏 You can find it simply as Text node in the Dashboard section.
Text node highlighted in the dashboard palette and placed at the end of the flow
  1. In the Label line inside the node, set how the message will look like when randomly selecting one participant.
Edit text node dialog with the Label set to And fate chooses...

Confirm it with the Done button.

  1. And then put it all beautifully together. In the upper part, connect all the nodes that ensure the draw, in the lower part, connect the ones that form the draw table.
Finished flow with the draw and participant nodes wired together and the Deploy button highlighted
  1. Don’t forget to click on the Deploy button in the top right corner! 🚨

Let the fun begin!

  1. And now, let’s get down to business! In the Dashboard tab, enter the names of all participants. If you haven’t selected the auto-refresh time period in the nodes for each participant, be sure to press Enter after each name. 👈
Dashboard with the Reset button, five participant name fields and the drawn name shown below
  1. Who was chosen by fate? And what for? It’s only up to you now. 😈 You can, for example:
  • draw who gives a kiss to whom (woohoo),
  • pull the shortest match for the one who takes out the trash,
  • draw the winner of a competition,
  • assign crazy tasks chosen by chance,
  • and anything else that crosses your mind!