Processing to Arduino

Hi everyone

This is my first post here and require so help with a programming issue I’m having I am trying to send three values from processing to Arduino so they are stored in different variables. When printing the processing program works accordingly to check if my values were being sent to the Arduino I connected a LED to see if the brightness would change when moving one of the sliders however nothing happens. I thing the issue is trying to convert from byte format to int or the values are stored as ASCII values.
Processing Code:
import controlP5.*;
import processing.serial.*;

Serial port;
ControlP5 cp5;
PFont font;
color currentcolor;

void setup(){

** size(1000, 450);**
** port = new Serial(this, “COM2”, 9600);//Choose correct COM number which is your Arduino board connected to:**

** cp5 = new ControlP5(this);**
** font = createFont(“Calibri”, 30);**

** cp5.addSlider(“Scale”)**
** .setPosition(150, 200)**
** .setSize(500,70)**
** .setRange(0,100)**
** .setValue(0)**
** .setFont(font)**
** .setColorValue(color(0,0,255))**
** .setColorBackground(color(100, 100, 255))**
** ;**

** cp5.addSlider(“Octave”)**
** .setPosition(150, 300)**
** .setSize(500,70)**
** .setRange(0,100)**
** .setValue(102)**
** .setFont(font)**
** .setColorValue(color(0,0,255))**
** .setColorBackground(color(100, 100, 255))**
** ; **
** cp5.addSlider(“Transposition”)**
** .setPosition(150, 100)**
** .setSize(500,70)**
** .setRange(0,100)**
** .setValue(102)**
** .setFont(font)**
** .setColorValue(color(0,0,255))**
** .setColorBackground(color(100, 100, 255))**
** ; **
}

void draw(){
** background(50, 0 ,100);**
** fill(200, 200,255);**
** text(“LED Control”,350, 50);**
** textSize(30);**
** int octave = int(cp5.getController(“Octave”).getValue());**
** int scale = int(cp5.getController(“Scale”).getValue());**
** int transposition = int(cp5.getController(“Transposition”).getValue());**

** byte out =new byte[3];**
** out[0]=byte(octave);**
** out[1]=byte(scale);**
** out[2]=byte(transposition);**
** println(out);**
** port.write(out);**
}

Arduino Code:
int currentValue = 0;
#define LED 11
int values[] = {0,0,0};
int Oct;
int Sca;
int Tran;
void setup() {
** Serial.begin(9600);**
** pinMode(LED,OUTPUT);**
**} **

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

** int incomingValue = Serial.read();**

** values[currentValue] = incomingValue;**

** if(currentValue<1){**
** Oct=values[currentValue];**
** currentValue++; **
** analogWrite(LED,Oct); **
** }**
** if(currentValue<2&& currentValue>0){**
** Sca=values[currentValue];**
** currentValue++;**
** }**
** if(currentValue<3&& currentValue>1){**
** Tran=values[currentValue];**
** currentValue++;**
** }**
** }**
}

Any help would be greatly appreciated.

Thanks
Goose

Serial.read returns a character, in ascii code, not a numeric.

In this case would I have to convert the number from ASCII to int would the following code then allow the slider to change the value or not?

Use a char to receive Serial.read().
.
.

if(currentValue == "0") {........}
else if currentValue == "1"{....}
else if currentValue == "3"{.....}
.
.
.

If I change the incoming value to a char that means I would need to change the values to a char too wouldn't I or is this not necessary anymore along with this once it is a char I can take the values and assign them to the int variables so they can be used for analogWrite. Sorry for all the questions I've been stuck on this problem for ages. Thanks for all the help so far

Yes, change from integer to character array.

I checked som old code here. Use '0', '1' and '2'. "" was a mistake.

I tried that and it doesn't change the brightness of the LED I've tried different pins and different sliders however there's no change
int currentValue = 0;
#define LED 11
char values = {0,0,0};
char Oct;
char Sca;
char Tran;
void setup() {
Serial.begin(9600);
pinMode(LED,OUTPUT);
}

void loop() {
if(Serial.available()){
char incomingValue = Serial.read();

values[currentValue] = incomingValue;

if(currentValue=="0"){
Oct=values[currentValue];
currentValue++;
analogWrite(LED,Oct);
}
if(currentValue=="1"){
Sca=values[currentValue];
currentValue++;
}
if(currentValue=="2"){
Tran=values[currentValue];
currentValue++;
}
}
}

Aja, I see something:

    values[currentValue] = incomingValue;

Some more work is needed. The ASCII character 0 is represented by the value 48 in decimal, 0X30 in HEX.

The received character must be translated into

 int currentIndex = currentValue - '0'

The code will be like

    values[currentIndex] = incomingValue;

Railroader:
Aja, I see something:

    values[currentValue] = incomingValue;

Some more work is needed. The ASCII character 0 is represented by the value 48 in decimal, 0X30 in HEX.

The received character must be translated into

 int currentIndex = currentValue - '0'

The code will be like

    values[currentIndex] = incomingVal

Wouldn't this result in currentIndex being equal to -48 as its 0-'0' or have I completed missed the ball because currentValue is intialised as zero and running -48 as an index would send it out of range.

No. Treating the recived character 0 yields the value 48. Char Value 48 minus represetation value 48 results in zero, first cell in the array.

Use Serial.print to tell You!

Your initial code would, if it compiles, put a character value, like zero == 48. That would go wrong.

Can You post the new code? I'll try to check it up better.

int currentValue = 0;
#define LED 11
char values = {0,0,0};
char Oct;
char Sca;
char Tran;
void setup() {
Serial.begin(9600);
pinMode(LED,OUTPUT);
}

void loop() {
if(Serial.available()){
char incomingValue = Serial.read();
int currentIndex =currentValue -'0';
values[currentIndex] = incomingValue;

if(currentIndex=="0"){
Oct=values[currentValue];
currentValue++;
analogWrite(LED,Oct);
}
if(currentIndex=="1"){
Sca=values[currentValue];
currentValue++;
}
if(currentIndex=="2"){
Tran=values[currentValue];
currentValue++;
}
}
}

So this is the updated version I've tried the slider yet there is no change

I'm not shure I understand You description of what the code is supposed to do.

Look at this, using code tags, upper left symbol in this window.

int currentIndex = 0;

#define LED 11
char values[] = {0, 0, 0};
char Oct;
char Sca;
char Tran;
void setup()  {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    char incomingChar = Serial.read();

    values[currentIndex] = incomingChar;//charcater stored.

    if (incomingChar == "0") {
      Oct = values[currentIndex];
      currentIndex++;
      analogWrite(LED, Oct);
    }
    if (incomingChar == "1") {
      Sca = values[currentIndex];
      currentIndex++;
    }
    if (incomingChar == "2") {
      Tran = values[currentIndex];
      currentIndex++;
    }
  }
}

Note the select option in the inserted window.

Have a look .

So the code is meant to take the values from the GUI drawn by processing where the sliders can be moved to change the value

Processing code:
import controlP5.;
import processing.serial.
;

Serial port;
ControlP5 cp5;
PFont font;
color currentcolor;

void setup(){

size(1000, 450);
port = new Serial(this, "COM2", 9600);//Choose correct COM number which is your Arduino board connected to:

cp5 = new ControlP5(this);
font = createFont("Calibri", 30);

cp5.addSlider("Scale")
.setPosition(150, 200)
.setSize(500,70)
.setRange(0,100)
.setValue(0)
.setFont(font)
.setColorValue(color(0,0,255))
.setColorBackground(color(100, 100, 255))
;

cp5.addSlider("Octave")
.setPosition(150, 300)
.setSize(500,70)
.setRange(0,100)
.setValue(102)
.setFont(font)
.setColorValue(color(0,0,255))
.setColorBackground(color(100, 100, 255))
;
cp5.addSlider("Transposition")
.setPosition(150, 100)
.setSize(500,70)
.setRange(0,100)
.setValue(102)
.setFont(font)
.setColorValue(color(0,0,255))
.setColorBackground(color(100, 100, 255))
;
}

void draw(){
background(50, 0 ,100);
fill(200, 200,255);
text("LED Control",350, 50);
textSize(30);
int octave = int(cp5.getController("Octave").getValue());
int scale = int(cp5.getController("Scale").getValue());
int transposition = int(cp5.getController("Transposition").getValue());

byte out =new byte[3];
out[0]=byte(octave);
out[1]=byte(scale);
out[2]=byte(transposition);
println(out);
port.write(out);
}

The arduino code is to take the values from the different sliders and store the values from the sliders as an integer to later be used in a different code. The issue is to do with the Arduino code in which I send the array out to it and each index value is to be stored to the variables oct,sca and tran as intergers which can be later manipulated I hope this cleared up the issue at hand

Please find out about, and use code tags, for posting code. I rather use time for brain work than editing and copying text.

Sliders… I’m lost. There are no analog readings in the code.

I don’t get it, don’t understand the info You just posted. Give an example of the stream of characters sent to the Arduino and how You want to handle them.

Apologises about my previous posts the processing code produces an output for the sliders so for example out[0]=100, out[1]=0 and out[2]=0 the port.write() function sends the information of out to the serial which is then read by the Arduino code. This code is sent as a byte and then uses serial.read to store the values into an array and then the array values are stored into different variables. The processing side works and produces the above values however the Arduino doesn't assign the values to the variable the whole point behind using the LED is to see if the values from processing get to the Arduino by changing the slider I would be able to see if the brightness changes accordingly. Thank you for all the help so far i really appreciate it.

goose123456:
the processing code produces an output for the sliders

What b---dy sliders? I don't want to know how data is generated. I ask for what characters will be received by the Arduino.

so for example out[0]=100, out[1]=0 and out[2]=0

I undestand nothing. Above me head.
I want to help You making the low level transfer of information work. What You will do with that information, received by the Arduino, is a different matter.

So Serial should have {100,0,0} this is then stored in an array which is then put through a conditional loop assigning each index to a variable to check if the index value has been stored I use analogWrite() to the LED to see if the LED will light up according to the value sent on the serial monitor. The serial monitor receives this as a byte and I want to assign each value to an integer variable. I apologise for the low level communication and I really do appreciate the help I hope this has made it clearer.

Okey. Do You want to transfer a numerical value, 0 to 255, in each char/byte, 3 bytes in line/each transfer?

I likely made the mistake assuming character transfers.

Sorry for the confusion yes I want a numerical value but I want to use the values on the serial monitor so in this case if the serial monitor has {100,1,10} I want the variable oct=100, variable sca=1 and variable tran =10.