DaddyOh's Blog

Making, Cooking, Living

NeoPixel Test on Mbed

| Comments

I will be using several of these 8 LED NeoPixel Sticks from Adafruit in a Persistence of Vision (POV) project soon. The target POV MicroController I will probably use is based on the ARM mbed platform.

So thought I would test NeoPixel Stick on a NXP LPC1768 and if that goes well then a mBuino platform which is the desired target device for my POV.

I will add a blog pos url here soon.

The project uses this NeoPixel library by Jacob Bramley. This library uses the SPI bus for controlling the timing and the author states that this library should support interrupt driven code (with some limitations). The POV prototypes I will create will be interrupt driven once per rotation so I figured this would be a good library to test.

The NeoPixel Stick have 4 connections 2 GNDs, POWER (+5V) and one for the control signal. The control signal is plugged into P5 on the LPC1768, and we are taking power off of the VU pin (5.0v USB out). I have also tested this powering the stick from an external power supply. Both works nearly flawlessly.

Initially I had a problem with the stick showing colors and lighting LEDs that I did not program in to the code. The stick had long 9 inch leads between the mbed and the stick. If I touched these leads the stick would act erratically. I assume this was due to the high speed communications that the stick depends ons for its control signals. Once I shortened the leads the stick performed great.

main.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 
License: Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.html
Copyright: Eric F Palmer, 2014
Email: eric [at] ericfpalmer [dot] [com]
 */

#include "mbed.h"
#include "neopixel.h"

// This must be an SPI MOSI pin.
#define DATA_PIN p5

// this program has the number of LEDs fixed at 8 for the Adafruit NeoPixel Stick

void setPixel(neopixel::Pixel * buffer, uint32_t index, uint8_t red, uint8_t green, uint8_t blue, uint32_t length)
{
    if (index >= length ) {
        return;
    }
    buffer[index].red = red;
    buffer[index].green = green;
    buffer[index].blue = blue;
}

void brightnessTest(neopixel::PixelArray array, neopixel::Pixel * buffer)
{
    // step through three colors with the same brigtness
    uint32_t i;
    for (i = 0x00; i<= 0xC8; i = i + 0x0A) {
        setPixel(buffer, 0, i, 0x00, 0x00, 8);
        setPixel(buffer, 2, 0x00, i, 0x00, 8);
        setPixel(buffer, 4, 0x00, 0x00, i, 8);
        // clear the rest
        setPixel(buffer, 1, 0x00, 0x00, 0x00, 8);
        setPixel(buffer, 3, 0x00, 0x00, 0x00, 8);
        setPixel(buffer, 5, 0x00, 0x00, 0x00, 8);
        setPixel(buffer, 6, 0x00, 0x00, 0x00, 8);
        setPixel(buffer, 7, 0x00, 0x00, 0x00, 8);
        array.update(buffer, 8);
        wait_ms(250);
    }
}

// not tested yet, use at own risk
void clearAll(neopixel::PixelArray array, neopixel::Pixel * buffer, uint32_t length)
{
    uint32_t i;
    for (i = 0; i< length; i = i +1) {
        setPixel(buffer, i, 0x00, 0x00, 0x00, length);
    }
}

int main()
{

    // Create a temporary DigitalIn so we can configure the pull-down resistor.
    // (The mbed API doesn't provide any other way to do this.)
    // An alternative is to connect an external pull-down resistor.
    DigitalIn(DATA_PIN, PullDown);

    // The pixel array control class.
    neopixel::PixelArray array(DATA_PIN);

    // 8 is the number of pixels
    uint32_t numLeds = 8;
    neopixel::Pixel buffer[numLeds];
    int waitMs = 1000;

    /*
     * control the colors and brightness by turing on (or off) red, gree, blue
     * values should be hex 0x00 (off) to 0xFF full on for each color
     *
     * these LEDs are very bright and don't need to be driven much to get
     * sufficient light out of then 
     *
     * achieve other colors by mixing RGB
     *
     * this code is not pretty but it works
     */

    while (1) {

        // Turn on blue for the 4th LED
        // All LEDs off
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x00, 0x00, 0x0, numLeds);
        }
        setPixel(buffer, 3, 0x00, 0x00, 0x12, numLeds);
        array.update(buffer, numLeds);
        wait_ms(waitMs);

        // All LEDs white
        // All LEDs off
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x00, 0x00, 0x0, numLeds);
        }
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x12, 0x12, 0x12, numLeds);
        }
        array.update(buffer, numLeds);
        wait_ms(waitMs);

        // All LEDs yellow
        // All LEDs off
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x00, 0x00, 0x0, numLeds);
        }
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x23, 0x23, 0x00, numLeds);
        }
        array.update(buffer, numLeds);
        wait_ms(waitMs);

        // All LEDs off
        for (int i = 0; i < numLeds; ++i) {
            setPixel(buffer, i,0x00, 0x00, 0x0, numLeds);
        }
        array.update(buffer, numLeds);
        wait_ms(waitMs);
    }
}

Please leave comments if you find this useful.

POV blog posts

Comments