Before You Start

If this is your first foray into the world of Arduino-based microcontrollers, you'll need to install some software first. Head over to the Circuit Playground Lesson 0 guide for detailed installation and setup instructions.  

You'll only need to do all this once!

TiCo Servo Library

You will also need to install the TiCoServo library in Arduino (Sketch > Include Library > Manage Libraries...)

This library by Phil Burgess makes it possible to use servos and NeoPixel LEDs at the same time, driven from one Arduino board. Head over to the TiCoServo LIbrary guide to learn more about why we need this, and how it works.

Upload Code

Once you've got everything installed and your computer can talk to the Circuit Playground, it's time to upload the code.

Plug your Circuit Playground into your computer and select the Circuit Plaground under Tools > Boards.  Then select the Circuit Playground as the Port.

Copy and paste this code into a new Arduino window and click "upload".

/*------------------------------------------------------------------------
  Unicorn Hat sketch for Circuit Playground
  Reads a potentiometer on pin 10, moves servos (pin 9) 
  ------------------------------------------------------------------------*/

#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_TiCoServo.h>

// Pin number for joystick. Only the "right side" pads work w/analog!
// And the numbering is unusual: for the pads labeled 12, 6, 9 and 10,
// set CONTROL_PIN to 11, 7, 9 or 10, respectively.
#define CONTROL_PIN 10

// Servo parameters. On Circuit Playground, only pins 9 and 10 are
// supported by the TiCoServo library.
// Servo position can be specified in degrees or in microseconds; library
// can distinguish between the two. The #defines below are reasonable
// min/max pulse durations (in microseconds) for many servos, but for
// maximum control you'll probably need to do some calibration to find
// the optimal range for your specific servos.
#define SERVO_PIN    9
#define SERVO_MIN 1000 // 1 ms pulse
#define SERVO_MAX 2000 // 2 ms pulse

// set currentSpeed to 0-4 to change the horn animation speed
static int speeds[] = { 5, 10, 50, 100 };
int currentSpeed = 1;

Adafruit_TiCoServo servo;

void setup(void) {
  servo.attach(SERVO_PIN, SERVO_MIN, SERVO_MAX);
  CircuitPlayground.begin(); 
  // Make it bright!
  CircuitPlayground.setBrightness(255);
}

void loop()  {

  // Servo control
  int a, x;
  a = analogRead(CONTROL_PIN);                  // 0 to 1023
  x = map(a, 0, 1023, SERVO_MIN, SERVO_MAX);    // Scale to servo range
  servo.write(x);                               // Move servo

  // Horn light control.  Make an offset based on the current millisecond count scaled by the current speed.
  uint32_t offset = millis() / speeds[currentSpeed];
  // Loop through each pixel and set it to an incremental color wheel value.
  for(int i=0; i<10; ++i) {
    CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(((i * 256 / 10) + offset) & 255));
  }
  // Show all the pixels.
  CircuitPlayground.strip.show();
}

If all goes well, the lights on the Circuit Playground will come on in a rainbow cycle pattern.  You can change the speed of the LED animation from within the code.  Look for this line:

// set currentSpeed to 0-4 to change the horn animation speed
static int speeds[] = { 5, 10, 50, 100 };
int currentSpeed = 1;

Change currentSpeed to a number between 0-4, and watch for the difference in the neopixels.

You may also need to calibrate your servo response time.  Look for these lines:

#define SERVO_MIN 1000 // 1 ms pulse
#define SERVO_MAX 2000 // 2 ms pulse

Servo position can be specified in degrees or in microseconds.  The TiCoServo library can distinguish between the two. The #defines in the code are reasonable min/max pulse durations (in microseconds) for many servos, but for maximum control you may need to do some calibration to find the optimal range for your specific servos.  

The code as-written works great for the micro servos recommended in this guide, but play with these numbers once you've got everything hooked up to change the range of motion for your ears.

This guide was first published on Jun 16, 2017. It was last updated on Jun 16, 2017.

This page (Code) was last updated on Sep 29, 2021.

Text editor powered by tinymce.