1 moving pixel rainbow

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

Do you want the rainbow cycle to be controlled by the accelerometer or by time?

By time, just like the usual rainbow cycle,

Ok, well, you are correct to say that you need to call the Wheel() function to get the rainbow colours. But you can't do it in the same way as it's done in the rainbow() function. The rainbow() function has loops inside loops and uses delay(). You can't have that because it would stop the sketch updating the led according to the accelerometer.

The Wheel() function takes a parameter which is a number between 0 and 255, those numbers representing a colour position on the "rainbow wheel of colours". You will need a byte variable to keep track of where you are in the wheel. That variable needs to be a global variable, declared at the top of the sketch, so that it's value does not get forgotten each time loop() finishes executing. Then you just give that variable to Wheel() when you update the lit led based on the accelerometer reading.

You then need to update your LEDs position in the wheel over time. Use millis() to do this. Each time a few millis() have passed, add one to the position. Because it is a byte variable, when it exceeds 255, it will set itself back to zero automatically.

To know when several millis() have passed, you need another global variable. This one remembers the time that the colour was last changed. This must be an unsigned long.