Arduino Uno R3 - NeoPixel - Code Issue?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by adafruit_support_mike »

Bill was actually referring to CODE tags (fifth item from the left just above the text field where you compose a message).

It looks like he used the full tags though, and the forum software interpreted it as a code listing. I've modified your previous post, so you can use the edit button to see how the formatting works.

User avatar
ltodd2
 
Posts: 30
Joined: Mon Apr 06, 2015 12:14 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by ltodd2 »

Hi all. Just read this post and its a bit like what im after but im wanting the colours to change after the bounce and not stay one colour. The idea im after is like whats in the code below but this just goes from start to end then changes colour and repeat. I have tried mixing it all up a bit but failed. My understanding of some code is poor and C++ is a no go (yet)

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN    6
#define N_LEDS 32 // 4 meter reel

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
}

void loop() {
  pinMode(13, OUTPUT);
  strip.setBrightness(128);
  chase(strip.Color(255, 0, 0)); // Red
  chase(strip.Color(0, 255, 0)); // Green
  chase(strip.Color(0, 0, 255)); // Blue
}

static void chase(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels()+1; i++) {
      strip.setPixelColor(i  , c); // Draw new pixel
      strip.setPixelColor(i-3, 1); // Erase pixel a few steps back
      strip.show();
      delay(20);
  }
}
Thanks

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by adafruit_support_mike »

You'll want to change colors in here:

Code: Select all

  } else if(pos >= strip.numPixels()) {
    pos = strip.numPixels() - 2;
    dir = -dir;
  }

User avatar
ltodd2
 
Posts: 30
Joined: Mon Apr 06, 2015 12:14 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by ltodd2 »

Mike

Thanks for the reply. I've had a go at using what you put but am failing. my knowledge of programming is next to none but am trying to work on that. Where/how do I use this?
Thanks

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by adafruit_support_mike »

It's easiest to get familiar with things like NeoPixels by writing a simple program that lets you play with a few details without a bunch of other details in the way.

This has just enough code to set the color of the first pixel in a strip:

Code: Select all

#include "Adafruit_NeoPixel.h"

#define     NUMPIXELS   1
#define     PIN         6

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup () {
    pixel.begin();
    pixel.setPixelColor( 0, pixel.Color( 255, 0, 0 ));
    pixel.show();
}

void loop () {
}
Play around with the parameter values for pixel.Color(). The first value is the amount of red, the second is the amount of green, and the third is the amount of blue. The numbers can range between 0 and 255, with larger numbers being brighter and smaller ones being dimmer.

User avatar
ltodd2
 
Posts: 30
Joined: Mon Apr 06, 2015 12:14 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by ltodd2 »

Mike
Thanks for that. I have played with that already and get the workings of it. Its the more complex commands used to make it do something that I dont fully understand. For example the larson scanner on this site is a good example. That is what im wanting to have change colour after 1 cycle but need to know what the for ,int etc all do.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by adafruit_support_mike »

For that code, changing colors at the end of a sweep will be kind of tricky. It sets five pixels at a time, at different intensities to provide the fade.

The values for each pixel in the 5-pixel pattern are hardcoded, which gives you two problems: first, you have to choose a way to change a color while keeping the same intensity in the pattern. Second, you have to decide which pixels at the end of a sweep are still moving forward and which ones are coming back so you what color they should be.

That's not an easy problem to solve, and the solution will probably involve a lot of bookkeeping.

To build on the existing code, the first step is to get extremely familiar with how it works. That usually involves two things: first, if you don't know what a chunk of code does, put Serial.print() statements around it so at least you know when it happens and what the visible parts of the system are doing before, during, and after it runs. If necessary, add some delay() statements so you can see the operations happen slowly enoguh to understand them. Second, write code of your own to do what you think is happening, and compare that to the original code to see if they match.

There's no better way to understand code than to write a copy of it yourself. Even the simplest "type what's on the page rather than copy/pasting it" duplication will force you to look at every variable name and function call more carefully than you will just trying to read it.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by adafruit_support_bill »

There is a different implementation of a scanner here: https://learn.adafruit.com/multi-taskin ... -3/scanner
Changing colors at the end of a sweep with that one is fairly straightforward.

User avatar
ltodd2
 
Posts: 30
Joined: Mon Apr 06, 2015 12:14 pm

Re: Arduino Uno R3 - NeoPixel - Code Issue?

Post by ltodd2 »

Thanks guys for the help.
Bill, thanks for the link. I've had a quick look at it and tried it as is and seems to be what im after. Im going to have a good read of the doc and have a go at making it do what im wanting. I found some code called ripple that I like but could not get it to display for more than a fraction of a second but might have more luck in this.
Thanks again.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”