processing PWM to arduino

I'm trying to control some PWM outputs with processing. This code is a bit of a hack of a couple of examples. I'm working under the assumption that a slider created with a name red will have the current value of the slider in a variable red. I not sure this is the case but may help you understand my logic.

It would seem to need a a way of only sending a command to the arduino when the data has changed but I could not see how this was being done in the in the example arduino_pwm.

import processing.serial.*;
import cc.arduino.*;
import controlP5.*;

ControlP5 cp5;

Arduino arduino;

int red = 10;
int blue = 0;
int green = 0;
int slider = 0;
Slider abc;      //not sure what this does but it was in the example

void setup() {
  size(600, 400);
  noStroke();
  cp5 = new ControlP5(this);

  // Prints out the available serial ports.
  println(Arduino.list());
  
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[2], 57600);
    
   cp5.addSlider("red")
     .setPosition(50,150)
     .setSize(200,20)
     .setRange(0,255)
     ;
     
      cp5.addSlider("green")
     .setPosition(50,200)
     .setSize(200,20)
     .setRange(0,255)
     ;
     
      cp5.addSlider("blue")
     .setPosition(50,250)
     .setSize(200,20)
     .setRange(0,255)
     ;
}

void draw() {
  background(128);
  
  
   arduino.analogWrite(3, red);
   arduino.analogWrite(5, blue);
   arduino.analogWrite(6, green);
   println(blue , red , green );
    
}

There was a lot wrong with this code... I had done this long ago and a few computer crashes back... I dont know what version of processing I was using but it didn't seem so difficult before. The ah ha moment came when I figured out that the slider calls a subroutine that hopefully does what you want.

I had another snag in that red blue and green seemed to be predefined as floats or something other than integer which is what you need to write to the PWM output of the arduino. I had originally named my sliders red blue and green.

Another thing that is confusing is that your slider can be named Red and the variable for the slider value can be Red as well. Probably not the best idea but doable as is a slider named slider as was the case in one of the examples.

At any rate I got it working and here is the updated code for those that may be interested.

import processing.serial.*;
import cc.arduino.*;
import controlP5.*;
ControlP5 cp5;
Arduino arduino;

void setup() {
  size(700, 400);
  noStroke();
  cp5 = new ControlP5(this);
  
  println(Arduino.list());      // Prints out the available serial ports.
  
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[2], 57600);
    
   cp5.addSlider("Red")
     .setPosition(50,150)
     .setSize(512,20)
     .setRange(0,255)
     ;
     
      cp5.addSlider("Green")
     .setPosition(50,200)
     .setSize(512,20)
     .setRange(0,255)
     ;
     
      cp5.addSlider("Blue")
     .setPosition(50,250)
     .setSize(512,20)
     .setRange(0,255)
     ;
}

void draw() {
  background(128);
}

// Output analog values (PWM waves) to digital pins 3, 5, 6
// Note that only certain Arduino pins support analog output (PWM).
// See the documentation for your board for details.
// needs to be integer 0 - 255  

void Red(int Red) {
  arduino.analogWrite(5, Red);
    println ("PWM Red is " +Red);
}

void Green(int pwm_g) {
   arduino.analogWrite(3, pwm_g);
   println ("PWM Green is " +pwm_g);   
}

void Blue(int pwm_b) {
   arduino.analogWrite(6, pwm_b);
   println ("PWM Blue is " +pwm_b);   
}
void Red(int Red) {
  arduino.analogWrite(5, Red);
    println ("PWM Red is " +Red);
}

void Green(int pwm_g) {

I'm always interested in inconsistencies. Why does one function have an argument named Red, while the other function that does the exact same thing, except for which pin it operates on, take an argument with a completely different name?

Personally, I'd have used redValue and greenValue as the argument names.