Trying to control Neopixel ring delay rate with d from Processing

Hi, I'm trying to control the pace at which my two Neopixel ring LEDs glow with a byte variable called d from Processing. When I run the two sketches, the lights will turn on but blink at a constant pace, unaffected by the d variable.

  • In my computer, yes, it's port 3
  • I'm using an Arduino Nano

Please help asap!

Here are the two sketches, first Processing and then Arduino:

///PROCESSING------------------------------------------------------------------------------

import processing.video.;
import processing.serial.
;
int PulseVal;

Serial myPort;
Capture video;
PImage prevFrame;

float threshold = 50;
float average=0;
float sum = 0;
int count = 0;
float AVGr = 0;
float[] storedValues; // array for storing values to smooth

void setup() {
storedValues = new float[10];
size(320, 240);
// Using the default capture device
String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
video = new Capture(this, width, height);
video.start();
// Create an empty image the same size as the video
prevFrame = createImage(video.width, video.height, RGB);
myPort = new Serial(this, portName, 9600);
myPort.clear();
}

// New frame available from camera
void captureEvent(Capture video) {
// Save previous frame for motion detection!!
prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
prevFrame.updatePixels();
video.read();
}

void draw() {
background(0);
frameRate(10);
// You don't need to display it to analyze it!
image(video, 0, 0);

video.loadPixels();
prevFrame.loadPixels();

// Begin loop to walk through every pixel
// Start with a total of 0
float totalMotion = 0;

// Sum the brightness of each pixel
for (int i = 0; i < video.pixels.length; i ++ ) {
// Step 2, what is the current color
color current = video.pixels*;*

  • // Step 3, what is the previous color*
    _ color previous = prevFrame.pixels*;_
    _
    // Step 4, compare colors (previous vs. current)_
    _
    float r1 = red(current);_
    _
    float g1 = green(current);_
    _
    float b1 = blue(current);_
    _
    float r2 = red(previous);_
    _
    float g2 = green(previous);_
    _
    float b2 = blue(previous);_
    _
    // Motion for an individual pixel is the difference between the previous color and current color._
    _
    float diff = dist(r1, g1, b1, r2, g2, b2);_
    _
    // totalMotion is the sum of all color differences._
    _
    totalMotion += diff;_
    _
    }_
    _
    // averageMotion is total motion divided by the number of pixels analyzed._
    _
    float avgMotion = totalMotion / video.pixels.length;_
    _
    // Draw a circle based on average motion*_
    * noStroke();*
    * fill(0);*
    _ float r = avgMotion * 2;_

* AddNewValue(r); // function to average values*
* float lerp = 0.95; //must be between 0-1*
_ AVGr = lerp * AVGr + (1.0-lerp) * r;_
* int AVGr2 = int (AVGr);*

* int AVGr3 = 100 - AVGr2;*
* float d = map(AVGr3, 0, 100, 0, 10);*
* byte d2 = byte (d);*

* myPort.write(d2);*
* //println(d2);*

* ellipse(width/2, height/2, r, r);*

* }*

// FUNCTION to smooth the values:
void AddNewValue(float val){
* if(count < storedValues.length){*
* //array is not full yet*
* storedValues[count++] = val;*

* sum += val;*
* }*
* else{*
* sum += val;*
* sum -= storedValues[0];*

* //shift all of the values, drop the first one (oldest)*
* for(int i = 0; i < storedValues.length-1; i++){*
_ storedValues = storedValues[i+1] ;
* }
//the add the new one*
* storedValues[storedValues.length-1] = val;
}
}
////// ARDUINO------------------------------------------------------------------------------_
#include <Adafruit_NeoPixel.h>
_#define PIN 3 // input pin Neopixel is attached to*
//#define PIN 4
#define NUMPIXELS 12 // number of neopixels in Ring
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
* Serial.begin(9600);
pixels.begin(); // Initializes the NeoPixel library.
pixels.show();
pixels.setBrightness(40);
Serial.begin(9600);
}
void loop() {
//
byte d2; // timing delay*
* if (Serial.available()) { // If data is available to read,
d2 = Serial.read();
Serial.println(d2);
}
// GOING UP:
for(int i=0;i<255;i++){
for(int j=0; j<NUMPIXELS; j++){
pixels.setPixelColor(j, i, i, i); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(2);
// Serial.print("LED speedD: ");
// Serial.println(i);
}*_

// GOING DOWN:
* for(int i=255;i>0;i--){*
* for(int j=0; j<NUMPIXELS; j++){*
* pixels.setPixelColor(j, i, i, i); // Moderately bright green color.*
* }*
* pixels.show(); // This sends the updated pixel color to the hardware.*
* delay(d2);*
* Serial.print("LED speedD: ");*
* Serial.println(d2);*
* }*

}

I'm actually trying to communicated d2, not d (Sorry for the typo in the title/description of the post!)

You will not notice the diferance with such a short delay as you have

  frameRate(10);

It does NOT make sense to set the frame rate in draw(). That is something you do ONCE (in setup()).

What value does video.pixels.length give you? Are you having overflow issues?

void loop() {
//
  byte d2; // timing delay

  if (Serial.available()) { // If data is available to read,
    d2 = Serial.read();
    Serial.println(d2);
   }

If there is no serial data ON THIS PASS THROUGH loop(), what value is in d2 when you use it? If you said "I haven't a clue", you'd be right. And wrong. NEVER use an uninitialized variable. The corollary is ALWAYS initialize variables.

Of course, Mike is right. Setting a 10 millisecond (max) delay between showing some brightness level and the next is NOT going to have a noticeable impact.