slider with processing to control PWM arduino using Firmata

hi im jonny.. im have problem about my project. i want to make interfaceing control RGB LED in arduino . and on/off with slider in processing . and im use library g4p from processing. for GUI .

so for on/off rgb led with button i have finish. but to control PWM i have problem.

this is my code

import processing.serial.*;

import g4p_controls.*;

import org.firmata.*;
import cc.arduino.*;

Arduino arduino;


int LED[] = {9,10,11};
int led[] = {0,1,2};
int c;
int value;
int x;

GButton btnRedOn,btnRedOff,btnGreenOn,btnGreenOff,btnBlueOn,btnBlueOff;
GCustomSlider sdrRed,sdrGreen,sdrBlue;
boolean state;


void setup(){
  size(500,300);
  
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  
  for(int i=0;i<LED.length;i++){
    arduino.pinMode(LED[i], arduino.OUTPUT); 
  }
  
  //button Merah
  btnRedOn = new GButton(this,width-100, 50, 30,20,"ON");
  btnRedOff = new GButton(this,width-100, 70, 30,20,"OFF");
 
 //button Hijau
  btnGreenOn = new GButton(this,width-150, 100, 30,20,"ON");
  btnGreenOff = new GButton(this,width-150, 120, 30,20,"OFF");
  
  //button Biru
  btnBlueOn = new GButton(this,width-200, 150, 30,20,"ON");
  btnBlueOff = new GButton(this,width-200, 170, 30,20,"OFF");
  
  sdrRed = new GCustomSlider(this,150,70,150,70,"red_yellow18px");
  sdrRed.setLimits(0,0,255); 
  
}


void draw(){
  background(255); 
  smooth();
  
       //on/off rgb each color its work ...        
    //for(int i=0;i<led.length;i++){ 
    //      arduino.digitalWrite(LED[led[i]], c);
  //    }
  
 arduino.analogWrite(LED[led[0]. value); // a sample for control PWM for RED led in RGB.       

  if(state){
     c = arduino.HIGH;
   }
   
    if(!state){
      c = arduino.LOW;    
   }
}

void handleButtonEvents(GButton button,GEvent event) { 
  
  for(int i=0;i<led.length;i++){ 
  
   //led ON
  if(this.btnRedOn == button && event == GEvent.CLICKED){
      state = true;
      led[i] = 0;
     
  }else if (this.btnGreenOn == button && event == GEvent.CLICKED) {
     state = true;
     led[i] = 1 ;
  }else if (this.btnBlueOn == button && event == GEvent.CLICKED) {
     state = true;
     led[i] = 2;
  }
  
  //led OFF
  if(this.btnRedOff == button && event == GEvent.CLICKED){
      state = false;
      led[i] = 0;
  }else if(this.btnGreenOff == button && event == GEvent.CLICKED){
      state = false;
      led[i] = 1;
  } else if(this.btnBlueOff == button && event == GEvent.CLICKED){
      state = false;
      led[i] = 2;
  }
}
    
}

public void handleSliderEvents(GValueControl slider, GEvent event){
           if(slider == sdrRed) {
                 if(state){
                      
                }
           }
      
}


void mousePressed(){
   print(mouseX);
   print(" || ");
   println(mouseY); 
}

if u see the function

public void handleSliderEvents(GValueControl slider, GEvent event){
           if(slider == sdrRed) {
                   value =  sdrRed.getValueI();
           }
      
}

that's mean for give value from 0 to 255 with slider to arduino. and i do have a value. and still error.

my question ... how to get value if im click ON in btnRedOn .. and give value to slider 255 , else click OFF in btnRedOff give value 0 automatic move the cursor slider .. thankss :slight_smile:

int LED[] = {9,10,11};
int led[] = {0,1,2};

If you worked for me, you wouldn't anymore. Really, now, there are other letters in the alphabet. Pick names that differ by more than case. Since led[] is an array of indices, and LED is an array of pins, pinNumbers and pinIndices are better choices.

that's mean for give value from 0 to 255 with slider to arduino. and i do have a value. and still error.

What error?

Which Firmata sketch is loaded? Where are you telling the arduino instance that the pins 9, 10, and 11 are PWM pins? The answer is that you aren't.

Diddling with the indices array is pointless. You are never actually assigning different values to the various elements in the array.

PaulS:

int LED[] = {9,10,11};

int led[] = {0,1,2};



If you worked for me, you wouldn't anymore. Really, now, there are other letters in the alphabet. Pick names that differ by more than case. Since led[] is an array of indices, and LED is an array of pins, pinNumbers and pinIndices are better choices.



> that's mean for give value from 0 to 255 with slider to arduino. and i do have a value. and still error.


What error?

Which Firmata sketch is loaded? Where are you telling the arduino instance that the pins 9, 10, and 11 are PWM pins? The answer is that you aren't.

Diddling with the indices array is pointless. You are never actually assigning different values to the various elements in the array.

thank you for respone ... the code a work ..

before im not upload sketch standard firmata XD XD