I'm wondering if someone could take a look at this code for me that I cut up a bit. I would like to add a random blink to both eyes. I see a random seed command, random min, and random max instruction but I am unsure how to integrate it in. I appreciate it very much... Chris
#include <Adafruit_NeoPixel.h>
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
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, Adafruit_NeoPixel::Color(0,0,25));
rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0,0,25));
leftEye.show(); // This sends the updated pixel color to the hardware.
rightEye.show();
}
very basically, you can do it sort of like this:
#include <Adafruit_NeoPixel.h>
const byte LeftEyePin = 6;
const byte RightEyePin = 8;
const byte NumPixels = 24; //const is preffered over define
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
unsigned long lastBlinktime = 0;
unsigned long randomBlinkInterval = 1000;
void setup()
{
leftEye.begin();
rightEye.begin();
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop()
{
if(millis() - lastBlinktime >= randomBlinkInterval)
{
randomSeed(analogRead(A4));
for (int i = 0; i < NumPixels; i++) // fix this block to make eye closed
{
leftEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 0));
rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 0));
digitalWrite(13, LOW);
}
leftEye.show();
rightEye.show();
delay(random(50,75)); // random time closed in milliseconds: human blink takes 100-150 milliseconds
for (int i = 0; i < NumPixels; i++) // fix this block to make eye open
{
leftEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 25));
rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 25));
digitalWrite(13, HIGH);
}
lastBlinktime = millis();
randomBlinkInterval += random(10000,15000); //random time between blinks: humans blink average 4-5 times per minute
}
}
Hello, I uploaded this sketch but its not working. Im wondering what needs to be fixed in the block that has the comment fix this block to make eye opened/closed. Sorry I am very new to this type of programming. Chris
yeah, I missed the show() function on the second part.
you can try it below.
you probably should go through the examples to try to understand how this library functions.
#include <Adafruit_NeoPixel.h>
const byte LeftEyePin = 6;
const byte RightEyePin = 8;
const byte NumPixels = 24; //const is preffered over define
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
unsigned long lastBlinktime = 0;
unsigned long randomBlinkInterval = 1000;
void setup()
{
leftEye.begin();
rightEye.begin();
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop()
{
if(millis() - lastBlinktime >= randomBlinkInterval)
{
randomSeed(analogRead(A4));
for (int i = 0; i < NumPixels; i++) // fix this block to make eye closed
{
leftEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 0));
rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 0));
digitalWrite(13, LOW);
}
leftEye.show();
rightEye.show();
delay(random(50,75)); // random time closed in milliseconds: human blink takes 100-150 milliseconds
for (int i = 0; i < NumPixels; i++) // fix this block to make eye open
{
leftEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 25));
rightEye.setPixelColor(i, Adafruit_NeoPixel::Color(0, 0, 25));
digitalWrite(13, HIGH);
}
lastBlinktime = millis();
randomBlinkInterval += random(10000,15000); //random time between blinks: humans blink average 4-5 times per minute
leftEye.show(); // <<<<<<<<<< Here
rightEye.show(); // <<<<<<<<<< Here
}
}
You can check out some sample code that includes Neopixels and random 'blinking' (and even random colors for the blinks) here:
Specifically, take a look at this kind of code:
// Determine the amount of time to sleep:
int sleepTime = random(minSleep, maxSleep) * 1000;