Checking Whether Button Was Pressed or not?

edited April 2017 in Arduino

I just need some help with checking whether or not this button was pressed. If its pressed I will have to write to the servo, instead of writing to the servo automatically.

import processing.serial.*;          //imports serail library
import cc.arduino.*;                 //imports arduino library so I don't have to use arduino IDE
import controlP5.*;                  //Part of processing library


Arduino arduino;
ControlP5 cp5;

Slider Servo1;

void setup() 
{
  size(950, 900);

  println(Arduino.list());                                  //sends data to serial port for arduino
  arduino = new Arduino(this, Arduino.list()[0], 57600);    //sets COMM Port Baud Rate
  cp5 = new ControlP5(this);                                //dynamic variable of control class

  arduino.pinMode(3, Arduino.SERVO);

 Servo1 = cp5.addSlider(".").setRange(0,180).setValue(90).setPosition(110,134).setSize(270,25).setNumberOfTickMarks(270).setLabelVisible(true).setColorForeground(color(102,102,102)).setColorBackground(color(255,153,0)).setColorActive(color(102,102,102));
cp5.addButton("PressButton")
     .setValue(1)
     .setPosition(400,600);
 }

void draw() 
{
 arduino.servoWrite(3, int(Servo1.getValue()));
 }

Answers

  • look at examples

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

    http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5button/ControlP5button.pde

  • edited April 2017

    I dont understand, how that will check whether the button was pushed or not in my code? Also isn't getName for strings right?

  • Answer ✓

    Notice that instantiating the controls seems to trigger the controlEvent() function. This is the reason I added a boolean variable to make sure the function is only available when in running mode. There are two ways to know if a specific button was triggered. A third option is using plugTo: http://www.sojamo.de/libraries/controlP5/examples/use/ControlP5plugTo/ControlP5plugTo.pde

    Kf

    import controlP5.*;
    
    ControlP5 cp5;
    int myColor = color(255);
    boolean setupDone=false;
    
    void setup() {
      size(400, 600);
      noStroke();
      cp5 = new ControlP5(this);
    
      cp5.addButton("playAgain")
        .setValue(128)
        .setPosition(210, 300)
        .updateSize()
        ;
    
      setupDone=true;
    }
    
    void draw() {
      background(myColor);
    }
    
    
    
    public void controlEvent(ControlEvent theEvent) {
    
      if (setupDone==false)
        return;
    
      println(theEvent.getController().getName());
      if (theEvent.getController().getName().equals("playAgain"))
        exit();
    }
    
    //THIS NEXT IS ANOTHER WAY to do things
    //public void playAgain(int theValue) {
    
    //  if (setupDone==false)
    //    return;
    
    //  exit();
    //}
    
  • edited April 2017

    Thank You. So let me get this right, the controller event is link to that specific button because of it being instantiated in void setup?

  • The controller event will detect events from the different controllers instantiated during your program. In the controller event, you need to identify which controller triggered the event so for you to take proper action. For example, if you have a play and stop button, hitting either button triggers the controller event, and based on the button, you will play of stop a video object for example.

    Now, something I found out using controlP5 is that when you create any controllers in setup, the control Event function triggers once for each controller. This is an undesirable behavior of this library as you can see in my example above. It could be cause by either a bug in the actual library or by poor documentation. My hack to this problem is to make sure the controller event is enabled only after setup has executed all its code.

    Kf

  • edited April 2017

    Ah ok. And kfajer, I edited your function, to this:

                int ServoValue=0;
                   public void controlEvent(ControlEvent theEvent) {`  
                      if (setupDone==false)
                        return;
    
                      println(theEvent.getController().getName());        
    
                      if (theEvent.getController().getName().equals("Push Button")){
    
                           ServoValue = int(Servo4.getValue()+15); 
    
                           arduino.servoWrite(4,ServoValue); 
    
    
    
                           delay(3000);
    
                           ServoValue = ServoValue-15; 
                           arduino.servoWrite(4,ServoValue); 
    
                      }
                }
    

    and my issue is in my void draw, I have

    void draw()
            {
    
    
             background(color(51,102,153));                    
    
    
               text("Sending Servo Data: "+ServoValue,360,680);  
    
              arduino.servoWrite(4, int(Servo4.getValue())); 
            }
    

    which based on the control event function, it outputs the value of ServoValue. Now ServoValue changes from +15 of whatever position my Servo4 is at, then -15. Which means the servo makes a movement then goes back to its original position. But the text("Servo data", +ServoValue, 360, 480) function on the processing window for the ServoValue only displays the default, it does not show +15 then -15 of whatever value my ServoValue is at?

    I even used println(), and on the console ServoValue changes, but not on the processing window that shows up.

  • Answer ✓

    For this part of the code I am shooting in the dark. The reason are:

    • I don't know how your servo operates
    • I don't have experience with firmatta
    • I am not able to test your code

    Btw, what is Servo4.getValue()? Is that a controlP5 object?

    Notice you are writing to your servo in draw. When you do that in testing mode (what you are doing now), you have not control of these events as they are happening 30 or even 60fps. I suggest this instead:

    void draw(){}
    
    void keyReleased(){
      background(color(51,102,153));                    
      text("Sending Servo Data: "+ServoValue,360,680);   
      arduino.servoWrite(4, int(Servo4.getValue())); 
    }
    

    Which means the servo makes a movement then goes back to its original position. But the text("Servo data", +ServoValue, 360, 480) function on the processing window for the ServoValue only displays the default, it does not show +15 then -15

    If Servo4 is a controlP5, then you need to update the value of the value field:

    ServoValue = int(Servo4.getValue()+15);
    Servo4.setvalue(ServoValue);
    

    Please notice that it is customary to write the first letters of variables and instantiated objects in lower case: servoValue, servo4 instead of ServoValue, Servo4. For classes, the first letter is always capital: class MyCustomObkect {}.

    If I were you, I will be more interested to know if my servo does what it suppose to do. For example, if you write to it, does it turn consistently? Does it go back and forth? For example, check the code below. Use the left/right arrow keys to issue turn commands to your servo.

    Kf

    import processing.serial.*;          //imports serail library
    import cc.arduino.*;                 //imports arduino library so I don't have to use arduino IDE
    
    final int kStep=45;  //Increment step
    
    Arduino arduino;
    int svalue;
    
    void setup() 
    {
      size(950, 900);
      textAlign(CENTER,CENTER);
      fill(255);
                                   //sends data to serial port for arduino
      arduino = new Arduino(this, Arduino.list()[0], 57600);    //sets COMM Port Baud Rate 
      arduino.pinMode(3, Arduino.SERVO);
      svalue=0;
    
    }
    
    void draw() {
      background(0);
      text("Current value "+svalue,width/2,height/2);  
    }
    
    void keyReleased() {
    
      if(key == CODED){
        if(keyCode==RIGHT){
          svalue+=kStep;
        }
        if(keyCode == LEFT){
          svalue-=kStep;
        }
      }
    
      arduino.servoWrite(3, svalue);
    
    }
    
  • Thx kfrajer, and can you look at my other discussion for the reliability?

Sign In or Register to comment.