three sliders to control three servomotors with ControlP5

edited July 2014 in Arduino

Hi,

I am trying to use 3 different servos with 3 sliders using ControlP5.
The sketch should know which slider (servo) it is and which direction (open or closed) since I need to send that information to Arduino.
So far, processing just displays the values within saying which slider (servo) has moved. Would it be possible to assign a character for each slider? Then, Arduino would check the first character to see which servo it is moving and which direction, e.g., V1--V servo going clockwise, or A-1--A servo going counter-clockwise.

If someone could give me a hint, that would be greatly appreciated.

Thanks.

Lawrence

import processing.serial.*;   // Import processing serial library
import controlP5.*;           // Import controlP5 library
Serial serial;                // Create serial object
ControlP5 cp5;                // Create ControlP5 object

void setup() 
{
  int V = 0;
  int A = 0;
  int D = 0;

  size(720,480);              // Size of window
  smooth();                   // Anti-aliased edges
  cp5 = new ControlP5(this);  // Create new cp5 object
  PFont font = createFont("ArialMT",20);

  cp5.addSlider("V")
     .setLabel("V Valve")
     .setPosition(100,100)
     .setWidth(200)
     .setHeight(20)
     .setRange(-1,1)
     .setValue(0)
     .setNumberOfTickMarks(3)
     .setSliderMode(Slider.FLEXIBLE)
     ;

   cp5.addSlider("A")
      .setLabel("A Valve")
     .setPosition(100,200)
     .setWidth(200)
     .setHeight(20)
     .setRange(-1,1)
     .setValue(0)
     .setNumberOfTickMarks(3)
     .setSliderMode(Slider.FLEXIBLE)
     ;

   cp5.addSlider("D")
     .setLabel("D Valve")
     .setPosition(100,300)
     .setWidth(200)
     .setHeight(20)
     .setRange(-1,1)
     .setValue(0)
     .setNumberOfTickMarks(3)
     .setSliderMode(Slider.FLEXIBLE)
     ;

  // Set texts for controlling servomotors   
  cp5.addTextfield("Closed")
     .setPosition(100,350)
     .setSize(100,1)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     .setAutoClear(false)
     ;
  cp5.addTextfield("Stop")
     .setPosition(192,350)
     .setSize(100,1)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     .setAutoClear(false)
     ;
  cp5.addTextfield("Open")
     .setPosition(290,350)
     .setSize(10,1)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     .setAutoClear(false)
     ; 
}

void draw()
{
  background(0);                // Set background color to black
}

void controlEvent(ControlEvent c)
{
  println(c.getValue());       // Display value of sliders and toggle
}

Answers

  • edited July 2014

    I don't get what you are asking. but I interpret it as you want to send the data to arduino.. you use .write to send data..

    import processing.serial.*;   // Import processing serial library
    import controlP5.*;           // Import controlP5 library
    Serial serial;                // Create serial object
    ControlP5 cp5;                // Create ControlP5 object
    int delay=0;
    
    void setup() 
    {
      size(720,480);              // Size of window
      smooth();                   // Anti-aliased edges
      cp5 = new ControlP5(this);  // Create new cp5 object
      PFont font = createFont("ArialMT",20);
    
      // Open whatever port is the one you're using.
      String portName = Serial.list()[0];
      serial = new Serial(this, portName, 9600);
    
      cp5.addSlider("V")
         .setLabel("V Valve")
         .setPosition(100,100)
         .setWidth(200)
         .setHeight(20)
         .setRange(-1,1)
         .setValue(0)
         .setNumberOfTickMarks(3)
         .setSliderMode(Slider.FLEXIBLE)
         ;
    
       cp5.addSlider("A")
          .setLabel("A Valve")
         .setPosition(100,200)
         .setWidth(200)
         .setHeight(20)
         .setRange(-1,1)
         .setValue(0)
         .setNumberOfTickMarks(3)
         .setSliderMode(Slider.FLEXIBLE)
         ;
    
       cp5.addSlider("D")
         .setLabel("D Valve")
         .setPosition(100,300)
         .setWidth(200)
         .setHeight(20)
         .setRange(-1,1)
         .setValue(0)
         .setNumberOfTickMarks(3)
         .setSliderMode(Slider.FLEXIBLE)
         ;
    
      // Set texts for controlling servomotors   
      cp5.addTextfield("Closed")
         .setPosition(100,350)
         .setSize(100,1)
         .setFont(font)
         .setFocus(true)
         .setColor(color(255,0,0))
         .setAutoClear(false)
         ;
      cp5.addTextfield("Stop")
         .setPosition(192,350)
         .setSize(100,1)
         .setFont(font)
         .setFocus(true)
         .setColor(color(255,0,0))
         .setAutoClear(false)
         ;
      cp5.addTextfield("Open")
         .setPosition(290,350)
         .setSize(10,1)
         .setFont(font)
         .setFocus(true)
         .setColor(color(255,0,0))
         .setAutoClear(false)
         ; 
    }
    
    void draw()
    {
      background(0);                // Set background color to black
    }
    
    void controlEvent(ControlEvent c)
    {
    
    //delay timer because for some reason it runs controleEvent 4 times. when u press somthing..
      if(millis() > delay){ 
        delay=millis()+100;
        println(c.getValue());       // Display value of sliders and toggle
        println(c.getController().getName());  // Display name of slider.
    //sends to arduino. the string that is sent is somthing like "A1.0" if i used A and set it to 1..
        serial.write(c.getController().getName()+c.getValue()+"\r");
    }
    
    }
    
  • edited July 2014

    and for the arduino it would be somthing like. but i dont have an arduino so cant rly test anything, but i dont see why it would not work.

    String strCompare1 = "A0.0";
    String strCompare2 = "A-1.0";
    String strCompare3 = "A1.0";
    String strCompare4 = "V0.0";
    char inByte;
    String strInput = "";
    
    void setup(){
     Serial.begin(9600);
    }
    
    
    void loop(){
    
    
    
     while (Serial.available() > 0)
     {
      // get incoming byte:
      inByte = Serial.read();
      if (inByte == '\r'){
       //compare input message
       if(strInput.equals(strCompare1)){
        //If A 0 do stuff
       }
       if(strInput.equals(strCompare2)){
        //If A -1 do stuff
       }
       if(strInput.equals(strCompare3)){
        //If A 1 do stuff
       }
       if(strInput.equals(strCompare4)){
        //If V 0 do stuff
       }
    
    
       //reset strInput
       strInput = "";
      }
      else
      {
      strInput += inByte;
      }
     } 
    
    
    }
    
Sign In or Register to comment.