Code the Light Paintbrush

Before you get started, make sure you've been through the basic tutorials on using the Circuit Playground. You'll need to have installed the Arduino IDE, added the Circuit Playground board to Arduino, and added the Circuit Playground library. This excellent guide, Introducing Circuit Playground and Circuit Playground lesson #0 will show you how to do all of that and get you going in no time!

If you are using the Circuit Playground Express, be sure to check out the next page of the guide to see how to program the Light Paintbrush using MakeCode

Plug your Circuit Playground into your computer now using a USB cable, and launch the Arduino IDE. Double check that you've selected the board and port as in the Lesson #0 tutorial and are ready to upload code.

The code for the Light Paintbrush has two jobs:

  • Read the on-board accelerometer to determine which color to set the NeoPixels
  • Read the potentiometer connected to pin #10 to set the NeoPixel brightness
Download: file
// ---------------------------------------------------------------------------
//Circuit Playground Light Paintbrush
// John Park
// for Adafruit Industries
//
// Use the Circuit Playground as a long exposure photography light paintbrush
// tilt to change color
// adjust brightness with the 10k potentiometer
//
 /*      +X 
 *       __
 *      /  \  
 * +Y  |    |  -Y
 *      \__/
 *      
 *       -X
 */
//
// MIT license, check LICENSE for more information
//
//
// ---------------------------------------------------------------------------

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

int sensorPin = A10;
int sensorValue = 0;
int brightVal = (20);

int X;
int Y;

int red = 0;
int green = 255;
int blue = 0;

void setup() {
  CircuitPlayground.begin();
  CircuitPlayground.clearPixels();
  CircuitPlayground.setBrightness(brightVal);  
}

void loop() {
  sensorValue = analogRead(sensorPin);
  brightVal = map(sensorValue, 0, 1023, 0, 225);

  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();

  if ( (X>=1) && (Y<1) ) {      //+x, -y = GREEN
    red = 0 ;
    green = 255;
    blue = 0;
  }
  else if ( (X <1 ) && (Y<1) ){ //-x, -y = BLUE
    red = 0;
    green = 0;
    blue = 255;
  }
  else if ( (X >=1) && (Y>=1) ){ //+x, +y = RED
    red = 255;
    green = 0;
    blue = 0;
  }
  else if ( (X <1) && (Y>=1) ){  //-x, +y = YELLOW
    red = 255;
    green = 255;
    blue = 0;
  }

  for (int i=0; i<10; ++i) {
    CircuitPlayground.setBrightness(brightVal);
    CircuitPlayground.strip.setPixelColor(i, red, green, blue);
  }   
  CircuitPlayground.strip.show();
}

Download the Code

You can press the button below to download the code. Unzip and move the directory CircuitPlaygroundLightPaintbrush to your Arduino sketch directory, then open it in the Arduino IDE and upload it to your Circuit Playground.

Next you'll assemble the Light Paintbrush.

This guide was first published on Nov 09, 2016. It was last updated on Nov 09, 2016. This page (Code the Light Paintbrush) was last updated on Oct 15, 2019.