user3260316
user3260316

Reputation:

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

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

Answers (3)

AXOME
AXOME

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

Dejan Kovačević
Dejan Kovačević

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

ch271828n
ch271828n

Reputation: 17607

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

Upvotes: 1

Related Questions