Using external buttons to start parts of the script?

I’m new to using Arduino boards but it was a perfect fit to control two DC motors for my project. Right now I have 6 different scripts set up to run the two motors at slightly different speeds. I was wondering rather than plugging the board into my computer and running the different scripts if there is a way to compile all the different scripts into a one and trigger different parts of the script individually with external buttons. Like I said I am new to all of this so I’m sorry if this is a dumb question or not even possible at all. I looked into external interrupts and I’d imagine that’s how it would work but I couldn’t find anything about starting different parts of the script with it. Thanks for any help!

Yes, that'd be easy to do. Can read all 6 buttons and perform whatever code each does, then read the uttons again and it nothing changed, keep running the same code, or run the code for the new button press.

if there is a way to compile all the different scripts into a one

Copy and Paste

if there is a way to compile all the different scripts into a one

This demo of merging sketches may help to give you the general idea.


I looked into external interrupts and I'd imagine that's how it would work

It absolutely is not. You simply do not understand what "interrupts" are - but for a "newbie", that's quite commonplace. So just forget about them for a long while. :smiley:

Essentially, you need to start with a main "loop()" code to read your buttons by polling, that is, to read one by one, make decisions according to that, and then let the loop cycle back to the beginning again to keep on testing the buttons.

For each button that is detected (and de-bounced), you insert the corresponding part of the code which may come from the loop in another sketch. You have to determine whether it is sufficient for each to be performed one at a time, and whether each of these requires to run in its own "loop" and it becomes much more complicated if you want more than one to execute simultaneously- but it is perfectly do-able to have many running apparently simultaneously.

And the setup() simply has to contain the necessary setups for each sub-function.

Implicit in what Paul__B said there, and which makes polling feasible, is that even a lowly Arduino is running faster than most of our real-world interactions. Don't forget that loop() is exactly that, a loop which runs for ever. The chances are that when you press a switch, loop() will get to you before you release it. Even a quick flick of a button (quick in human hand press / release terms) is likely to take aaaaaages in processor land, so the switch will still be "in" when loop() gets round again.

Probably, anyway, and interrupts exist for the cases where "probably" isn't good enough.