# Two for() loops at the same time

I’m modifying a new project, used for playing a melody, and right now I’m adding a base line. I must use two for() loops at the same time, and here’s where I’m stuck:

#include “pitches.h”
int harmony = {C3, E3, G3, D3, F3, F#3, G, B2, D3, F#3, C3, E3, G3, C3, C3, E3, G3, A#2, E3, G3, G#2, C3, E3, F2, A2, D3, G2, C3, E3, G2, C3, E3, G2, B2, F3, E3};
int melody = {G3, G3, A3, G3, C4, B3, G3, G3, A4, G4, D4, C4, G3, G3, G4, E4, C4, B3, A3, F4, F4, E4, C4, D4, C4};
int noteDurations = {4/0.75, 16, 4, 4, 4, 2, 4/0.75, 16, 4, 4, 4, 2, 4/0.75, 16, 4, 4, 4, 4, 4, 4/0.75, 16, 4, 4, 4, 2};
void setup() {
for(int thisNote = 0; thisNote < 25; thisNote++){
int noteDuration = 1500 / noteDurations[thisNote];
tone(12, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(12);
}
}
void loop(){
}

I’m pretty sure I haven’t used harmony.
And here’s pitches.h:

#define A0 27.5
#define A#0 29.1
#define B0 30.8
#define C1 32.7
#define C#1 34.6
#define D1 36.7
#define D#1 38.8
#define E1 41.2
#define F1 43.6
#define F#1 46.2
#define G1 49
#define G#1 51.9
#define A1 55
#define A#1 58.3
#define B1 61.7
#define C2 65.4
#define C#2 69.2
#define D2 73.4
#define D#2 77.8
#define E2 82.4
#define F2 87.3
#define F#2 92.5
#define G2 98
#define G#2 103.8
#define A2 110
#define A#2 116.5
#define B2 123.5
#define C3 130.8
#define C#3 138.6
#define D3 146.8
#define D#3 155.6
#define E3 164.8
#define F3 174.6
#define F#3 185
#define G3 196
#define G#3 207.7
#define A3 220
#define A#3 233.1
#define B3 246.9
#define C4 261.6
#define C#4 277.2
#define D4 293.7
#define D#4 311.1
#define E4 329.6
#define F4 349.2
#define F#4 370
#define G4 392
#define G#4 415.3
#define A4 440
#define A#4 466.2
#define B4 493.9
#define C5 523.3
#define C#5 554.4
#define D5 587.3
#define D#5 622.3
#define E5 659.3
#define F5 698.5
#define F#5 740
#define G5 784
#define G#5 830.6
#define A5 880
#define A#5 932.3
#define B5 987.8
#define C6 1046.5
#define C#6 1108.7
#define D6 1174.7
#define D#6 1244.5
#define E6 1318.5
#define F6 1396.9
#define F#6 1480
#define G6 1568
#define G#6 1661.2
#define A6 1760
#define A#6 1864.7
#define B6 1975.5
#define C7 2093
#define C#7 2217.5
#define D7 2349.3
#define D#7 2489
#define E7 2637
#define F7 2793.8
#define F#7 2960
#define G7 3136
#define G#7 3322.4
#define A7 3520
#define A#7 3729.3
#define B7 3951.1
#define C8 4186

I hope you’re able to fix my issue.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
`[code]``[color=blue]// your code is here[/color]``[/code]`
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Where did you find this pitches.h? You can’t use the hash symbol in a macro name.

Don't use delay, you're going to have to use millis(), and compare the current millis() time to the last time you changed notes to know whether it is time to change notes yet.

A simple example is this (I just copy pasted blink without delay, its example 1 under the second set of examples which I believe is just titled "Digital," should have come with the Arduino IDE). I've also left off the declarations and the set up since they aren't really needed to show what is going on, just assume that variables exist.

``````void loop() {
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
``````

Specifically, look at what is going on in the if statement. Also, if you replace the comment about blinking the LED with changing notes, it should pretty much be at where I think you are trying to go.

Do you mean nested for loops?

In the sketch, I found pitches.h by adding a new tab and entering the code that defines the names of the keys on the keyboard and converting them into Hertz. (Formula: 440(a^n) where a = the twelfth root of 2, and n is the number of half steps above A4)

You can run for loops simultaneously only so much in that they are nested:

``````fior (int i = 1; i <= 10; i++)
{
for (char j = 'A'; j <= 'Z'; j++)
{
Serial.print(j);
Serial.println(i);
}
}
``````

The output will be

A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
.
.
.
.
.
.

boylesg:
You can run for loops simultaneously only so much in that they are nested:

Yeah, I'm not sure why I thought it would be possible to jump out of each loop.

Could do two arrays with the notes in order and increment through them with counters that only increment inside an if statement at the appropriate delay. You could get really fancy, and use 4 arrays, two pairs of notes and delays.

I think what you are trying to do is play complex music?

But it is only a 16MHz processor and only really good for simple tones and melodies (using sequential tones).

There are ways of getting round the thing you're worrying about but you also need to consider that only one note at any time can be playing use the tone() command. See tone() - Arduino Reference

So harmonies will not work without finding some other mechanism to play the notes.

Steve

pon00009:
WHAT IS A MACRO NAME????????????????????????????!?!?!?!?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!???!?

What brings on this outburst?

Your efforts are doomed anyway because the tone function can only play one note at a time, so you can’t add a base line to that tune using the tone function.