Checking button states over time to delay the sending of a signal
in
Programming Questions
•
1 month ago
Hey there,
I have this sketch that is connected to an arduino with two buttons and uses the oscP5 library. I have it configured so that the sketch sends an osc signal when both buttons have been pressed for over two seconds and resets the signal when one or both of the buttons become unpressed.
My problem is when both buttons are pressed and the signal is sent and then one is of the buttons is released that the signal resets straight away. I want the sketch to be able to wait for two seconds to check if the released button is pressed again in that time so that the signal will not reset or will wait for 2 seconds.
I'm basically trying to avoid an on/off glitchy situation. Any help would be greatly appreciated or any suggestions to improve the overall code.
** code revised** 11 sept 13
I have this sketch that is connected to an arduino with two buttons and uses the oscP5 library. I have it configured so that the sketch sends an osc signal when both buttons have been pressed for over two seconds and resets the signal when one or both of the buttons become unpressed.
My problem is when both buttons are pressed and the signal is sent and then one is of the buttons is released that the signal resets straight away. I want the sketch to be able to wait for two seconds to check if the released button is pressed again in that time so that the signal will not reset or will wait for 2 seconds.
I'm basically trying to avoid an on/off glitchy situation. Any help would be greatly appreciated or any suggestions to improve the overall code.
** code revised** 11 sept 13
- import processing.serial.*;
import cc.arduino.*;
import oscP5.*;
import netP5.*;
PFont arial;
Arduino arduino1;
// Code For TIMING and BUTTONS
int led = 13;
int btn = 2;
int btn1 = 3;
String displayText1;
//String headphone;
//String blindfold;
String btnpressed;
int count; // How long the button was held (secs)
int TimePressed; //time button has been HIGH for
int check = 0; //check previous value of button
int firstTime; // how long since the button was first pressed
//CODE FOR OSC SIGNALS
OscP5 oscP5;
NetAddress myBroadcastLocation;
void setup()
{
size(800,600);
background(0);
arial = createFont("Arial", 32);
arduino1 = new Arduino(this, Arduino.list()[11], 57600);
arduino1.pinMode(btn, Arduino.INPUT);
arduino1.pinMode(btn1, Arduino.INPUT);
arduino1.pinMode(led, Arduino.OUTPUT);
arduino1.digitalWrite(led, Arduino.LOW);
oscP5 = new OscP5(this,12000);
//Change PORT HERE
myBroadcastLocation = new NetAddress("127.0.0.1",1234);
}
void draw()
{
textFont(arial);
int BtnVal = arduino1.digitalRead(btn);
int BtnVal1 = arduino1.digitalRead(btn1);
if(BtnVal == Arduino.HIGH){
// OscMessage myOscMessage = new OscMessage("/VolumeOn");
// myOscMessage.add(1);
// oscP5.send(myOscMessage, myBroadcastLocation);
background(0);
fill(255);
String headphone = "Headphones : ON";
textSize(32);
text(headphone, 400, 100);
println("Headphones ON");
}
if (BtnVal == Arduino.LOW) {
background(0);
fill(255);
String headphone = "Headphones : OFF";
textSize(32);
text(headphone, 400, 100);
println("Headphones OFF");
}
if(BtnVal1 == Arduino.HIGH) {
println("Blindfold ON");
fill(255);
String blindfolds = "Blindfolds : ON";
textSize(32);
text(blindfolds, 400, 200);
}
else{
println("Blindfold OFF");
fill(255);
String blindfolds = "Blindfolds : OFF";
textSize(32);
text(blindfolds, 400, 200);
}
if(BtnVal == Arduino.HIGH && BtnVal1 == Arduino.HIGH && check == 0){
check = 1;
firstTime = millis()/1000;
println("Both Pressed at : " + firstTime + " Seconds");
fill(255);
String btnpressed = "Both Pressed at : " + firstTime + " Seconds";
textSize(32);
text(btnpressed, 200, 400);
}
if(BtnVal == Arduino.HIGH && BtnVal1 == Arduino.HIGH && check == 1){
TimePressed = millis()/1000 - firstTime;
println("Both Been Pressed For: " + TimePressed);
fill(255);
String btnpressed = "Both Been Pressed For: " + TimePressed;
textSize(32);
text(btnpressed, 200, 400);
if (TimePressed >= 2 && TimePressed <= 718) {
println("5 secs passed");
arduino1.digitalWrite(led, Arduino.HIGH);
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(1);
oscP5.send(myOscMessage, myBroadcastLocation);
println("Button Turned ON");
fill(255);
String displayText1 = "Movie Playing";
text(displayText1, 100, 100);
textSize(32);
fill(0, 0, 0);
fill(255);
textSize(32);
text(btnpressed, 200, 400);
}
if (TimePressed >= 719) {
println("OVER");
arduino1.digitalWrite(led, Arduino.LOW);
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(0);
oscP5.send(myOscMessage, myBroadcastLocation);
println("Turned OFF");
fill(255);
textSize(32);
String displayText1 = "Movie Over";
text(displayText1, 100, 100);
}
}
if (BtnVal == Arduino.LOW && BtnVal1 == Arduino.LOW && check == 1)
{
count = millis()/1000 - firstTime;
check = 0;
println(" button pressed for : " + count + " Seconds");
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(0);
oscP5.send(myOscMessage, myBroadcastLocation);
arduino1.digitalWrite(led, Arduino.LOW);
println("Turned OFF");
}
if (BtnVal == Arduino.HIGH && BtnVal1 == Arduino.LOW && check == 1)
{
count = 0;
check = 0;
println(" button pressed for : " + count + " Seconds");
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(0);
oscP5.send(myOscMessage, myBroadcastLocation);
arduino1.digitalWrite(led, Arduino.LOW);
println("Turned OFF");
}
if (BtnVal == Arduino.LOW && BtnVal1 == Arduino.HIGH && check == 1)
{
count = 0;
check = 0;
println(" button pressed for : " + count + " Seconds");
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(0);
oscP5.send(myOscMessage, myBroadcastLocation);
arduino1.digitalWrite(led, Arduino.LOW);
println("Turned OFF");
}
if (BtnVal == Arduino.LOW && BtnVal1 == Arduino.LOW && check == 0)
{
OscMessage myOscMessage = new OscMessage("/Pulse");
myOscMessage.add(0);
oscP5.send(myOscMessage, myBroadcastLocation);
arduino1.digitalWrite(led, Arduino.LOW);
println("Standby");
}
}
1