Arduino Example -PartyBot
Get the party started! Turn your RVR+/RVR into a motion-activated disco ball with this fun and easy project. Only requiring a few extra parts, this example is great for beginners, but also has plenty of room for expansion for more experienced inventors!
Parts List
[RVR]
Breadboard *
3 Jumper Wires *
10KΩ Resistor **
* You could also just connect the wires from the sensor directly to your Arduino, but note that the wires on this particular sensor fit very loosely in the headers on the Arduino, which is why we recommend using a breadboard to ensure more reliable connections.
** Your circuit may work just fine without the resistor. However, if you notice that your output isn't changing as expected, you'll need to add the resistor as a pullup between the data and power pins. More info on that here.
Instructions
The first thing you'll need to do is get your circuit set up. Here's the wiring diagram:
Basically, there are three connections we're making:
connect the black wire (GND pin) of the PIR sensor to GND on the Arduino
connect the red wire (+5V pin) of the PIR sensor to 5V on the Arduino
connect the yellow wire (OUT pin) of the PIR sensor to digital pin 4 on the Arduino (you can use a different pin if you'd like but you'll need to make sure you change the code to match!)
As we mentioned above, the resistor may not be necessary. However, if your output isn't doing what it's supposed to, put the resistor between the output and power pins.
Next you'll need to flash the code to your Arduino. If you don't already have it installed, download the Arduino IDE here. Then, download the party_bot.ino file from our GitHub repo or follow along below to create it yourself!
We'll start our file by pulling in the SpheroRVR Arduino library so that our Arduino can communicate with RVR+/RVR. Next we'll define the pin that will be connected to the data pin of the PIR sensor, as well as the bitmask that indicates to RVR+/RVR that we want to set all the LEDs. We'll also set arrays that correspond with each color we'll want to use, filling them with the RGB values of the colors we'll be cycling through. Lastly, we'll want to set the index and RGB array for the current color:
#include <SpheroRVR.h>
#define PIRPIN 4
#define ALL_LEDS 0x3FFFFFFF
const uint8_t RED[] = {255, 0, 0};
const uint8_t ORANGE[] = {255, 100, 0};
const uint8_t YELLOW[] = {255, 255, 0};
const uint8_t GREEN[] = {0, 255, 0};
const uint8_t BLUE[] = {0, 0, 255};
const uint8_t PURPLE[] = {100, 0, 255};
const uint8_t WHITE[] = {255, 255, 255};
const uint8_t OFF[] = {0, 0, 0};
byte colorIndex = 0;
uint8_t currentColor[3] = {0};
Next, we'll write our setup method... We'll start by configuring the Arduino pin to read from the PIR sensor and initialize our communication with the RVR+/RVR. Finally, we'll insert a delay, to give RVR+/RVR time to wake up:
void setup() {
pinMode(PIRPIN, INPUT);
rvr.configUART(&Serial);
delay(2000);
}
Our loop is where all of our functionality will be! Our party bot is motion activated, so our first bit of logic is "activated" when our PIR pin gives us a reading of HIGH. When that happens, we'll increment the color index and set the currentColor. If we aren't seeing any motion, we turn off the LEDs (by setting the currentColor to OFF). After our logic, we'll create an array to store all of our LED color values, called ledValues. We'll initialize it with space for 30 values, as we are setting 10 LEDs that each have a (1) Red, (2) Green and (3) Blue component. To change the values in the array, we create a for loop that changes each LED by looping through the ledValues array and setting each value in it. We'll then set all of the LEDs (using setAllLeds) with the values from the ledValues array. The delay at the end tells the program to loop every second:
void loop() {
if (digitalRead(PIRPIN) == HIGH)
{
colorIndex += 1;
colorIndex = colorIndex % 7;
setCurrentColor();
}
else
{
memcpy(currentColor, OFF, 3);
}
uint8_t ledValues[30] = {0};
for (int i = 0; i < 28; i = i + 3)
{
ledValues[i] = currentColor[0];
ledValues[i + 1] = currentColor[1];
ledValues[i + 2] = currentColor[2];
}
rvr.setAllLeds(ALL_LEDS, ledValues, 30);
delay(1000);
}
You may have noticed that, in our loop, we used a method called setCurrentColor that had not yet been defined; let's define it! The setCurrentColor method will update the currentColor array based on the current colorIndex value:
void setCurrentColor()
{
switch (colorIndex) {
case 0:
memcpy(currentColor, RED, 3);
break;
case 1:
memcpy(currentColor, ORANGE, 3);
break;
case 2:
memcpy(currentColor, YELLOW, 3);
break;
case 3:
memcpy(currentColor, GREEN, 3);
break;
case 4:
memcpy(currentColor, BLUE, 3);
break;
case 5:
memcpy(currentColor, PURPLE, 3);
break;
case 6:
memcpy(currentColor, WHITE, 3);
break;
default:
break;
}
}
If you haven't used the Arduino IDE before, you'll need to get it configured for your board. Plug your Arduino in to your computer using your A/B USB cable. Go to the Tools tab and make sure Board is set to "Arduino/Genuino Uno" and set your Port to the USB port you'll be using (you can usually figure this out through trial and error). Once everything's configured, click the Upload button (the one with an arrow) in the top left corner of the IDE. Wait a few seconds until it tells you it's done uploading, and then you're all set!
With your circuit built and your Arduino flashed with code, all that's left to do is plug it in and turn it on! Just connect the Arduino to the USB port of the RVR+/RVR and power on the RVR+/RVR. Make sure you mount your circuit so that the PIR sensor is facing up and not blocked by anything. Also note that the PIR sensor can take up to 30-60 seconds to stabilize after you turn it on, so don't panic if things seem a little wonky at first! If you wait a minute and things are still a little off, read this guide about changes you can make to improve the PIR sensor's performance.
Congratulations! You just built your very own PartyBot!
Now What?
The party's just getting started! Here are some ideas for things you can do to make an even cooler party bot:
Parties need music! See if you can add a speaker into the mix.
Try making your disco ball spin! Keep in mind that when it's spinning, the sensor will read it as motion even if nothing around the RVR+/RVR is moving. Try designing a mount that holds the sensor stationary while the RVR+/RVR spins!
Swap out the motion sensor for a microphone to make the PartyBot voice-activated instead!