Switching from RGB led to NeoPixel in Arduino code

Hi,

I have a bit of a problem and I need some help, so hopefully someone can aid me.

I have this code written for a simple RGB light which fades from yellow to red when reed switch is off and produces continuous yellow light when reed switch is on. However, I need to have the same effect with NeoPixel leds (only 2) and I can't figure out how to adjust my code, so that it works smoothly. Could someone help me with that?

int ledPinR = 11;
int ledPinG = 10;
int ledPinB = 9;
int reedPin = 2;


int brightness1 = 0;
int brightness2 = 0;
int fadeAmount = 5;
unsigned long timestamp = 0;

void setup(){
    pinMode(ledPinR, OUTPUT);
    pinMode(ledPinG, OUTPUT);
    pinMode(ledPinB, OUTPUT);
    pinMode(reedPin, INPUT_PULLUP);
}

void loop(){
    if(millis()-timestamp > 100){
        brightness1 = brightness1 - fadeAmount;
        brightness2 = brightness2 - fadeAmount;
        if(brightness1 < 0) brightness1 = 0;
        if(brightness2 < 0) brightness2 = 0;
        timestamp = millis();
    }

    if(!digitalRead(reedPin)){
        brightness1 = 255;
        brightness2 = 155;
    }

    analogWrite(ledPinG, brightness1);
    analogWrite(ledPinR, brightness2);
}

What about Google on "Arduino + NeoPixel" ?

For only two puxels, you probably don’t need to use WS28xx LED pixels.

A couple of decent RGB LEDs driven the same way you already do will probably be more than adequate, and give more flexibility with the cnoice or LED.

Install Adafruits Neopixel Library.
Start with the example "Simple".
Make your two Neopixels work with the "Simple" sketch.

And then, compare the line

    analogWrite(ledPinG, brightness1);
    analogWrite(ledPinR, brightness2);

with

pixels.setPixelColor(i, pixels.Color(0, 150, 0));

I did that, but it does not fade out then, only produces one color, because it is not a variable. Thank you for your answer tho!

I looked through all the forums, and did find similar codes, but none of them fits what I need or are way too complicated for my beginners knowledge...

thats fine you only got one color as you where only writing one color.

pixels.setPixelColor(i, pixels.Color(0, 150, 0));

now add the logic where you increase the green color in a variable and use that color:

pixels.setPixelColor(i, pixels.Color(0, green, 0));

Your still need to use your brightness1 and brightness2 variables!

If you can't make it work, post your updated code.

I specialize in knowing the least I need to know.

neopixels are very useful, and super easy to use. There are many wonderful things you can do with them, but getting them going is easy:

// you have to include the library
# include <Adafruit_NeoPixel.h>

// and where the strip is hooked up on the Arduino
# define PIXEL_PIN    6

// how many pixels on the strip?
# define N_LEDS 20

// make the neopixel object.
// here I use NEO_GRB + NEO_KHZ800.  that's typical, but must match the strip you have
// it is called myStrip. Most just call it "strip"

Adafruit_NeoPixel myStrip(N_LEDS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
// in your setup, make one call to the begin method

  myStrip.begin();
}

void loop() {
// in your code, change any pixel's color
// pixel number: NN from 0 to the end of the strip (NLEDS - 1) in this example 0..19
// red level: redBrightness  0 off to - 255 full brightness red
// green level: greenBrightness
// red level: blueBrightness

  int NN = 4;                 // some pixel
  byte redBrightness = 128;   // half bright red +
  byte greenBrightness = 0;   // no green +
  byte blueBrightness = 255;  // full bright blue

  myStrip.setPixelColor(NN, redBrightness,  greenBrightness,  blueBrightness);

// when you have set all the pixel colors, the call to show will update the physical strip:

    myStrip.show();
}

I hope it is obvious that you can declare your variables like any normal variables and set them to get the pixel you want to be the color/brightness you need.

The strip should be connected with a series resistor and have a capacitor on its power leads, see

neopixel hardware best practices

I plucked this page from the uber-guide, the whole thing is good reading, but this is... the least you need to know and more than enough to get into some serious trouble fun.

Try it here: wokwi simulation of minimum neopixel sketch

It's a long way to go to make one purple pixel, I will admit. But once you know it's mostly just setPixelColor and show, you can be off and running.

HTH

a7

Hi!

Thank you for your answer. It does produce the color I want, however I still do not know how to crossfade them and make them react to reed sensor.
Do you have knowledge in that?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.