# Programming limit switch?

**URL:** <https://forum.arduino.cc/t/programming-limit-switch/1368203>\
**Category:** Programming\
**Created:** [March 28, 2025, 6:10am UTC](https://forum.arduino.cc/t/programming-limit-switch/1368203 "2025-03-28T06:10:45Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 28, 2025, 11:06am UTC](https://forum.arduino.cc/t/programming-limit-switch/1368203/14 "2025-03-28T11:06:38Z")

</div>

> [@datfreak](#):
>
> i get totally lost when i'm trying to figure out what the program should be doing while its at one end holding that limit switch in a high state.

It should be marking time, that is to say waiting for one hour to pass.

This is easy enough to do easily and would also be fun to take a bit more sophisticated approach.

```arduino
loop
   trundle left until the left limit switch is hit

   do nothing for one hour

   trundle right until the right limit switch is hit

   do nothing for one hour

```

You've said the motor turns one way only, so it's even simpler

```arduino
loop
   trundle until either limit switch is hit
   do nothing for one hour

```

The key is being able to see when the switch becomes pressed, not when it is being pressed.

This is a basic concept it will be worth getting your head around. It is referred to as "edge detection" or "state change detection". There is a demonstration of this in the IDE examlkes and it links to a competent article on the subject.

Fortunately there are also several many other people who have covered the same idea. Multiple perspectives can be helpful; one explanation may land better than another for you.

[https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/](https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/)

Of course we all feel like we've explained it once and perfectly:

> [@Storing Button State](https://forum.arduino.cc/t/storing-button-state/1185825/6):
>
> I normally point ppl to the IDE Examples/02.Digital/StateChangeDetection sketch, and the article that goes with it. Here is an adjustment and reduction of that example, which actually works. And somewhat less code to slog through to get the basic idea. One of the original's flaws is that it does not handle Try it here [Wokwi\_badge] [State Change Detection](https://wokwi.com/projects/380600410686023681) This code also follows the IPO model, which is a very useful and very used model for many the things we do with the Arduino. [IPO Pr…](https://en.wikipedia.org/wiki/IPO_model)

HTH - start turning on and off some LEDs using pushbuttons.

a7

---

_[View the full topic](https://forum.arduino.cc/t/programming-limit-switch/1368203)._
