Program two pins to do the same thing....

Currently using a neo pixel ring for robot eyes. I am using an already built file from adafruit. (simple) What I am trying to do is make this program work on pin 6 and pin 8. I have tried modifying the code many times but for whatever reason I am not getting it. Can anyone help? Thanks

Chris

We should be able to give you some advice. .

So, where is it, that code?

And making 2 pins do the same is pretty useless most of the time. Why not simply connect the two things to the same pin?

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter–see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {

pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

for(int i=0;i<NUMPIXELS;i++){

// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

pixels.show(); // This sends the updated pixel color to the hardware.

delay(delayval); // Delay for a period of time (in milliseconds).

}
}

Please edit your post to sue code tags ;)

And yeay, you can duplicate everything but that's just a wast. Just connect both neo pixels to the same pin and your done...

Sorry can you explain how to use code tags?

Click the </> button, put your code in the box that appears.

Like this one.

That’s why there is a How to use this forum post on top. Also applies to you :wink:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6



// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      24


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int delayval = 500; // delay for half a second

void setup() {
 

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0;i<NUMPIXELS;i++){
  

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
 }

But again, duplicating a pin is just useless here. Just connect the second neo pixel ring to the same pin and you're done.

The reason for separating the pins is so I can have individual control of each eye. I would like to be able to add a wink (separate) and would also like the eyes to blink randomly.

Ahhhh, you didn’t tell! And with only duplicating you would have never succeeded!

Key is to have two objects of the Adafruit_Neopixel. To match the application I would go for leftEye and rightEye instead of pixels.

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

const byte LeftEyePin = 6;
const byte RightEyePin = 8;



// How many NeoPixels are attached to the Arduino?
const byte NumPixels = 24; //const is preffered over define


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel leftEye = Adafruit_NeoPixel(NumPixels, LeftEyePin, NEO_GRB + NEO_KHZ800); //left eye neopixels
Adafruit_NeoPixel rightEye = Adafruit_NeoPixel(NumPixels, RightEyePin, NEO_GRB + NEO_KHZ800); //right eye neopixels


int delayval = 500; // delay for half a second

void setup() {
  leftEye.begin(); // This initializes the NeoPixel library.
  rightEye.begin();
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0; i < NumPixels; i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    leftEye.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
    rightEye.setPixelColor(i, pixels.Color(0,150,0));

    leftEye.show(); // This sends the updated pixel color to the hardware.
    rightEye.show();

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}

Keep in mind that normally you don’t call .show() after altering each pixel. Most of the time you change every pixel you want to change and then do a .show() to update them all at once.

And for a robot I would get rid of delay() as fast as possible because now you can’t do anything while waiting. Bit stupid for a robot.

Septillion thank you very much! This sketch was an example in the neo pixel arduino library. I was trying to disassemble and make work. It is not completely what I want to do. I'm sure you will be hearing from me again soon! Thanks. Chris

Hello again. I tried running the posted code. It does not compile. Any help would be appreciated! Chris

It does not compile.

When you tried, the compiler told you exactly what was wrong.

Any help would be appreciated!

I'm sure. But it won't be forthcoming without knowing what the problem is.

sorry....

sketch_feb29a:39: error: 'pixels' was not declared in this scope

    leftEye.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
    rightEye.setPixelColor(i, pixels.Color(0,150,0));

Apparently should be

    leftEye.setPixelColor(i, leftEye.Color(0,150,0)); // Moderately bright green color.
    rightEye.setPixelColor(i, rightEye.Color(0,150,0));

Ah, missed that part. But Color() is a static method so it doesn't matter from with object you call it

    leftEye.setPixelColor(i, rightEye.Color(0,150,0)); // Moderately bright green color.
    rightEye.setPixelColor(i, leftEye.Color(0,150,0));

Will work the same. I wouldn't recommend doing so because it's confusing for us but it does the same.

You can also call it from the class itself

    leftEye.setPixelColor(i, Adafruit_NeoPixel::Color(0,150,0)); // Moderately bright green color.
    rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0,150,0));

Maybe to most transparent approach because now you see the Color() method doesn't change anything in leftEye or rightEye.

I do not understand why the eyes should be driver by different pins at all.

How about daisy-chaining them and controlling the content of the transferred data?

That’s very possible as well. But if you have a pin and memory to spare I can see the added value of separate variables for the left and right eye just to make it clear and easy for a starter. But yeay, you can make one big string of 48 Neopixels.

Then I would do it like this:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

const byte LeftEyePin = 6;
//const byte RightEyePin = 8;



// How many NeoPixels are attached to the Arduino?
const byte PixelsPerEye = 24;
const byte NumPixels = PixelsPerEye * 2; //const is preffered over define


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel eyes = Adafruit_NeoPixel(NumPixels, LeftEyePin, NEO_GRB + NEO_KHZ800); //left eye neopixels

int delayval = 500; // delay for half a second

void setup() {
  eyes.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0; i < PixelsPerEye; i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    eyes.setPixelColor(leftEye(i), Adafruit_NeoPixel::Color(0,150,0)); // Moderately bright green color.
    eyes.setPixelColor(rightEye(i), Adafruit_NeoPixel::Color(0,150,0)); // Moderately bright green color.    

    eyes.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}

inline int leftEye(int val){
  return val;
}

inline int rightEye(int val){
  return val + PixelsPerEye;
}

This assumes the right eye is chained after the left