controlling motor speed with a slider

Hi,

I need some help creating a slider program that controls the speed
of two DC motors individually, using (correct me if I’m wrong) the
PWM pin, with Processing. I was able to get one motor running in
a half-assed way using these two programs (one for the Arduino, the
other for Processing):

// Language: Arduino

int speedbpin = 9;
int dirbpin = 12;
int speedapin = 10;
int dirapin = 13;
int speed;
int dir;
int val;

void setup()
{
  Serial.begin(9600);
  pinMode(speedapin, OUTPUT);
  pinMode(speedbpin, OUTPUT);
}

void loop()
{
  if (Serial.available()) {
    val = Serial.read();
    if (val == 20) {
      analogWrite(speedapin, speed=130);
      digitalWrite(dirapin, dir);
    }
    if (val == 60) {
      analogWrite(speedapin, speed=160);
      digitalWrite(dirapin, dir);
    }
    if (val == 100) {
      analogWrite(speedapin, speed=190);
      digitalWrite(dirapin, dir);
    }
    if (val == 140) {
      analogWrite(speedapin, speed=220);
      digitalWrite(dirapin, dir);
    }
    if (val == 180) {
      analogWrite(speedapin, speed=250);
      digitalWrite(dirapin, dir);
    } else if (val == 0) {
      analogWrite(speedapin, speed=0);
    }
  }
  
  if (Serial.available()) {
    val = Serial.read();    
    if (val == 'Q') {
      analogWrite(speedbpin, speed=200);
      digitalWrite(dirapin, dir);      
    }
    if (val == 'W') {
      analogWrite(speedbpin, speed=0);    
    }
  }
}
// Language: Processing

import controlP5.*;
import processing.serial.*;
Serial port;

ControlP5 controlP5;

int myColorBackground = color(0,0,0);

int sliderValue = 100;

void setup() {
  size(400,400);
  controlP5 = new ControlP5(this);
  controlP5.addSlider("slider",0,200,0,100,160,100,10); // (theName, theMin, theMax, theDefaultValue, theX, theY, theW, theH)
  controlP5.addSlider("sliderValue",0,200,0,100,200,100,10);
  //---------------------
  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  background(myColorBackground);
  fill(sliderValue);
  rect(0,0,width,100);
}

void slider(int theColor) {
  myColorBackground = color(theColor);
  println("a slider event. setting background to "+theColor);
  controlP5.controller("sliderValue").setValue(theColor);
  if (sliderValue > 0 && sliderValue <= 40) {
    port.write(20);
  }
  if (sliderValue > 40 && sliderValue <= 80) {
    port.write(60);
  }
  if (sliderValue > 80 && sliderValue <= 120) {
    port.write(100);
  }
  if (sliderValue > 120 && sliderValue <= 160) {
    port.write(140);
  }
  if (sliderValue > 160 && sliderValue <= 200) {
    port.write(180);
  } else if (sliderValue == 0) {
    port.write(0);
  }
}

void keyPressed() {
  if (key == 'H' || key == 'h') {
    controlP5.controller("sliderValue").setValue(150);
    port.write('H');
  } else if (key == 'L' || key == 'l') {
    controlP5.controller("sliderValue").setValue(100);
    port.write('L');
  }
}

However, what it did was make two sliders, one acts as the main and
does the actual controlling, the other doesn’t do anything. :-/

Here’s what I need help on, I want to have two sliders, each controlling
one motor without interfering with each other.

Hope all that’s not too confusing… :wink: