Go Down

Topic: Using Multiple Adafruit LEDs  (Read 809 times) previous topic - next topic

CrisBen1211

Hello, I am trying to use multiple adafruit RGB leds displaying different colors
I am new to coding and unsure how to add the other pins. I tried adding ledPin1 and ledPin2 to the Adafruit_NeoPixel pixels  list in () however that comes as an error.
could someone please explain how can i add the other two pins?

Eventually id would also like to make these leds pulsate instead of just turning on and off.

Here is the code i have so far...

Code: [Select]

#include <Adafruit_NeoPixel.h>
const int pResistor = A0; //Sensor
int value;  //SensorVal (300-800 or 0-1023)
int ledPin = 3;
int ledPin1 = 5;
int ledPin2 = 6;
int NUMPIXELS = 1;
int val;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


void setup() {
  Serial.begin(9600);
 
  pinMode(pResistor, INPUT);
  pinMode(ledPin, OUTPUT);

 pixels.begin();

}

void loop() {
 

   val =  analogRead(A0);

  Serial.println(val);
  //Serial.write(val);

pixels.setPixelColor(0, pixels.Color(150,0,150));
pixels.setPixelColor(ledPin1, pixels.Color(150,0,150));
pixels.setPixelColor(ledPin2, pixels.Color(150,0,150));

pixels.show();

if (val < 520){
    digitalWrite(ledPin, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin, HIGH); //Turn led on
  }

  delay(200); //Small delay
}


Thank you for reading and if you have any advice or instruction i would greatly appreciate the help.

larryd

#1
Dec 15, 2019, 12:07 am Last Edit: Dec 15, 2019, 12:13 am by larryd
Your pixels should be connected in series Dout to Din.
First Pixel Din connects to ledPin

Code: [Select]


#include <Adafruit_NeoPixel.h>
const int pResistor = A0; //Sensor
int value;  //SensorVal (300-800 or 0-1023)
int ledPin = 3;
int NUMPIXELS = 3;
int val;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


void setup()
{
  Serial.begin(9600);

  pinMode(pResistor, INPUT);

  pixels.begin();

  pixels.setPixelColor(0, pixels.Color(0, 0, 0));
  pixels.setPixelColor(1, pixels.Color(0, 0, 0));
  pixels.setPixelColor(2, pixels.Color(0, 0, 0));
  pixels.show();
}

void loop()
{
  val =  analogRead(A0);

  Serial.println(val);
  //Serial.write(val);

  if (val < 520)
  {
    pixels.setPixelColor(0, pixels.Color(0, 0, 0));
    pixels.setPixelColor(1, pixels.Color(0, 0, 0));
    pixels.setPixelColor(2, pixels.Color(0, 0, 0));
    pixels.show();
  }

  else
  {
    pixels.setPixelColor(0, pixels.Color(150, 0, 150));
    pixels.setPixelColor(1, pixels.Color(150, 0, 150));
    pixels.setPixelColor(2, pixels.Color(150, 0, 150));
    pixels.show();
  }

  delay(200); //Small delay
}






No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

CrisBen1211

larryd Thank you for responding, I forgot to say they are three individual neopixels so i have three separate pins. so i want to set each pin to a different colors. I had added the ints but i tried to add them to the...

Code: [Select]

int ledPin = 3;
int ledPin1 = 5;
int ledPin2 = 6;


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


...part of the code but it did not work.


thank you again!

larryd

#3
Dec 15, 2019, 01:52 am Last Edit: Dec 15, 2019, 01:59 am by larryd
You need to make '3' objects then.

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(NUMPIXELS, ledPin1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2= Adafruit_NeoPixel(NUMPIXELS, ledPin2, NEO_GRB + NEO_KHZ800);

. . .

pixels.begin();
pixels1.begin();
pixels2.begin();

. . .

pixels.setPixelColor(0, pixels.Color(150,0,150));
pixels1.setPixelColor(0, pixels.Color(150,0,150));
pixels2.setPixelColor(0, pixels.Color(150,0,150));
 . . .

pixels.show();
pixels1.show();
pixels2.show();
etc.


But, why not just place your pixels in series and use one instance?








No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

pert

To add to what larryd said, the Adafruit Neopixels are addressable. That means you can have a string of them connected to a single pin of your Arduino and individually control the color of each LED on the string. The address of the first LED on the string is 0, the address of the second is 1, and so on. It doesn't matter whether you have them on a strip or individual LEDs wired in series.

I think you will find that having them on a single pin makes the programming easier, as well as saving two pins on your Arduino for other uses. The knowledge you gain will also be more useful for future projects you might do with many LEDs where putting each one on a separate pin would not be feasible.

CrisBen1211

Ohhhhh, i didn't realize that. but when i did have the 0,1,2 in the code originally it made all three leds go through the different colors. I want them to each be a different color. would that still work?

Thank you for the advice!

On another note, I would like to make the three leds Pulse slowly and then quicker when triggered.
How can i do this? right now they just turn on and off based on the sensor value. I have looked at other tutorials but am confused as to how to apply this to my code. Thank you again for your guidance!

Code: [Select]
#include <Adafruit_NeoPixel.h>
const int pResistor = A0; //Sensor
int value;  //SensorVal (300-800 or 0-1023)
int ledPin = 3;
int ledPin1 = 5;
int ledPin2 = 6;
int NUMPIXELS = 3;
int val;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(NUMPIXELS, ledPin1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2= Adafruit_NeoPixel(NUMPIXELS, ledPin2, NEO_GRB + NEO_KHZ800);

void setup()
{
  Serial.begin(9600);

  pinMode(pResistor, INPUT);

  pixels.begin();
  pixels1.begin();
  pixels2.begin();

  pixels.setPixelColor(0, pixels.Color(0, 0, 0));
  pixels1.setPixelColor(1, pixels.Color(0, 0, 0));
  pixels2.setPixelColor(2, pixels.Color(0, 0, 0));
  pixels.show();
  pixels1.show();
  pixels2.show();
 
}

void loop()
{
  val =  analogRead(A0);

  Serial.println(val);
  //Serial.write(val);

  if (val < 520)
  {
    pixels.setPixelColor(0, pixels.Color(0, 0, 0));
    pixels1.setPixelColor(0, pixels.Color(0, 0, 0));
    pixels2.setPixelColor(0, pixels.Color(0, 0, 0));
    pixels.show();
    pixels1.show();
    pixels2.show();
  }

  else
  {
    pixels.setPixelColor(0, pixels.Color(150, 0, 150));
    pixels1.setPixelColor(0, pixels.Color(150, 0, 0));
    pixels2.setPixelColor(0, pixels.Color(0, 150, 0));
    pixels.show();
    pixels1.show();
    pixels2.show();
  }

  delay(200); //Small delay
}

larryd

#6
Dec 15, 2019, 05:50 pm Last Edit: Dec 15, 2019, 07:30 pm by larryd
" want them to each be a different color. would that still work? "

Yes






Define pulse?

If this means dim then brighten, you need the use a range of succeeding colour values.

example (150, 0, 150) >>>---->  (100, 0, 100) >>>---->  (50, 0, 50)  from bright to dim.

More values for smother control.

Use variables rather  than numbers and do some math stuff.

(intensity- -, 0, intensity- -) subtracts 1 from intensity each time through, etc.


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

CrisBen1211

Re:define pulse

I would like for the LEDs to blink on and off at a steady (calm) pace then if (val < 520) I want them to speed up blinking faster.

I was looking at this tutorial https://www.sparkfun.com/tutorials/329 but i'm not sure how to apply what i would like to do here into my code.



CrisBen1211

update:

I was able to apply a simple way of blinking the leds however when the ledPin blinks it changes from purple to red.

Code: [Select]
#include <Adafruit_NeoPixel.h>
const int pResistor = A0; //Sensor
int value;  //SensorVal (300-800 or 0-1023)
int ledPin = 3;
int ledPin1 = 5;
int ledPin2 = 6;
int NUMPIXELS = 3;
int val;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(NUMPIXELS, ledPin1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2= Adafruit_NeoPixel(NUMPIXELS, ledPin2, NEO_GRB + NEO_KHZ800);

void setup()
{
  Serial.begin(9600);

  pinMode(pResistor, INPUT);
  pinMode(ledPin,OUTPUT);

  pixels.begin();
  pixels1.begin();
  pixels2.begin();

  pixels.setPixelColor(0, pixels.Color(0, 0, 0));
  pixels1.setPixelColor(1, pixels.Color(0, 0, 0));
  pixels2.setPixelColor(2, pixels.Color(0, 0, 0));
  pixels.show();
  pixels1.show();
  pixels2.show();
 
}

void loop()
{
  val =  analogRead(A0);

  Serial.println(val);
  //Serial.write(val);

  if (val < 520)
  {
    pixels.setPixelColor(0, pixels.Color(150, 0, 150));
    pixels1.setPixelColor(0, pixels.Color(150, 0, 0));
    pixels2.setPixelColor(0, pixels.Color(0, 150, 0));
    pixels.show();
    pixels1.show();
    pixels2.show();
    digitalWrite (ledPin, HIGH);
    delay(100);
    digitalWrite (ledPin, LOW);
    delay(100);
    digitalWrite (ledPin1, HIGH);
    delay(100);
    digitalWrite (ledPin1, LOW);
    delay(100);
    digitalWrite (ledPin2, HIGH);
    delay(100);
    digitalWrite (ledPin2, LOW);
    delay(100);
  }

  else
  {
    pixels.setPixelColor(0, pixels.Color(150, 0, 150));
    pixels1.setPixelColor(0, pixels.Color(150, 0, 0));
    pixels2.setPixelColor(0, pixels.Color(0, 150, 0));
    pixels.show();
    pixels1.show();
    pixels2.show();
    digitalWrite (ledPin, HIGH);
    delay(800);
    digitalWrite (ledPin, LOW);
    delay(800);
     digitalWrite (ledPin1, HIGH);
    delay(800);
    digitalWrite (ledPin1, LOW);
    delay(800);
     digitalWrite (ledPin2, HIGH);
    delay(800);
    digitalWrite (ledPin2, LOW);
    delay(800);
  }

  delay(200); //Small delay
}

larryd

#9
Dec 15, 2019, 08:34 pm Last Edit: Dec 17, 2019, 07:23 pm by larryd
Use this schematic:





Then try this sketch:

Code: [Select]

//Version 1.04
//https://forum.arduino.cc/index.php?topic=653764.0

#include <Adafruit_NeoPixel.h>

const byte pResistor = A0; //Sensor
const byte fast      = 5;
const byte slow      = fast * 4;
const byte ledPin    = 3;
const byte NUMPIXELS = 3;

byte mState;

int change           = 1;
int val;
int intensity;

//timer stuff
unsigned long LEDmillis;


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

//***********************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(pResistor, INPUT);

  pixels.begin();

  //turn off all pixels
  mState = 0;
  sMachine();

} //END of setup()

//***********************************************************************
void loop()
{
  val =  analogRead(A0);
//  Serial.println(val);

  //********************
  if (val < 520)
  {
    //set to slow
    mState = 2;
  }

  else
  {
    //set to fast
    mState = 1;
  }

  //update the state machine
  sMachine();
  //********************

} //END of loop()


//***********************************************************************
void sMachine()
{
  switch (mState)
  {
    //********************
    //turn off pixels
    case 0:

      pixels.setPixelColor(0, pixels.Color(0, 0, 0));
      pixels.setPixelColor(1, pixels.Color(0, 0, 0));
      pixels.setPixelColor(2, pixels.Color(0, 0, 0));
      pixels.show();

      break;
    //END case 0:
    //LEDs off

    //********************
    //fast pulsing
    case 1:
      if (millis() - LEDmillis >= fast)
      {
        //reset timer
        LEDmillis = millis();

        pixels.setPixelColor(0, pixels.Color(intensity, 0, 0));
        pixels.setPixelColor(1, pixels.Color(0, intensity, 0));
        pixels.setPixelColor(2, pixels.Color(0, 0, intensity));
        pixels.show();

        intensity = intensity + change;

        //255 is too bright ;-)
        if (intensity > 64)
        {
          //go the other way
          //change = ~change + 1;
          change = change * -1;
          intensity = 64;
        }

        else if (intensity < 0)
        {
          //go the other way
          //change = ~change + 1;
          change = change * -1;
          intensity = 0;
        }

      } //END if (millis() - LEDmillis >= fast)

      break;
    //END case 1:
    //fast

    //********************
    //slow pulsing
    case 2:
      if (millis() - LEDmillis >= slow)
      {
        //reset timer
        LEDmillis = millis();

        pixels.setPixelColor(0, pixels.Color(intensity, 0, 0));
        pixels.setPixelColor(1, pixels.Color(0, intensity, 0));
        pixels.setPixelColor(2, pixels.Color(0, 0, intensity));
        pixels.show();

        intensity = intensity + change;

        //255 is too bright ;-)
        if (intensity > 64)
        {
          //go the other way
          //change = ~change + 1;
          change = change * -1;
          intensity = 64;
        }

        else if (intensity < 0)
        {
          //go the other way
          //change = ~change + 1;
          change = change * -1;
          intensity = 0;
        }

      } //END if (millis() - LEDmillis >= slow)

      break;
      //END case 2:
      //slow

      //********************

  } //END of switch/case

} //END of sMachine()

//***********************************************************************




EDIT
Updated code to version 1.04 and schematic

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Grumpy_Mike

Code: [Select]
digitalWrite (ledPin, HIGH);
    delay(100);
    digitalWrite (ledPin, LOW);
    delay(100);
    digitalWrite (ledPin1, HIGH);
    delay(100);
    digitalWrite (ledPin1, LOW);
    delay(100);
    digitalWrite (ledPin2, HIGH);
    delay(100);
    digitalWrite (ledPin2, LOW);
    delay(100);

What is all that about?
You do not do this when driving these LEDs, the only interaction should be via the show method.
Cut it out.

Go Up