How to end a digitalRead?

Hi there! :slight_smile: 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. :confused: 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 :slight_smile:

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 :stuck_out_tongue:
NickGammon - Thanks for the link! ill check it out. :slight_smile:

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

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.