Processing to Arduino to Neopixels communication problems

Hello!

I’m trying change the intensity of my Neopixels with a string of values from processing. Setup is pretty simple - 15 Neopixels wired to Arduino Uno. Signal on pin 6.

I’m sending a string of 16 characters from processing, like “000000060000000)”. Each digit represents one LED on neopixel strip. I’m currently (for testing) mapping mouseX in processing to position (1-15), and mouseY to value between 0-9. “)” is for arduino to recognize the end of message. My intent is to re-map it after receiving on Arduno side to LED index and brightness.

My problem is, that even though I see some blinking around LED that my mouse is pointing, all LEDs are continuously red, even though the string sent from processing is mostly 0s - so most LEDs should be unlit. Additionally when the brightness value from processing goes higher than 6, selected LED goes dark.

My Arduino is on COM10, and all serial communication is running on 9600.

I would appreciate if someone could check my code, and point me towards right solution:

Processing:

import processing.serial.*;

Serial myPort;  // create object from Serial class

int pos; //mouse position maped to neopixel index
int mHeight; //mouse height mapped to neopixel brightness
String val; //message
String oldVal; 

void setup() 
{
  size(1000, 200); 
  //String portName = Serial.list()[0];
  myPort = new Serial(this, "COM10", 9600);
}

void draw() {
  pos = int(map(mouseX, 0, width, 0, 15));
  mHeight = int(map(mouseY, 0, height, 0, 10));

  val = ""; //clear message just in case

  stroke(120);
  
  for (int i=0; i < 15; i++) //segment lines for reference
  {
    float x = width/15;
    x = x*i;
    line(x, 0, x, height);
  }

  for (int j = 0; j < 15; j++) //construct val string of 15 digits
  {
    if (j == pos) {
      val = val + mHeight;
    } else
    {
      val = val + "0";
    }
  }

  val = val + ")"; //add ")" for Arduino

  if (val.equals(oldVal) == false)  //send only if value changed
  {
    myPort.write(val);         
    println(val);
    delay(10);
  } 

  oldVal = val; //update oldVal
}

Arduino:

#include <Adafruit_NeoPixel.h>

#define PIN            6 //NeoPixel pin
#define NUMPIXELS      15 //Number of NP LEDs

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

int delayval = 1000; // delay for a second
String fromP = ""; //store data from processing
String fromP_Previous = "000000000000000";
boolean fromP_Completed = false;
char ReadChar;

void setup()
{
  Serial.begin(9600); //Arduino serial port baud rate:9600
  fromP.reserve(15);

  pixels.begin(); //initializes the NeoPixel library
  pixels.clear(); //set all NP to 0.0.0

  //setup check
  for (int i = 0; i < NUMPIXELS; i++) {
    for (int j = 0; j < 240; j = j + 10)
    {
      pixels.setPixelColor(i, pixels.Color(j, j, j));
      pixels.show();
      delay(3);
    }
  }
}

void loop() {
  if (Serial.available()) {

    ReadChar = (char)Serial.read();

    if (ReadChar == ')') { // Right parentheses ) indicates complet of the string
      fromP_Completed = true;
    } else {
      fromP += ReadChar;
    }

    if (fromP_Completed) {

      Send_fromP();

      delay(10);

      fromP = "";
      fromP_Completed = false;
    }
  }
}

void Send_fromP() { //split message into values and remap for brightness

  pixels.clear();

  for (int p = 0; p < NUMPIXELS; p++)
  {
    int valP =  fromP.charAt(p);
    int eachNP = map(valP, 0, 9, 0, 255); //map each char from 0-9 to 0-255

    pixels.setPixelColor(p, pixels.Color(eachNP, 0, 0));
  }
  
  pixels.show(); //send to neopixels
  delay(10);
}

Thanks!

@mhol I believe you are running this Processing sketch on a Windows machine, and not really a Raspberry Pi (I saw this got posted in the Processing for Pi section)?

@gohai

You’re right this is an Arduino with Processing.

You’re both right. I originally posted it in “Coding Questions”, but for some reason @jeremydouglass moved it to Pi. Could someone please be moved back?
Thanks
mhol

edit: never mind - did it myself.

I checked your code and it seems all right. Based on your comment which I quote above, I recommend you check the documentation of your hardware. If you are providing 0000000 values in your string, then all your LEDs should be off. If this first task is not working, then you should investigate further and see what is missing to get this first part working. another suggestion is to explore any example demos provided by the manufacturer, if they exist.

Kf

ok, I figured that one out. The trick is to after receiving on arduino divide the string into sections by inserting dots (or something else), and than to use “toInt()” on these sub-strings to convert them from bytes to ints.

void Send_fromP() {

  // Find indexes of '.' and save to array
  int sect[15];

  sect[0] = fromProc.indexOf('.'); //first one

  for (int s = 1; s < 15; s++)  { //all others
    int sTemp = s - 1;
    sect[s] = fromProc.indexOf('.', sect[sTemp] + 1);
  }

  // Save what's between '.' to array of strings
  String len[15];

  len[0] = fromProc.substring(0, sect[0]); //first one

  for (int l = 1; l < 15; l++)  { //all others
    int lTemp = l - 1;
    len[l] = fromProc.substring((sect[lTemp] + 1), sect[l]); 
  }

  //Convert strings to Int values
  float br[15];

  for (int b = 0; b < 15; b++)  {
    br[b] = len[b].toInt();
  }

  //Send values to neopixels
  for (int i = 0; i < NUMPIXELS; i++)  {

      pixels.setPixelColor(i, pixels.Color(br[i], br[i], br[i]));
    
  }
}