Reputation:
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?
Upvotes: 0
Views: 737
Reputation: 1
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.
Upvotes: 0
Reputation: 1
This code can replace all your code through your function loop()
void setup()
{
attachInterrupt(2, update, RISING);
}
void loop()
{
}
void update()
{
...
}
Upvotes: 0
Reputation: 17607
Of course not. This function will return the current state of that pin immediately.
Upvotes: 1