Interfacing with processing for Control a motor DC

hi, I am new in the arduino and processing, and I will make the project control the speed and turning on an LED, this only as an example for me to understand how to send a slider value from processing to arduino. so far I've tried it, but can only turn on the LED only, and has not been able to send the value of the slider, can help me ??
where is the sketch that I made a mistake, or can someone make a sketch arduino and processing for my project, thanks.

this is arduino sketch :

char val;
int ledPin = 12;
int motorPin = 9;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  analogWrite(motorPin, LOW);
void loop(){
  {if (Serial.available()) 
  { val =;
  if (val == '1')
  { digitalWrite(ledPin, HIGH);} 
  {digitalWrite(ledPin, LOW);}
   } {
  if (Serial.available()) 
  {int r  = Serial.parseInt();
   analogWrite(motorPin, r);

and this is a processing sketch :

import processing.serial.*;
import controlP5.*;
ControlP5 cp5a;
ControlP5 cp5b;
Serial myPort;
int Speed;
void setup() {
  myPort = new Serial(this, Serial.list()[0], 9600);
  cp5a = new ControlP5(this);
  cp5b = new ControlP5(this);
  cp5b.addSlider("Speed",0 ,255, 30, 10, 50, 120, 30);

void draw() {

void controlEvent(ControlEvent theEvent)  {
    print("control event from : "+theEvent.controller().name());
    println(", value : "+theEvent.controller().value());
    if(theEvent.controller().value()==1) {

    if(theEvent.controller().value()==Speed) {

I really hope someone helps me, because this project is my college assignment that must be completed in the near future, thanks.

Look at the examples in serial input basics for simple reliable ways to receive data.

Your loop() is using followed by Serial.parseInt() without any way of knowing what data might be available.

If you want to send different types of data you need some sort of scheme so the Arduino knows what it is getting.


thanks so much for the instructions, but I'm sorry. I've read the instructions from the link given to me. but I still do not understand.
whether you can give a simple example of a slider for receiving data processing. Thank you, your help will be greatly appreciated by me. thanks so much.

thanks so much for the instructions, but I'm sorry. I've read the instructions from the link given to me. but I still do not understand.

If you can point out what part(s) of my instructions you don't understand I will try to help.

I am not familiar with the details of the Processing language myself.


    if(theEvent.controller().value()==Speed) {

You have a slider named "Speed". You are checking that the event is from a control named "Speed". Then, you are testing the value from that control. Only if the value is 0 do you send anything.

That makes no sense to me. It seems to me that you want to have:


Putting every { on a new line would REALLY help you understand the code structure.