We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5button/ControlP5button.pde
I dont understand, how that will check whether the button was pushed or not in my code? Also isn't getName for strings right?
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
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
Ah ok. And kfajer, I edited your function, to this:
and my issue is in my void draw, I have
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.
For this part of the code I am shooting in the dark. The reason are:
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:
If Servo4 is a controlP5, then you need to update the value of the value field:
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
Thx kfrajer, and can you look at my other discussion for the reliability?