Now let's bring it all together into our final code. We just add our animation functions to the Hand Position Detection code and then replace the Serial.println()
statements with calls to our left/right turn animations.
And here it is:
/////////////////////////////////////////////////////////////////////////////// // Circuit Playground Bike Glove - With "On/Off" // // Author: Carter Nelson // MIT License (https://opensource.org/licenses/MIT) #include <Adafruit_CircuitPlayground.h> #define THRESHOLD_UP 5 // threshold for hand up test #define THRESHOLD_RIGHT 5 // threshold for right turn #define THRESHOLD_LEFT -5 // threshold for left turn #define BRIGHTNESS 255 #define LEFT_COLOR 0xFFFFFF #define RIGHT_COLOR 0xFFFFFF #define ANIM_DELAY 200 /////////////////////////////////////////////////////////////////////////////// void leftTurnAnimation() { // just to be sure, turn off all NeoPixels CircuitPlayground.clearPixels(); // Turn on 5 & 4 CircuitPlayground.setPixelColor(5, LEFT_COLOR); CircuitPlayground.setPixelColor(4, LEFT_COLOR); delay(ANIM_DELAY); CircuitPlayground.clearPixels(); // Turn on 6 & 3 CircuitPlayground.setPixelColor(6, LEFT_COLOR); CircuitPlayground.setPixelColor(3, LEFT_COLOR); delay(ANIM_DELAY); CircuitPlayground.clearPixels(); // Turn on 7 & 2 CircuitPlayground.setPixelColor(7, LEFT_COLOR); CircuitPlayground.setPixelColor(2, LEFT_COLOR); delay(ANIM_DELAY); CircuitPlayground.clearPixels(); // Turn on 8 & 1 CircuitPlayground.setPixelColor(8, LEFT_COLOR); CircuitPlayground.setPixelColor(1, LEFT_COLOR); delay(ANIM_DELAY); CircuitPlayground.clearPixels(); // Turn on 9 & 0 CircuitPlayground.setPixelColor(9, LEFT_COLOR); CircuitPlayground.setPixelColor(0, LEFT_COLOR); delay(ANIM_DELAY); CircuitPlayground.clearPixels(); } /////////////////////////////////////////////////////////////////////////////// void rightTurnAnimation() { // just to be sure, turn off all NeoPixels CircuitPlayground.clearPixels(); // turn on NeoPixels to make an arrow shape CircuitPlayground.setPixelColor(2, RIGHT_COLOR); CircuitPlayground.setPixelColor(5, RIGHT_COLOR); CircuitPlayground.setPixelColor(6, RIGHT_COLOR); CircuitPlayground.setPixelColor(7, RIGHT_COLOR); CircuitPlayground.setPixelColor(8, RIGHT_COLOR); CircuitPlayground.setPixelColor(9, RIGHT_COLOR); // wait a little bit delay(ANIM_DELAY); // turn them all off CircuitPlayground.clearPixels(); // wait again delay(ANIM_DELAY); } /////////////////////////////////////////////////////////////////////////////// void setup() { CircuitPlayground.begin(BRIGHTNESS); } /////////////////////////////////////////////////////////////////////////////// void loop() { // check slide switch position if (CircuitPlayground.slideSwitch()) { // check for hand up or down if (CircuitPlayground.motionZ() > THRESHOLD_UP) { // do nothing } else { // check for right turn if (CircuitPlayground.motionX() > THRESHOLD_RIGHT) { rightTurnAnimation(); // check for left turn } else if (CircuitPlayground.motionY() < THRESHOLD_LEFT) { leftTurnAnimation(); } } } }
"ON/OFF" Switch
You may have noticed one extra feature added to this final code. Within the loop()
function, everything is wrapped in an if
statement that checks the position of the slide switch using CircuitPlayground.slideSwitch()
. Like this:
void loop() { // check slide switch position if (CircuitPlayground.slideSwitch()) { // MAIN CODE HERE } }
This was done to provide a simple on/off capability. If the Circuit Playground had an on/off switch for power, then we could just use that and turn it off for real. But since it doesn't we can use the slide switch. The Circuit Playground is still powered, but the code that makes the bike glove work is only run if the slide switch is in the + position.
Therefore:
Maybe you want to stop and pet a cute kitty you see, but don't want to freak it out with flashing lights. So you can just disable the lights real quick by sliding the switch to - position. Slide bike to + position when you're ready to go.
Happy riding!
Page last edited June 03, 2017
Text editor powered by tinymce.