Loading...
Logo
Processing Forum
Hi Everyone,

I am very new to this and hope someone can help.

Im driving an Arduino Tank and require buttons from ControlP5 to perfom an action while pressing the button than stop once released. I can do this function with keyboard but cant get it to work with ControlP5 buttons

Can this be done?

Thanks Bart


Copy code
  1. import processing.serial.*;
    import controlP5.*;

    ControlP5 cp5;
    import cc.arduino.*;

    Arduino arduino;



    int hornbeeper = 11; // piezo/beeper connected to digital pin 11


    PFont fontA;
    PFont chalkF;
    PFont digital;


    PImage chalk;

    PImage horn;
    PImage horn1;



    void setup() {
      size(1024, 683);
     println(Arduino.list());
     arduino = new Arduino(this, Arduino.list()[0], 57600);
     cp5 = new ControlP5(this);
     chalk = loadImage("Chalk.jpg");
     
     //Load Fonts
     fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
     chalkF = loadFont("Chalkboard-Bold-48.vlw");
     digital = loadFont("AmericanTypewriter-70.vlw");
     
     textFont(fontA, 20);
     //background(#FC0808);
     //fill(150); //Text colour
     smooth();
     



        
    cp5.addButton("Horn")
         .setPosition(625, 55)
         .setImages(loadImage("Horn.png"), loadImage("Horn.png"), loadImage("Horn1.png"))
         .updateSize()
         .activateBy(ControlP5.PRESSED)
         ;


     }

    void draw() {
     
      background(chalk); // Set background to dark gray

    fill(#363639, 126);
    noStroke();
    rect(50, 50,350,600);
    rect(425, 50, 550,200);
    rect(425, 350, 550,300);


    //strokeWeight(1);
    fill(250);
    stroke(#FFFFFF);
    line(175, 90, 275, 90); // underline
    line(175, 540, 275, 540); // underline



    //HEADINGS
    textAlign(CENTER);
    textFont(chalkF, 25);
    text("Sensors", 225, 85);
    text("Rules", 225, 535);
    textFont(chalkF,18);
    text("TEMPRETUERE", 225, 185);
    text("HUMIDITY", 225, 285);
    text("LIGHT", 225, 385);


    // Rules
    textFont(chalkF,13);
    text("L/SENSITIVE", 100, 635);
    text("SPEED", 225, 635);
    text("SERVO1", 475, 635);
    text("SERVO2", 550, 635);

    // Rule Settings
    //text(lightsensitivity, 100, 600);
    //text(SPEED, 225, 600);


    // Icon Headings
    text("LIGHTS", 475, 160);
    text("SENSOR", 575, 160);
    text("HORN", 675, 160);
    text("BATTERY", 775, 160);

    //Sesnsor Readings
    textAlign(CENTER);
    textFont(digital,30);
    text("25.5C", 225, 155);
    text("40%", 225, 255);
    text("125", 225, 355);


                                       
     
    }



    void keyPressed(){
    }
    void keyReleased(){
     
    }

    public void controlEvent(ControlEvent theEvent) {
      println(theEvent.getController().getName());
     
    }


    // function buttonA will receive changes from
    // controller with name Horn
    public void Horn(int theValue) {
       //hornval = 1;
      
       arduino.analogWrite(hornbeeper, 950);

      
      
    }

Replies(1)

Hi,
you can use a Callback to catch actions applied to a controller, see sample below (further details see the ControlP5callback example that comes with controlP5):

Copy code
  1. // versions used : processing 2.0b7, controlP5 2.0.4

  2. import controlP5.*;

  3. ControlP5 cp5;

  4. void setup() {
  5.   size(400,400);
  6.   
  7.   cp5 = new ControlP5(this);
  8.   
  9.   Button b1 = cp5.addButton("click").setPosition(100,100).setSize(100,40);
  10.   
  11.   b1.addCallback(new CallbackListener() {
  12.     public void controlEvent(CallbackEvent theEvent) {
  13.       switch(theEvent.getAction()) {
  14.         case(ControlP5.ACTION_PRESSED): println("start"); break;
  15.         case(ControlP5.ACTION_RELEASED): println("stop"); break;
  16.       }
  17.     }
  18.   }
  19.   );

  20. }

  21. void draw() {
  22. }

andreas schlegel, http://www.sojamo.de