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!