How to get code-snippet to only 'loop' once?


I have a motor with rotary encoder. And code to count the pulses, and ability for this motor to go up and down, to save a position and to erase this position from memory.
When the motor goes by this position, it stops for 1.5 second (delay).
But the problem here is that it continues to loop this code, and continue to loop the message “Users Stop”.
I can get out of it by pressing up, but it takes some time for the 1.5 seconds to finnish and then it moves out of its position.
If I could get it to “read” this stop only once, and display the message only once, it would just work better and be more user friendly.
Any suggestion on how can do this? (I am a newbie to this so plz be kind).
Should I have a variable of some sort? Or is there a more obvious way to solve this?

Here is the code snippet that recognises the saved position, and makes the motor pause for 1.5 second.

  // ---- This code stops the motor at the saved position.

    if ( UserStopPulseCount != 0 && PulseCount >= UserStopPulseCount - 15 && PulseCount <= UserStopPulseCount + 15 )
      digitalWrite(controlPin1, LOW);
      digitalWrite(controlPin2, LOW);
      lcd.setCursor(0, 1);
      lcd.print("Users Stop");
      Serial.println("Users programmed STOP activated");


You need to post the complete program so we can see the context.

A common way to ensure a piece of code only runs once is to have a variable that counts how many times it runs.


Because you have:

    if ( UserStopPulseCount != 0 && ...

you could add this line inside it to prevent it from triggering again until UserStopPulseCount is set again:

        UserStopPulseCount = 0;


But the problem here is that it continues to loop code, and continue to loop the message “Users Stop”…

Don’t over think this, it is really very simple.

The loop function is called repeatedly. So, within loop, only call your section of code if a flag say UserStopActivated is false (it should be initialised to false). Within your section of code set UserStopActivated to true. Your section of code will then only be called once, unless you set UserStopActivated to false again somewhere.

Ok, thanks guys, will re-read your comments right after this and try to better the code.

Im trying to post the complete code here but I cant, it exceeds 9000 characters.
Anyway around this?

Click the reply button (as shown in screenshot) and the attach option becomes available. Then you can attach your code file.

forum reply.jpg

Now I have the code attached here. :slight_smile:

So a short description of…

It has a motor with a rotary encoder. Its connected via a H-bridge.
The motor can run UP and DOWN when pressing button 1 or button 2.
Button 3 can save an position from the rotary encoder, so that when passing by it pauses/stops there when passing by.
Button 4 removes this position.
Button 5 and 6, plays a little melody. No function really, and just for test.
It has also a couple of IR sensors connected that stops the motor from going too far up or down, and one third sensor that can be placed anywhere one want to.
I have a I2C 2x16 LCD display connected to it, so I can view the pulses from the rotary encoder, and se short messages like “Going up” or “Saving position”.

Right now, if I restart the machine it forgets where the motor is positioned last time. It starts at 0 (zero) everytime even though I turned it off at positionpulse for example 560. So the saved self-pause-position then is useless. And I have to re-save a new position everytime I have restarted the machine. I would want this machine to remember where the motor was when restarted. Ill guess I’ll have to have some code that save the position everytime I press Up or Down button. But problem here is that I dont know how to create a “second” part in the memory where I can save this position. How to do? :o

Another annoying problem (and the cause for this threadstart) is that when I do save the position that I can save, it keeps looping at this part of the code. So it says “Users stop” and keep on adding the 1.5 sec delay. I can go out of this by keep Up or Down button pressed in for at least 1.5 sec, but it takes some time and is not very user friendly.
I would like this message, and the delay, only to be executed Once when pressing “Save position” and when its at the position where the position was saved.

I am a newbie, so pls forgive the codes eventual rudeness.

Test_61_-_Skicka.ino (16 KB)

Right now, if I restart the machine it forgets where the motor is positioned last time.

I don’t see that. What I see is that you read a value from EEPROM, but do nothing with it. You don’t print it. You don’t make the motor go to that position.

  lcd.setCursor(0, 0);

  if (PulseCount > -1) {
    lcd.print(" ");

  if (PulseCount < 10 && PulseCount > -10) {
    lcd.print(" ");

  if (PulseCount < 100 && PulseCount > -100) {
    lcd.print(" ");


This belongs in a function that you call from loop(), not in loop().

Your code is just littered with useless curly braces. Get rid of them.

How could I "translate" this to a function?
And call it from the loop?

How could I "translate" this to a function?
And call it from the loop?

The Arduino IDE uses C++ but that is really a superset of C. Try reading this book to find out how functions etc work.

Then write very simple programs that just have print statements in the loop and in your own functions then build up to the final code. That way you will know how everything works.

How could I "translate" this to a function?
And call it from the loop?

Have a look at Planning and Implementing a Program in conjunction with the advice given by @ardly