I have a question.

I made a system which includes a servo, a stepper and a wiper motor :) All these parts has one job to do and I can code it. But, I want to make 2 modes in this system Single Operation, Loop operation.

for example position 1-single operation, 2-loop operation. I can code the loop operation thanks to arduino syntax :)

But I cannot code single one. I can read switch state detection. I want to do exactly same things, but only one time when slide switch on the position.

The code is really long and it includes Turkish defines. If you need to see the code, let me know.

1 Answer 1


Let's assume the switch is on pin 9, that the pinMode() has already been set, and that the switch reads HIGH for loop operation.

#define MODE_SWITCH  9
#define DO_LOOP  HIGH

// This loop will execute once if the mode-switch is off, or
// will execute continously if the mode-switch is on:
do {
} while( digitalRead(MODE_SWITCH) == DO_LOOP );

in C/C++, 'do-while' loops always execute at least once, since the condition isn't tested until after the first pass through the loop. If the 'while' condition is satisfied (or TRUE), the loop will execute again, otherwise the loop will exit.

  • I think you should do a digitalRead() inside the while condition. I think you meant that and only wrote it simplified, but since this isn't written in pseudo code, others might copy the code directly, what means to compare 9 to 1, which is always false.
    – chrisl
    Dec 14, 2019 at 20:42
  • Good catch, @chrisl - that was exactly right. Tthanks. Fixed now.
    – JRobert
    Dec 15, 2019 at 0:06
  • I'll try this. Thanks to you
    – can
    Dec 15, 2019 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.