Go Down

Topic: Custom Code with NeoPixels - Help! (Read 56 times) previous topic - next topic

RobHowdle

Hi all,

so just to get the formalities out of the way, total newb with Arduino so please forgive me if my code isn't in the correctly format or not the "standard" or anything. I am doing a custom lighting sketch for a robot head which consists of 3 NeoPixels.  I should add I'm using an Arduino Uno for this.

24 LED RGBW - strip_m
16 LED RGBW - strip_l
7 LED Jewel RGBW - strip_s

Now I have got the Ardafruit_Neopixel library and hopefully I can explain the problem for you:

What I'm Trying To Achieve:
Setup
All 3 NeoPixels to have the same start up sequence using the ColorWipe which has one colour going round the neopixel then is replaced with another colour etc.

Loop
strip_m (24 LED) to have a randomly colour changing effect.
strip_l, strip_s (16 LED & 7 LED Jewel) to have the same pulsing/ fading effect.

Both functions, so all 3 neopixels to be performing their functions at the same time.

What I currently have:
Setup
All 3 NeoPixels run the setup at the same time with the correct colours but the colours do not go round in the ring, all LED's light up, then change to the next color.

Loop
All the neopixels have the correct functions, the strip_s and strip_l both have the correct pulsing/ fading function applied to them and they both run at the same time. The strip_m has the correct colour changing function but the problem is that there is a small delay between them.

The strip_m will change a couple of times but in that time the other two pixels will stop and vice versa.

Hopefully that was laied out simply enough. Below is my code, please remember that it's probably laid our in the wrong format or something but for now it works perfectly fine minus the fact the functions wont perform at the same time.

Code: [Select]

#include <Adafruit_NeoPixel.h>
#define LED_PIN     9, 6, 5
#define LED_COUNT  9, 16, 24
#define BRIGHTNESS 255, 255, 255
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip_s = Adafruit_NeoPixel(7, 9, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel strip_l = Adafruit_NeoPixel(16, 6, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel strip_m = Adafruit_NeoPixel(24, 5, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:

void setup() { //The start up sequence is a colour wipe of Red, Orange, Yellow, White around all of the neopixels - setup only happens once so all colours go round the pixels once and then into the loop.

/*strip_l = Large Eye
  strip_s = Small Eye
  strip_m = Main Eye
*/

  //Startup Light Sequence
  strip_l.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip_l.show();            // Turn OFF all pixels ASAP
  strip_l.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  strip_m.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip_m.show();            // Turn OFF all pixels ASAP
  strip_m.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  strip_s.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip_s.show();            // Turn OFF all pixels ASAP
  strip_s.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  // Fill along the length of the strip in various colors...
 /*I think this is the problem!>>*/
  colorWipe_l(strip_l.Color(255,   0,   0)     , 50); // Red
  colorWipe_l(strip_l.Color(  255, 90,   0)     , 50); // Orange
  colorWipe_l(strip_l.Color(  255, 225,   0)     , 50); // Yellow
  colorWipe_l(strip_l.Color(  0, 0, 0,   255)     , 50); // White*/
}
void loop() {
  int now = millis();
  pulseWhite(5);
  mainEye();

}

//Color Wipe
//All Eye's Start up Sequence
void colorWipe_l(uint32_t color, int wait) { //Small Eye
  for(int i=0; i<strip_s.numPixels(); i++) { // For each pixel in strip...
    strip_s.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    delay(wait);                           //  Pause for a moment
  }
  for(int i=0; i<strip_l.numPixels(); i++){ //Large Eye
    strip_l.setPixelColor(i, color);
  }
    for(int i=0; i<strip_m.numPixels(); i++){ //Main Eye
    strip_m.setPixelColor(i, color);
    strip_s.show();
    strip_l.show();
    strip_m.show();
  }
}
//PulseWhite
void pulseWhite(uint8_t wait) {
  for(int j=0; j<256; j++) { // Ramp up from 0 to 255
    // Fill entire strip with white at gamma-corrected brightness level 'j':
    strip_l.fill(strip_l.Color(255, 0, 0,  j));
    strip_s.fill(strip_s.Color(255, 0, 0,  j));
    strip_l.show();
    strip_s.show();
    millis();
  }

  for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
    strip_l.fill(strip_l.Color(255, 90,0, j));
    strip_s.fill(strip_l.Color(255, 90,0, j));
    strip_l.show();
    strip_s.show();
    millis();
  }
}
//Main Eye Color Changing

void mainEye(){
int timer = random(500)+50; // between 1/5 second and 1 second
strip_m.setPixelColor(0,0,0,255,0);
strip_m.setPixelColor(1,255,255,255,0);
strip_m.setPixelColor(2,0,0,255,0);
strip_m.setPixelColor(3,255,255,255,0);
strip_m.setPixelColor(4,0,0,255,0);
strip_m.setPixelColor(5,255,255,255,0);
strip_m.setPixelColor(6,0,0,255,0);
strip_m.setPixelColor(7,255,255,255,0);
strip_m.setPixelColor(8,0,0,255,0);
strip_m.setPixelColor(9,255,255,255,0);
strip_m.setPixelColor(10,0,0,255,0);
strip_m.setPixelColor(11,255,255,255,0);
strip_m.setPixelColor(12,0,0,255,0);
strip_m.setPixelColor(13,255,255,255,0);
strip_m.setPixelColor(14,0,0,255,0);
strip_m.setPixelColor(15,255,255,255,0);
strip_m.setPixelColor(16,0,0,255,0);
strip_m.setPixelColor(17,255,255,255,0);
strip_m.setPixelColor(18,0,0,255,0);
strip_m.setPixelColor(19,255,255,255,0);
strip_m.setPixelColor(20,0,0,255,0);
strip_m.setPixelColor(21,255,255,255,0);
strip_m.setPixelColor(22,0,0,255,0);
strip_m.setPixelColor(23,255,255,255,0);
delay(timer);

strip_m.show();

strip_m.setPixelColor(0,255,255,255,0);
strip_m.setPixelColor(1,0,0,255,0);
strip_m.setPixelColor(2,255,255,255,0);
strip_m.setPixelColor(3,0,0,255,0);
strip_m.setPixelColor(4,255,255,255,0);
strip_m.setPixelColor(5,0,0,255,0);
strip_m.setPixelColor(6,255,255,255,0);
strip_m.setPixelColor(7,0,0,255,0);
strip_m.setPixelColor(8,255,255,255,0);
strip_m.setPixelColor(9,0,0,255,0);
strip_m.setPixelColor(10,255,255,255,0);
strip_m.setPixelColor(11,0,0,255,0);
strip_m.setPixelColor(12,255,255,255,0);
strip_m.setPixelColor(13,0,0,255,0);
strip_m.setPixelColor(14,255,255,255,0);
strip_m.setPixelColor(15,0,0,255,0);
strip_m.setPixelColor(16,255,255,255,0);
strip_m.setPixelColor(17,0,0,255,0);
strip_m.setPixelColor(18,255,255,255,0);
strip_m.setPixelColor(19,0,0,255,0);
strip_m.setPixelColor(20,255,255,255,0);
strip_m.setPixelColor(21,0,0,255,0);
strip_m.setPixelColor(22,255,255,255,0);
strip_m.setPixelColor(23,0,0,255,0);

delay(timer);
strip_m.show();
}


The code for the pulseWhite function was actually taken from an example library RGBWStrandTest but I removed the parts that made it flash white as I couldn't find anything relating to fading the rings so this was the closest I could get.

Anyone who can help it would be much appreciated.

Thanks
Rob

Deva_Rishi

Pff you are quite a way of your goal, if you want different strips to do different things you should create a function for each strip (or one that uses a pointer to the strip object, but that may be extra complicated given that they are of different lengths) and use millis() for timing. read the post on the sticky at the top of the topic list.
Code: [Select]
millis();this is not the way, millis() returns a unsigned long of the milliseconds passed since starting the microprocessor.
Code: [Select]
#define LED_PIN     9, 6, 5
#define LED_COUNT  9, 16, 24
#define BRIGHTNESS 255, 255, 255
this is also a tad confusing, these keywords can only represent 1 value. (actually i am confused it compiles)
so when you call the colorwipe function you actually  set all strips in a Color, then during this there is a delay() and then you show them all at the same time. It is clear you are a beginner, and i want to help but i don't know where to start. Back to my earlier suggestion, first work out how to use millis() for timing then tune in again.
To 'Correct' you have to be Correct. (and not be condescending..)

taterking

you where very specific on what you have but its unclear what the problem is? this would be much easier to answer if you made this question more specific. what exactly is the problem? what is the answer you are looking for?
Building robots to take over the world.

Go Up