Hi,
I'm making a pixel moving on a 16neopixel matrice following the movements of an accelerometer.
That work well, but I want the pixel to change color like the rainbow cycle.
Here's what I have as code:
(Next time you post, please use the code tags button on the menu, </>. Thanks, Moderator)
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 6, NEO_GRB + NEO_KHZ800);
int initX, initY, cursorIndex;
#define bucket 25 //Range in accelerometer that coorelates to each coordinate value
void setup()
{
strip.begin();
strip.show(); //Start up the LED panel
analogReference(EXTERNAL); //Uses 3.3 volts as the maximum analog reading value
pinMode(A0, INPUT); //X axis
pinMode(A1, INPUT); //Y axis
initX = analogRead(A0); //Initial reading (while breadboard is flat)
initY = analogRead(A1); //Initial reading (while breadboard is flat)
}
void setNthPixel(int pixel, uint32_t color) //Maps the "snaking" LED matrix to the coordinate array
{
if(pixel <= 3)
strip.setPixelColor(pixel, color);
else if(pixel <= 7)
strip.setPixelColor(11-pixel, color); //Flips LEDs 4, 5, 6, 7 to be 7, 6, 5, 4
else if(pixel <= 11)
strip.setPixelColor(pixel, color);
else if(pixel <= 15)
strip.setPixelColor(27-pixel, color); //Flips LEDs 12, 13, 14, 15 to be 15, 14, 13, 12
}
int getCursorIndex()
{
int x, y, xOffset, yOffset;
xOffset = initX - 2 * bucket;
yOffset = initY - 2 * bucket;
x = (analogRead(A0) - xOffset)/bucket;
y = (analogRead(A1) - yOffset)/bucket;
Serial.println("X = ");
Serial.print(x);
if(x < 0 || x > 3 || y < 0 || y > 3) //If the coordinate is off the LED matrix, we'll set the value to -1
return -1;
else
return x*4+y; //Converts the X, Y coordinates into one number that correlates to the LED matrix
}
void loop()
{
cursorIndex = getCursorIndex();
for(int i = 0; i < 16; i++)
{
strip.setPixelColor(i, strip.Color(0,0,0)); //Clears the LED panel
}
if(cursorIndex != -1)
setNthPixel(cursorIndex, strip.Color(0, 20, 0)); //Show the cursor LED
strip.show();
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i*1+j) & 255));
}
strip.show();
delay(wait);
}
}
// 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);
}
}
I think I sould find a way to pass Wheel as an argument for the setNthPixel but I don't know how.
I don't think I need the rainbow fonction since it is for putting it in all the pixels
As you can see, I'm quite noob and english is not my first language
Thanks