0

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.

asked Dec 14, 2019 at 17:35

1 Answer 1

3

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 {
 servo_things();
 stepper_things();
 motor_things();
} 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.

answered Dec 14, 2019 at 19:10
3
  • 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. Commented Dec 14, 2019 at 20:42
  • Good catch, @chrisl - that was exactly right. Tthanks. Fixed now. Commented Dec 15, 2019 at 0:06
  • I'll try this. Thanks to you Commented Dec 15, 2019 at 7:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.