How do I have 2 loops on an Arduino UNO?

I have two programs that are both loops. One program sweeps a micro servo back and forth. The other plays tones on a buzzer. Is there a way to combine both programs into one loop that plays the buzzer and moves the servo simultaneously? I have attached the code below.

servo code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 45; pos <= 135; pos += 5) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 135; pos >= 45; pos -= 5) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

Buzzer code:

// Set this to be the pin that your buzzer resides in. (Note that you can only have one buzzer actively using the PWM signal at a time).
int tonePin = 6;
void setup() {

}

void midi() {

    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);
    delay(538.792708333);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(538.792708333);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(538.792708333);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(323.275625);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 146, 106.68095625);
    delay(107.758541667);
    delay(538.792708333);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 220, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 164, 106.68095625);
    delay(107.758541667);
    delay(107.758541667);
    tone(tonePin, 130, 106.68095625);
    delay(107.758541667);

}

void loop() {
    // Play midi
    midi();
}

Yes.

All I can do right now is point you to

and also ask you to poke around what turns up with these google terms

 arduino two things at once

and

 arduino combine sketches

which should keep you busy until everyone else wakes up.

It will require a fair amount of work. You will learn things that will be valuable for any more programming of these tiny machines that you may do.

a7

2 Likes

Not answering the questoon but delay() works with whole numbers; anything after the decimal dot will be thrown away.

1 Like

Thanks so much! I’ll look into these!

Ooh good to know, I actually just used a midi to arduino converter to get this code.

Yes... but not until you get rid of those delay statements. You need a different approach for timing - there is a fairly standard technique that uses millis() instead. At first it probably looks quite complicated... but once you get your head around you will be able to develop much more flexible programs.

Suggest Googling some of the terms suggested by @alto777

2 Likes

"Read @alto777's link, you should."

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.