Can I run a command only once in loop?

Hey guys.

So I have this school project with my friends and we want to built a candy dispenser with Arduino, a servo motor and a switch.
We have an On/On switch and use it as an On/Off Switch (it has 3 pins/legs and we use one in the middle and one on the outside, so that it’s on when the switch is turned on the opposite side from the outside pin (e.g. pins are in the middle and the left side, so the switch is on when it’s on the right side)).
When the switch is turned on, the servo opens a small hole, where the candy slides out from (3 seconds long). But because the switch is still on, I would have to turn it back off every time, because the servo command is in loop. (but in a weird pattern, like even if I’ve switched to off it’d still move one time and then stop, so not immediately)
My question is, is there a command that can run a command in loop only once? Because then I could use the On/On switch and when it’s switched once e.g. left (and the candy slides out) it won’t do it a second time because the command wouldn’t run a second time or just stop and only run again, when it’s switched to right side.
Like when the switch read stuff is high, it does x and only once.
Is there such a thing?
I’ve also thought about using the for loop but I’m not sure if it would work….
I know it’s hard to understand, but English is not my first language so please bear with me.
Feel free to ask more details, I'm really desperate.... My teacher couldn't help as well

There is no special command for what you want to do, because no special command is needed for this. What you need is not a special command, just a simple technique. It may not seem relevant at first, but the technique you need is used in the "State Change" example sketch in the IDE.

You could also use a flag (define it globally as a boolean variable) and set it to false. When the activity has run once (the candy has been dispensed) , set the flag to true. You can use an 'if' statement to test if the flag has been set (or not) to avoid repeating the activity.

All the suggestions above are valid.
If the code is going to run only once when power is applied, why not put it in setup().
Unless there’s more than you’ve explained.

You are writing automation that happens over time. You can in a way measure time by runs through loop().

Some things are going on in time. Your sketch keeps track of those by setting values into variables.

Is the key turned left or right? Use a byte variable, are there only 2 possible states for the key?

Did the candy drop? Do you detect that? How else to know when to swing the servo back to closed?
Will your system only allow 1 candy to drop at a time?

If you have a key state and a candy state then key left candy not dropped means drop the candy but once the candy drops it takes a key right state to reset the candy drop state so that key to left again gets candy.

Depending on the state(s), you run different handling code inside of loop().

But note this:
You also have code in the same loop() that only watches the key and updates the key state every time the key changes.
You also have code in the same loop() that only watches the candy and updates the candy state when candy drops.

There is no problem with this, loop() just runs them in order over and over. Just make sure there is no if() that must be true for the sensing code to run and never use a delay unless you want to deal with timing bugs.

Welcome to the forum !

the best suggestion we can offer someone who has no programming experience is to start with the free examples.

load the Arduino program, get some resistors and some LED's and your switches and try some of the sample programs.

after you play around a bit, it will become much clearer as to what the Arduino does and how you can make it do what you are asking.

Hi,
Welcome to the forum.

What model Arduino do you have?
Can you please post your code?
This will help us help you develop your code.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need a circuit diagram so we can see how you have connected your components.

What school level is this, cos this is a great project to learn to get Arduino to control stuff.
Can we ask what your age is?

Thanks.. Tom.. :slight_smile:

Following on to @PaulRB's recommendation, your goal here is not to sense that the switch IS on but that it just CHANGED to on. This is what the state change detection example shows.

In the IDE: File/examples/digital/StateChangeDetection.

Instead of a toggle switch a momentary push button sounds more sensible for this project. Then you don't have to worry about it having to be switched back off.

There's a customer involved. Customers making poorly thought out decisions is like rain falling down instead of up, normal reality! When they quit with the lame demands, start checking on if you're dead and went to heaven or are being set up for a massive punking. (as in good bye to bonuses and back pay, it's never just for smiles when money and cheats are involved)