Processing passing data to arduino

Dear,

I want to make a little script with arduino and processing.
I have a color wheel in processing and a Neopixel connected to my arduino.
Now I want to be able to select a color with the color picker and the Neopixel should change into this color.

I know I can do this with the Serial, but the arduino takes every character as a seperate value.
So I can't go higher then LED 9...
Does anybody know how to solve this problem?
And maybe as well how to change the data type from a String into an int? (String from the serial and an int for the neopixel, if i'm not mistaking.)

Kind Regards,
Willem

I know I can do this with the Serial, but the arduino takes every character as a seperate value.
So I can't go higher then LED 9...

Y
e
s

y
o
u

c
a
n.

That was a hint. Let's see what you can do with it.

Hi,

so I managed to get it all working :smiley:
The only problem i have is that it crashes after about ten clicks...
And from time to time it flashes the wrong color.
I'm quiet sure that it's my arduino, I only don't know why.

Can anybody find a bug??

Processing code:

import processing.serial.*;

PImage m;
Serial port;
color c;
int r;
int g;
int b;

void setup() {
   port = new Serial(this,Serial.list()[0], 9600);
  
  
  size(400,400);
    smooth();
    ellipseMode(CENTER);
    background(0);
  colorMode(HSB,100);
  for(int x=0;x<width;x++){
    for(int y=0;y<height;y++){
      if(dist(200,200,x,y)<=190){
        stroke(map(atan2(x-200,y-200),-PI,PI,0,100),/*map(dist(200,200,x,y),0,190,0,100)*/100,100);
        point(x,y);
      }
    }
  }
  noFill();
  stroke(0);
  ellipse(200,200,380,380);
  m=get();
  
  
  colorMode(RGB,100);
}
void draw() {
   image(m,0,0);
    if(mousePressed == true){
        stroke(0);
        ellipse(mouseX,mouseY,30,30);
        
    }
}


void mousePressed() {
        c = get(mouseX,mouseY);
        r=(c>>16)&255;
        g=(c>>8)&255;
        b=c&255; 
        port.write(r + "," + g + "," + b + ",");
        println("Red:"+r+" Green:"+g+" Blue:"+b);
   println("light on");
} 

void keyPressed(){
  if(key == 32)
  {
     port.write("1");
  }
}

Arduino code:

#include <Adafruit_NeoPixel.h>

char inData[20]; // Allocate some space for the string
char color[3];
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character



int r=0;
int g=0;
int b=0;
byte a=0;
byte e=0;


// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 11, NEO_GRB + NEO_KHZ800);
    
void setup(){
 Serial.begin(9600);
 Serial.write("Power On");
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(60);
}





char Comp(char* This){

 while(Serial.available() > 0) // Don't read unless
   // there you know there is data
 {
   if(index < 19) // One less than the size of the array
   {
     inChar = Serial.read(); // Read a character
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string
   }
 }
 
 if(index>4)
 {
   return(2);
 }
 
 
  if(strcmp(inData,This)  == 0){
    clearArray();
   return(0);

 }
 else{
   return(1);


 }
}

void clearArray()
{
   for(int i=0;i<19;i++){
     inData[i]=0;
   }
   index=0;
   Serial.println("Array cleared");
  }


void getColor()
{
 e = 0;
 a = 0;
 for(int i=0;i<19;i++){
     if(inData[i] != ',')
     {
       color[e] = inData[i];
       e++;
       Serial.println(i);
     }else
     {
       if(a == 0)
       {
         r = atoi(color);
         Serial.println("red check");
         Serial.println(r);
       }
       if(a == 1)
       {
         g = atoi(color);
         Serial.println("green check"); 
         Serial.println(g); 
       }
       if(a == 2)
       {
         b = atoi(color);
         Serial.println("blue check"); 
         Serial.println(b); 
          clearArray();
         break;
       }
       color[0] = 0;
       color[1] = 0;
       color[2] = 0;
       e=0;
       a++;
     }
       
 }
   index=0;
}




void loop()
{
   if(Comp("1")==0){
         
      strip.setPixelColor(1, r, g, b);
       strip.show();
      delay(100); // Wait 100 milliseconds for next reading
   }
   if(Comp("")==2)
   {
       getColor();
      strip.setPixelColor(1, r, g, b);
       strip.show();
      delay(100); // Wait 100 milliseconds for next reading
   }
      strip.setPixelColor(1, 0,0,0);
       strip.show();
delay(100); // Wait 100 milliseconds for next reading
}

Thanks in advance,
Willem

        port.write(r + "," + g + "," + b + ",");

Start and end of packet markers are missing. Without them, you can not assure that the data stays in sync. Nor can you tell when a new packet starts or when the end of a packet is read.

You are assuming, incorrectly, that serial data delivery is guaranteed. It is not.

 if(index>4)
 {
   return(2);
 }

What is this supposed to be doing?

Properly indenting
your code makes it
much easier to
read. The stuff you have
runs all over
the place, making
it hard to follow.