digitalRead() Arduino, will it wait for a key to be pressed? - arduino-uno

This is my Arduino code:
void loop()
{
state=digitalRead(2);
if(state==HIGH)
{
update();
}
}
I want the function update() to be called if the button in pin2 is pressed.
Will 'state=digitalRead(2)' this statement wait for the key press? If no, what do you suggest?

Of course not. This function will return the current state of that pin immediately.

This code can replace all your code through your function loop()
void setup()
{
attachInterrupt(2, update, RISING);
}
void loop()
{
}
void update()
{
...
}

Try to create a while loop to wait for a button press:
while (!digitalRead(2)) {
delay(100);
}
So your script gets stuck in a loop and waits for a button press.

Related

Is it possible to put a variable in a key() function?

I am trying to make a calculator in processing. My thought process for how this is going to work is to make a function for if a certain number is pressed on the keyboard. So I would create some sort of for loop or array even that would sense when a number is pressed, and then return true if it goes through the if statements, however, in order for this to work, I would need to put a variable in the place of a specific key on the keyboard. Is this possible?
Code (so far):
void setup() {
size(800,600);
}
void draw() {
background(0);
Nums.create();
}
class Nums {
void create() {
for (int i = 0; i < 9; i++) {
zero(i);
}
}
boolean zero(int amnt) {
if (keyPressed) {
if (key == amnt) {
return true;
} else {
return false;
}
}
}
}
They keyPressed variable and the functions like keyPressed() and keyReleased() are specifically for keyboard input. They don't, and shouldn't, be concerned with on-screen buttons that you click with the mouse.
Instead, you should probably use the mousePressed() function to detect when the mouse is clicked, and then use if statements to figure out which button was clicked. You can use point-rectangle collision detection to detect which button was clicked. There's also a button sketch in the examples that come with the Processing editor.

does setup and draw function run parallel in processing

Does the fileselected function in this code complete its execution even before while loop execution?
void setup()
{
size(800, 600);
selectInput("Select a file to process:", "fileSelected");
while(data==null)
{
delay(1000);
}
}
void fileselected()
{
*
*
*
*
}
How do I make the draw function wait until it receives the necessary arguments to run?
does setup and draw function run parallel in processing
No. First the setup() function is called and completes, then the draw() function is called 60 times per second.
does the fileselected function completes its execution even before while loop execution.
The fileSelected() function will be called when the user selects a file. You really shouldn't call the delay() function in a loop like that.
How do I make the draw function to wait until it receives the necessary arguments to run.
Something like this:
boolean fileLoaded = false;
void setup(){
size(800, 600);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection){
fileLoaded = true;
}
void draw(){
if(!fileLoaded){
//short-circuit and stop the function
return;
}
}
You could go a step further and use the noLoop() and loop() functions. More info can be found in the reference.

GWT hold mouse button

I'm working with GWT and want to do an action when the user holds the left mouse button on a GWT button. But I can't find the right event handler or another solution for that problem.
Is there a way in GWT to klick on a button, hold the mouse button and do the same action again and again till the mouse button is released?
Button scrollUpBtn = new Button("Top");
scrollUpBtn.setWidth("66px");
scrollUpBtn.addMouseDownHandler(new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
//handCards.setVerticalScrollPosition(handCards.getVerticalScrollPosition() - 10);
mouseUp = true;
}
});
scrollUpBtn.addMouseUpHandler(new MouseUpHandler() {
#Override
public void onMouseUp(MouseUpEvent event) {
mouseUp = false;
}
});
scrollUpBtn.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if (mouseUp == true) {
handCards.setVerticalScrollPosition(handCards.getVerticalScrollPosition() - 10);
}
}
});
Step 3 in Andrei's answer assumes that the KeyDownEvent will keep firing. I'm not sure if that's so..
An alternative would be to use the Down & Up handlers to start/stop a repeating timer which carries out your action. You can then set the repeat interval based on how often you want your action to be carried out. Remember that this runs as single threaded JavaScript, so if you carry out lengthy processing things will slow down and the scheduled intervals will not be on time.
Button btn= new Button("Button");
final Timer actionTimer = new Timer() {
#Override
public void run() {
// Your action here
System.out.println("Doing something!");
}
};
btn.addMouseDownHandler(new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
// Choose the appropriate delay
actionTimer.scheduleRepeating(1000);
}
});
btn.addMouseUpHandler(new MouseUpHandler() {
#Override
public void onMouseUp(MouseUpEvent event) {
actionTimer.cancel();
}
});
Add MouseDownHandler to your widget. When it fires, it should set a flag to true, e.g. mouseDown = true;
Add MouseUpHandler to your widget. When it fires, it should set a flag to false, e.g. mouseDown = false;
Add KeyDownHandler to your widget. When if fires, check if flag is true - then do something. If flag is false, don't do it.

catch touch event on blackberry api5

I want to catch the touch event in OS5. I use this method protected boolean touchEvent(TouchEvent message) in ListFieldRich. But this method didn't run it. I press all keys nothing happens. Even if I debug my code, this method didn't run when press keys.
How can I know the touch event in OS5?
Thanks
Touchevent will be called if you have a touch screen phone, and you touch the screen. It will not be called if you press the keys or buttons. Did you touch the screen?
I resolved my problem. I used this method protected boolean trackwheelClick(int status, int time)
setChangeListener() on your RichTextField and override these method in your Screen class
protected boolean navigationClick(int status, int time) {
if(Touchscreen.isSupported()) {
return false;
}
fieldChangeNotify(1);
return true;
}
//for touch
protected boolean touchEvent(TouchEvent message) {
if (TouchEvent.CLICK == message.getEvent()) {
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
and in fieldChanged() method
public void fieldChanged(Field field, int context) {
if (field == yourRichTextField) {
Dialog.inform("RichtextField Clicked Button Pressed");
}

Can mousePressed be defined within a class?

I am trying to create a class called InputManager to handle mouse events. This requires that mousePressed be contained within the InputManager class.
like so
class InputManager{
void mousePressed(){
print(hit);
}
}
problem is, that doesn't work. mousePressed() only seems to work when it's outside the class.
How can I get these functions nicely contained in a class?
Try this:
in main sketch:
InputManager im;
void setup() {
im = new InputManager(this);
registerMethod("mouseEvent", im);
}
in InputManager Class:
class InputManager {
void mousePressed(MouseEvent e) {
// mousepressed handling code here...
}
void mouseEvent(MouseEvent e) {
switch(e.getAction()) {
case (MouseEvent.PRESS) :
mousePressed();
break;
case (MouseEvent.CLICK) :
mouseClicked();
break;
// other mouse events cases here...
}
}
}
Once you registered InputManger mouseEvent in PApplet you don't need to call it and it will be called each loop at the end of draw().
Most certainly, but you are responsible for ensuring it gets called:
interface P5EventClass {
void mousePressed();
void mouseMoved();
// ...
}
class InputManager implements P5EventClass {
// we MUST implement mousePressed, and any other interface method
void mousePressed() {
// do things here
}
}
// we're going to hand off all events to things in this list
ArrayList<P5EventClass> eventlisteners = new ArrayList<P5EventClass>();
void setup() {
// bind at least one input manager, but maybe more later on.
eventlisteners.add(new InputManager());
}
void draw() {
// ...
}
void mousePressed() {
// instead of handling input globally, we let
// the event handling obejct(s) take care of it
for(P5EventClass p5ec: eventlisteners) {
p5ec.mousePressed();
}
}
I would personally make it a bit tighter by also passing the event variables explicitly, so "void mousePressed(int x, int y);" in the interface and then calling "p5ec.mousePressed(mouseX, mouseY);" in the sketch body, simply because relying on globals rather than local variables makes your code prone to concurrency bugs.
The easiest way to do so would be:
class InputManager{
void mousePressed(){
print(hit);
}
}
InputManager im = new InputManager();
void setup() {
// ...
}
void draw() {
// ...
}
void mousePressed() {
im.mousePressed();
}
This should solve any problems you were having with variable scoping in your class.
Note: In the class, it doesn't even have to be named mousePressed, you can name it anything you wish, as long as you call it inside of the main mousePressed method.

Resources