0
votes

The problem is that every time I add a slider object and try to connect it to a different servo motor through a different pin the sliders both only control the same servo motor. It won't allow for me to add a servo motor that controls each servo motor independently through the user-interface. I'm using Processing as the interface and Arduino as the IDE.

I have tried adding other slider objects but they all still control the same servo. I do not know if the issue is through Arrduino or Processing. When I add the other sliders, I connect them to their own pins but it still doesn't allow for them to be controlled individually.

Processing code:

import processing.serial.*;
import cc.arduino.*;
import controlP5.*;
ControlP5 controlP5;
Arduino arduino;
int servoAngle = 90;
void setup() {

 size(400,400);

 println(Arduino.list());
 arduino = new Arduino(this, Arduino.list()[0], 57600);

 for (int i = 0; i <= 13; i++)
 arduino.pinMode(i, Arduino.OUTPUT);

 controlP5 = new ControlP5(this);
 controlP5.addSlider("servoAngle",0,180,servoAngle,20,10,180,20);

}
void draw() {
 arduino.analogWrite(9, servoAngle);
 //delay(15);
}

Arduino code:

    #include <Servo.h>
    #include <Firmata.h>
    Servo servos[MAX_SERVOS];
    byte servoPinMap[TOTAL_PINS];
    byte servoCount = 0;

void analogWriteCallback(byte pin, int value)
{
  if (IS_PIN_DIGITAL(pin)) {
    servos[servoPinMap[pin]].write(value);
  }
}

void systemResetCallback()
{
  servoCount = 0;
}

void setup()
{
  byte pin;

  Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, 
  FIRMATA_FIRMWARE_MINOR_VERSION);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  Firmata.begin(57600);
  systemResetCallback();

  // attach servos from first digital pin up to max number of
  // servos supported for the board
  for (pin = 0; pin < TOTAL_PINS; pin++) {
    if (IS_PIN_DIGITAL(pin)) {
      if (servoCount < MAX_SERVOS) {
        servoPinMap[pin] = servoCount;
        servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin));
        servoCount++;
      }
    }
  }
}

void loop()
{
  while (Firmata.available())
    Firmata.processInput();
}

I would like to be able to add 3 more sliders that to the one already created but be able to control 4 servo motors, each controlled by its own slider, but the result I'm getting is that each additional slider is controlling the same motors.

1

1 Answers

-1
votes

totally depend on which hardware you are running. but i think you have to change a few places

(1) decelar all trackbar value and the pwm

 int servoAngle = 90; int servoAngle1 = 90; int servoAngle2 = 90; int servoAngle3 = 90;

(2) is add track bar

controlP5.addSlider("servoAngle",0,180,servoAngle,20,10,180,20);
controlP5.addSlider("servoAngle1",0,180,servoAngle1,20,10,180,20);
controlP5.addSlider("servoAngle2",0,180,servoAngle2,20,10,180,20);
controlP5.addSlider("servoAngle3",0,180,servoAngle3,20,10,180,20);

(3) link up the hardware pin. totally hardware depended, not sure which 1 u are using. ill jus tput here as sample

 arduino.analogWrite(9, servoAngle);
 arduino.analogWrite(10, servoAngle);
 arduino.analogWrite(11, servoAngle);
 arduino.analogWrite(12, servoAngle);
 //delay(15);
}

that should more or less do it. You know many years ago, we have to write our own processing to arduino serial protocol to do this.