# How to end a digitalRead?

Hi there! I had a question about the digitalRead function. Is there a way I could end the digitalRead after the input becomes HIGH? So, if I had a function start when my switch was HIGH, I would need the digitalRead to stop and still continue the function has already started.

The digitalRead() function doesn't stop and wait for the value to change - it returns with the current value. If you want to keep re-reading it until you get a certain value, you can use a loop to do that.

``````while(digitalRead(pin) == HIGH)
{
Â  Â  // keep going round in the while loop until the pin state stops being HIGH
}

// now we know the pin state is LOW
``````

Ahh. Thanks for the help!
I donâ€™t know what Iâ€™ll do then. I have a robot that runs on a straight track.
And the ends of the track there are switches that the robot runs into to stop the robot from going further. (Limit switches)
But the switches will be HIGH for a short time because the robot must go in the opposite direction and then start a digitalRead on the opposite switch. I know this is kinda confusing

have you thought about having active low switches?

You need to detect a state change. From LOW to HIGH. Act when that happens.

JakeDuino - I dont quite understand. what do you mean? sorry im still a little new to this
NickGammon - Thanks for the link! ill check it out.

tjrusk12:
JakeDuino - I dont quite understand. what do you mean? sorry im still a little new to this
NickGammon - Thanks for the link! ill check it out.

Actually, the active HIGH vs. active LOW doesn't matter.
Look at this psuedocode...

``````void loop() {
Â  Â  command robot to do something
Â  Â  while pin is LOW {
Â  Â  Â  Â   cycle here, doing whataver you need to do
Â   }

Â   command robot to do something else
Â  Â  while a different pin is LOW {
Â  Â  Â  Â   cycle here, doing whatever you need to do
Â  Â  }
}
``````

This will do pretty much what you want to do, where the first "command robot" is to run it in one direction, and the second is to run it in the other direction. Loop() will get you back to the top.

i understand now! thanks for the help!
i have one last question. could it be possible to do this same function with only one switch?

tjrusk12:
i have one last question. could it be possible to do this same function with only one switch?

tjrusk12:
i have one last question. could it be possible to do this same function with only one switch?

Well, it all depends on what you're trying to control, the physical movements you require, and the physical positions of the switches. If your limit switches are separated by a distance, say a metre or more, you would probably need two separate switches.

If the switch is actually on your robot, for example, a switch on the 'front' of the robot, that would detect running into an obstacle, then yes, a single switch would be sufficient. you would do something like this:

``````void loop() {
Â  Â  Â  Â  command robot to move forward
Â  Â  Â  Â   while obstacle_switch is HIGH {
Â  Â  Â  Â  Â  Â   keep moving forwardÂ  Â  // until obstacle switch activates (goes LOW)
Â  Â  Â  Â   }
Â  Â  Â  Â   command robot to move backward
Â  Â  Â  Â   while obstacle_switch is LOW {
Â  Â  Â  Â  Â  Â   keep moving backwardÂ  Â  // until obstacle switch deactivates (goes HIGH)
Â  Â  Â  Â   }
Â  Â  Â  Â  command robot to turnÂ  Â  // at this point, we loop and start moving forward again.
}
``````

Of course, you will want more complex actions. Better results will be had with two (or more) switches, so that you can better tell which side activated a switch, allowing you to turn in a direction more likely to avoid hitting the same obstacle again. Other sensors can be used... sonar or IR distance-measuring sensors, PIR motion detectors, temperature sensors, light sensors, and so on.

Basically, it's all up to you. There are many ways to program an autonomous robot. Don't be afraid to experiment.