I need some help assigning the value of a controlp5 slider to a variable

I need to assign the value of the speed slider to a variable and write that variable to the servo. Can you help me Here is my code:

import controlP5.*;
ControlP5 controlP5;  
import processing.io.*;
SoftwareServo servo;


void setup() {
  size(255,100);
  controlP5 = new ControlP5(this);
  controlP5.addSlider("Speed",0,180,0,10,10,200,30);
  servo = new SoftwareServo(this);
  servo.attach(4);
  servo.write(left);
  delay(200);
  exit();
  }

void draw(){
  background(100);

}

Answers

  • edited November 2016

    Format your code. Edit your post, select your code and hit ctrl+o.

    Here are two different ways to access a slider value, or more like how to establish the relationship btw them. I commented out code that was not relevant to my demo. I hope this helps,

    Kf

    import controlP5.*; 
    ControlP5 controlP5;
    //import processing.io.*; 
    //SoftwareServo servo;
    
    int Speed=0;
    int myColor=255;
    
    void setup() { 
      size(255,400); 
      controlP5 = new ControlP5(this); 
      controlP5.addSlider("Speed", 0, 180, 0, 10, 10, 200, 30); 
      //servo = new SoftwareServo(this); 
      //servo.attach(4); 
      //servo.write(left); 
      //delay(200); 
      //exit();
    
        // add a vertical slider
      controlP5.addSlider("slider")
         .setPosition(10,250)
         .setSize(200,20)
         .setRange(0,250)
         .setValue(128)
         ;
    }
    
    void draw() { 
      background(0);
      fill(Speed);
      rect(0, 0, width, 100);
    
      fill(myColor);
      rect(0,200, width, 100);
    }
    
    void slider(float theColor) {
      myColor = color(theColor);
      println("a slider event. setting background to "+theColor);
    }
    
Sign In or Register to comment.