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(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  analogWrite(motorPin, LOW);
}
void loop(){
  {if (Serial.available()) 
  { val = Serial.read();
  if (val == '1')
  { digitalWrite(ledPin, HIGH);} 
  else 
  {digitalWrite(ledPin, LOW);}
    delay(10);
    }
   } {
  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() {
  size(200,200);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  cp5a = new ControlP5(this);
  cp5a.addToggle("1",false,10,10,20,20);
  cp5b = new ControlP5(this);
  cp5b.addSlider("Speed",0 ,255, 30, 10, 50, 120, 30);
 
}

void draw() {
  background(100);
}

void controlEvent(ControlEvent theEvent)  {
  if(theEvent.isController()) 
  
    print("control event from : "+theEvent.controller().name());
    println(", value : "+theEvent.controller().value());
    
    if(theEvent.controller().name()=="1")
    if(theEvent.controller().value()==1) {
     myPort.write('1');
    }
    else
    {myPort.write('0');
    }

   if(theEvent.controller().name()=="Speed")
    if(theEvent.controller().value()==Speed) {
     myPort.write(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 Serial.read() 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.

...R

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.

Fiansyah:
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.

...R

   if(theEvent.controller().name()=="Speed")
    if(theEvent.controller().value()==Speed) {
     myPort.write(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:

   if(theEvent.controller().name()=="Speed")
  {
      myPort.write(theEvent.controller().value());
  }

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