Neopixel – Using a thumbstick to control direction

A random test using a thumbstick to control the theaterchase direction and color of a 16 LED Neopixel

Components:
16 led neopixel from adafruit
Arduino Uno
Analog 2-axis Thumb Joystick

The hookup is simple, wire up Vin/GND for the neopixel and joystick, input to the neopixel is on pin 6 of the Uno. Analog pins 0 and 1 connect to the thumbstick. As the thumbstick is moved vertically the analog value is read, compared to a value somewhere in the middle, if higher it sets direction one way, lower is the reverse. I didn’t get it exact so it will always move in one direction until the joystick is moved the other direction.

strip.setPixelColor(j,pixColor);
This uses the horizontal thumbstick value, which is set to a number between 0-255, and sends that to the Wheel function from the strandtest sketch in the Neopixel library, this returns an RGB color.

np - Copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <Adafruit_NeoPixel.h>

#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

const int VERT = 0;    //Thumbstick connected to the Analog pin 0 on the Uno
const int HORIZ = 1;   //Thumbstick connected to the Analog pin 1 on the Uno
int pixColor;
int pixVal;
void setup() {  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

}

void loop() {
  strip.setBrightness(20);  //sets the pixel brightness

  int vertVal, horizVal;  
  vertVal = analogRead(VERT);   //Reads the thumbstick, controls direction of pixels
  horizVal = analogRead(HORIZ);  //Controls the color

  pixVal=horizVal/2.7;  //Thumbstick values went to ~627, so this just makes sure its value is under 255
  if(pixVal>255){       //Just in case the value is > 255
    pixVal=255;
  }
  pixColor=Wheel(pixVal);   //Calls the Wheel function and sets the pixel color to the returned value
  if(vertVal>500){
     dirCounter();      //If the thumbstick value reads higher than 500, calls the dirCounter function, otherwise calls dirClock

  } else {

    dirClock();
  }
}


void dirCounter() {
    for(int j=0;j<=strip.numPixels();j++){  //strip.numPixels returns the # of leds in the neopixel
    strip.setPixelColor(j,pixColor);        //turns on each pixel, setting the color to the value returned from the Wheel function
    strip.show();
    delay(20);
    strip.setPixelColor(j,0);              //turns off the pixel before moving to the next
    strip.show();
  }
}

void dirClock() {
    for(int j=strip.numPixels();j>=0;j--){
    strip.setPixelColor(j,pixColor);
    strip.show();
    delay(20);
    strip.setPixelColor(j,0);
    strip.show();
  }
}

//Below function came from the strandtest sketch in the Neopixel library from Adafruit
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

You may also like...