need coding help to combine three sketches in one sketch

Hi together,

here is what i want to realize:

  1. I have a little arduino UNO circuit consisting of a servo (pin 11), an RGB-LED (pin 3,4,5), a potentiometer (A0) and a pushbutton (named "sketchbutton"; pin 9; including pull-down resistor).

  2. do have three independent sketches - each one with different controlling of the circuit

  • the first performs a motion when sketchbutton is pressed (lets name it "motion")
  • second performs motion-sequence when sketchbutton is pressed (lets name it "motion-sequence")
  • third allows manually positioning of the servo by help of the poti (lets name it "positioning")

now i would like to combine those three sketches and use another pushbutton (lets name it "choosebutton") (pin 8 ) to choose the sketch that is currently active. For visualization (so that you know you activated the right part of the sketch) the LED should be illuminated in one of the three colours.

  • for motion --> blue
  • for motion-sequence --> green
  • for positioning --> red

so finally it should look somehow like this:

if choosebutton is pressed once in quick succession
--> activate the motion loop
--> let LED shine blue

if choosebutton is pressed twice in quick succession
--> activate the motion-sequence loop
--> let LED shine green
 
if choosebutton is pressed three times in quick succession
--> activate the positioning loop
--> let LED shine red

Is that possible? Can someone maybe please help me and provide me with the code or helpful URL :slight_smile:

This demo shows how to merge two programs. The general approach applies no matter how many you want to combine.

You need to watch out for the possibility that two of the original programs try to use the same resource - such as an I/O pin and if they do you will have to resolve that somehow.

...R

Pete19:

if choosebutton is pressed once in quick succession

if choosebutton is pressed twice in quick succession
if choosebutton is pressed three times in quick succession




Is that possible? Can someone maybe please help me and provide me with the code or helpful URL :-)

You would start by detecting a button press. You need a button press count. You need a timer to know when to stop accepting button presses.

if the button count is not 0 and the timer has been running more than "timeout" milliseconds:
set the mode based on the button count.
set the button count to 0

If the button has just been pressed:
increment the button count
start the timeout timer

Run a step of the loop based on the current mode.
NOTE: These 'loops' can't use delay() for timing. They have to check each time they are called to see where they are in the sequence and to check if it is time to do the next step.

The code in this Thread has a very nice way of dealing with multiple key-presses.

...R