How To Utilize Some of the Arduino SDK Commands

Driving - Drive with Raw Motors

#include <SpheroRVR.h> void setup() { // set up communication with the RVR rvr.configUART(&Serial); } void loop() { // reset the heading to zero rvr.resetYaw(); // drive forward with speed 64 on both motors rvr.rawMotors(RawMotorModes::forward, 64, RawMotorModes::forward, 64); delay(1000); // drive backward with speed 32 on both motors rvr.rawMotors(RawMotorModes::reverse, 32, RawMotorModes::reverse, 32); delay(1000); // drive backward with speed 64 on the left motor and forward with speed 64 on the right motor rvr.rawMotors(RawMotorModes::reverse, 64, RawMotorModes::forward, 64); delay(1000); // drive forward with speed 64 on both motors rvr.rawMotors(RawMotorModes::forward, 64, RawMotorModes::forward, 64); delay(1000); // turn both motors off rvr.rawMotors(RawMotorModes::off, 0, RawMotorModes::off, 0); delay(1000); } 


Drive With Heading

#include <SpheroRVR.h> void setup() { // set up communication with the RVR rvr.configUART(&Serial); } void loop() { // reset the heading to zero rvr.resetYaw(); // drive forward with speed 64 rvr.driveWithHeading(64, 0, static_cast<uint8_t>(DriveFlags.none)); delay(1000); // drive forward with speed 64 rvr.driveWithHeading(64, 0, static_cast<uint8_t>(DriveFlags.none)); delay(1000); // drive backward with speed 32 rvr.driveWithHeading(32, 0, static_cast<uint8_t>(DriveFlags.driveReverse)); delay(1000); // drive to the right with speed 64 rvr.driveWithHeading(64, 90, static_cast<uint8_t>(DriveFlags.none)); delay(1000); // drive to the left with speed 64 rvr.driveWithHeading(64, 270, static_cast<uint8_t>(DriveFlags.none)); delay(1000); // stop driving rvr.driveWithHeading(0, 270, static_cast<uint8_t>(DriveFlags.none)); delay(1000); } 


Drive with Helper

#include <SpheroRVR.h> static DriveControl driveControl; void setup() { // set up communication with the RVR rvr.configUART(&Serial); // get the RVR's DriveControl driveControl = rvr.getDriveControl(); } void loop() { // reset the heading to zero driveControl.setHeading(0); // drive forward with speed 64 driveControl.rollStart(0, 64); delay(1000); // drive forward with speed 64 driveControl.rollStart(0, 64); delay(1000); // drive to the right with speed 64 driveControl.rollStart(90, 64); delay(1000); // drive to the left with speed 64 driveControl.rollStart(270, 64); delay(1000); // stop driving driveControl.rollStop(270); delay(1000); } 

Drive with Raw Motors with Helper

#include <SpheroRVR.h>


static DriveControl driveControl;


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);


    // get the RVR's DriveControl

    driveControl = rvr.getDriveControl();

}


void loop() {

    // reset the heading to zero

    driveControl.setHeading(0);

    

    // drive forward with speed 64 on both motors

    driveControl.setRawMotors(rawMotorModes::forward, 64, rawMotorModes::forward, 64);

    delay(1000);

    

    // drive backward with speed 32 on both motors

    driveControl.setRawMotors(rawMotorModes::reverse, 32, rawMotorModes::reverse, 32);

    delay(1000);

    

    // drive backward with speed 64 on the left motor and forward with speed 64 on the right motor

    driveControl.setRawMotors(rawMotorModes::reverse, 64, rawMotorModes::forward, 64);

    delay(1000);

    

    // drive forward with speed 64 on both motors

    driveControl.setRawMotors(rawMotorModes::forward, 64, rawMotorModes::forward, 64);

    delay(1000);

    

    // turn both motors off

    driveControl.setRawMotors(rawMotorModes::off, 0, rawMotorModes::off, 0);

    delay(1000);

}


LEDS

Set Multiple LEDs

#include <SpheroRVR.h>


static uint32_t ledGroup;


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // setting up the led group for both headlights

    ledGroup = 0;

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightRed));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightGreen));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightBlue));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightRed));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightGreen));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightBlue));

}


void loop() {

    // set headlights to red and wait 1 second

    uint8_t redArray[] = {0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00};

    rvr.setAllLeds(ledGroup, redArray, sizeof(redArray) / sizeof(redArray[0]));

    delay(1000);

    

    // set headlights to blue and wait 1 second

    uint8_t blueArray[] = {0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF};

    rvr.setAllLeds(ledGroup, blueArray, sizeof(blueArray) / sizeof(blueArray[0]));

    delay(1000);

    

    // set headlights to green and wait 1 second

    uint8_t greenArray[] = {0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00};

    rvr.setAllLeds(ledGroup, greenArray, sizeof(greenArray) / sizeof(greenArray[0]));

    delay(1000);

    

    // set headlights to yellow and wait 1 second

    uint8_t yellowArray[] = {0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00};

    rvr.setAllLeds(ledGroup, yellowArray, sizeof(yellowArray) / sizeof(yellowArray[0]));

    delay(1000);

}


Set Multiple LEDs with Helper

#include <SpheroRVR.h>


static LedControl ledControl;


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // get the RVR's LedControl

    ledControl = rvr.getLedControl();

}


void loop() {

    // set up the array of led indexes for both headlights

    uint8_t ledIndexes[] = {static_cast<uint8_t>(LEDs::rightHeadlightRed),

                            static_cast<uint8_t>(LEDs::rightHeadlightGreen),

                            static_cast<uint8_t>(LEDs::rightHeadlightBlue),

                            static_cast<uint8_t>(LEDs::leftHeadlightRed),

                            static_cast<uint8_t>(LEDs::leftHeadlightGreen),

                            static_cast<uint8_t>(LEDs::leftHeadlightBlue)};


    // set headlights to red and wait 1 second

    uint8_t redBrightnessValues[] = {0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00};

    ledControl.setLeds(ledIndexes, redBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set headlights to blue and wait 1 second

    uint8_t blueBrightnessValues[] = {0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF};

    ledControl.setLeds(ledIndexes, blueBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set headlights to green and wait 1 second

    uint8_t greenBrightnessValues[] = {0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00};

    ledControl.setLeds(ledIndexes, greenBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set headlights to yellow and wait 1 second

    uint8_t yellowBrightnessValues[] = {0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00};

    ledControl.setLeds(ledIndexes, yellowBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

}


Set Single LED

#include <SpheroRVR.h>


static uint32_t ledGroup;


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // setting up the led group for the right headlight

    ledGroup = 0;

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightRed));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightGreen));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightBlue));

}


void loop() {

    // set right headlight to red and wait 1 second

    uint8_t redArray[] = {0xFF, 0x00, 0x00};

    rvr.setAllLeds(ledGroup, redArray, sizeof(redArray) / sizeof(redArray[0]));

    delay(1000);

    

    // set right headlight to blue and wait 1 second

    uint8_t blueArray[] = {0x00, 0x00, 0xFF};

    rvr.setAllLeds(ledGroup, blueArray, sizeof(blueArray) / sizeof(blueArray[0]));

    delay(1000);

    

    // set right headlight to green and wait 1 second

    uint8_t greenArray[] = {0x00, 0xFF, 0x00};

    rvr.setAllLeds(ledGroup, greenArray, sizeof(greenArray) / sizeof(greenArray[0]));

    delay(1000);

    

    // set right headlight to yellow and wait 1 second

    uint8_t yellowArray[] = {0xFF, 0xFF, 0x00};

    rvr.setAllLeds(ledGroup, yellowArray, sizeof(yellowArray) / sizeof(yellowArray[0]));

    delay(1000);

}


Set Single LED with Helper

#include <SpheroRVR.h>


static LedControl ledControl;


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // get the RVR's LedControl

    ledControl = rvr.getLedControl();

}


void loop() {

    // set up the array of led indexes for the right headlight

    uint8_t ledIndexes[] = {static_cast<uint8_t>(LEDs::rightHeadlightRed),

                            static_cast<uint8_t>(LEDs::rightHeadlightGreen),

                            static_cast<uint8_t>(LEDs::rightHeadlightBlue)};

  

    // set right headlight to red and wait 1 second

    uint8_t redBrightnessValues[] = {0xFF, 0x00, 0x00};

    ledControl.setLeds(ledIndexes, redBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set right headlight to blue and wait 1 second

    uint8_t blueBrightnessValues[] = {0x00, 0x00, 0xFF};

    ledControl.setLeds(ledIndexes, blueBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set right headlight to green and wait 1 second

    uint8_t greenBrightnessValues[] = {0x00, 0xFF, 0x00};

    ledControl.setLeds(ledIndexes, greenBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

    

    // set right headlight to yellow and wait 1 second

    uint8_t yellowBrightnessValues[] = {0xFF, 0xFF, 0x00};

    ledControl.setLeds(ledIndexes, yellowBrightnessValues, sizeof(ledIndexes) / sizeof(ledIndexes[0]));

    delay(1000);

}


Power

Get Battery State

#include <SpheroRVR.h>


static void getBatteryVoltageStateCallback(GetBatteryVoltageStateReturn_t *batteryVoltageStateReturn);


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // give RVR time to wake up

    delay(2000);

    

    // get the battery voltage state

    rvr.getBatteryVoltageState(getBatteryVoltageStateCallback);

}


void loop() {

    // must include this line if expecting a response from the RVR

    rvr.poll();

}


static void getBatteryVoltageStateCallback(GetBatteryVoltageStateReturn_t *batteryVoltageStateReturn)

{

    // put your code here to run when you get the battery voltage state

}


Get Battery State Notifications

#include <SpheroRVR.h>


static void batteryVoltageStateChangeNotifyCallback(BatteryVoltageStateChangeNotifyReturn_t *batteryVoltageStateChangeReturn);


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // enable notifications for battery voltage state changes

    rvr.enableBatteryVoltageStateChangeNotify(true, batteryVoltageStateChangeNotifyCallback);

}


void loop() {

    // must include this line if expecting a response from the RVR

    rvr.poll();

}


static void batteryVoltageStateChangeNotifyCallback(BatteryVoltageStateChangeNotifyReturn_t *batteryVoltageStateChangeReturn)

{

    // put your code here to run when you get a battery voltage state change notification

}


Sleep Monitoring

#include <SpheroRVR.h>


static void willSleepNotifyCallback(bool isSuccessful);

static void didSleepNotifyCallback(bool isSuccessful);


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);


    // set up the callback function for "will sleep" notifications

    rvr.setWillSleepNotifyCallback(willSleepNotifyCallback);

    

    // set up the callback function for "did sleep" notifications

    rvr.setDidSleepNotifyCallback(didSleepNotifyCallback);

}


void loop() {

    // must include this line if expecting a response from the RVR

    rvr.poll();

}


static void willSleepNotifyCallback(bool isSuccessful)

{

    // put your code here to run when you get a "will sleep" notification

}


static void didSleepNotifyCallback(bool isSuccessful)

{

    // put your code here to run when you get a "did sleep" notification

}


System

Get Main App Version

#include <SpheroRVR.h>


static void getMainApplicationVersionCallback(GetMainApplicationVersionReturn_t *getMainApplicationVersionReturn);


void setup() {

    // set up communication with the RVR

    rvr.configUART(&Serial);

    

    // give RVR time to wake up

    delay(2000);

    

    // get the main application version for the Nordic processor

    rvr.getMainApplicationVersion(static_cast<uint8_t>(Processors::nordic), getMainApplicationVersionCallback);

}


void loop() {

    // must include this line if expecting a response from the RVR

    rvr.poll();

}


static void getMainApplicationVersionCallback(GetMainApplicationVersionReturn_t *getMainApplicationVersionReturn)

{

    // setting up the led group for both headlights

    uint32_t ledGroup = 0;

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightRed));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightGreen));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::rightHeadlightBlue));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightRed));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightGreen));

    ledGroup |= (1 << static_cast<uint8_t>(LEDs::leftHeadlightBlue));

    

    if (getMainApplicationVersionReturn->minor >= 1)

    {

        // set headlights to yellow if the y value in version x.y.z is 1 or greater

        uint8_t rgbArray[] = {0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00};

        rvr.setAllLeds(ledGroup, rgbArray, sizeof(rgbArray) / sizeof(rgbArray[0]));

    }

    else

    {

        // set headlights to pink if the y value in version x.y.z is less than 1

        uint8_t rgbArray[] = {0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF};

        rvr.setAllLeds(ledGroup, rgbArray, sizeof(rgbArray) / sizeof(rgbArray[0]));

    }

}