Hello to all.
I'm using an SRF02 to begin a musical sequence that is sent to another arduino. My issue currently is that when the sensor picks up an object between the two set distances, it plays the entire sequence 3 times before stopping. Why is it playing through specifically 3 times and not just once?
What I would like is that the sequence plays through only once before stopping. Any help would be appreciated ![]()
#include "Wire.h"
#include "SRF02.h"
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
SRF02 sensor(0x70, SRF02_CENTIMETERS);
// Two things need to be created: the array for the notes of the melody (in order)
// and the duration of each (think of it like sheet music in two parts)
// BOTH ARRAYS MUST BE THE SAME SIZE!
// The melody array
int melody[] = {
//intro glockenspiel
91, 94, 91, 94, 72, 75, 72, 75,
91, 94, 91, 94, 72, 75, 72, 75,
91, 94, 91, 94, 72, 75, 72, 75,
91, 94, 91, 94, 72, 75, 72, 75,
// drum saw canjo
60, 65, 62, 60, 94, 62, 64, 69, //60, 64, 62, 60, 64, 62, 64, 69,
60, 91, 62, 64, 60, 89, 62, 64, //60, 64, 62, 64, 60, 64, 62, 64,
60, 87, 62, 60, 64, 84, 64, 69, //60, 64, 62, 60, 64, 62, 64, 69,
60, 64, 62, 82, 60, 64, 79, 64, //60, 64, 62, 64, 60, 64, 62, 64,
// glockenshpiel
94, 91, 89, 91, 94, 91, 89, 87, //94, 91, 89, 91, 94, 91, 89, 87,
94, 91, 89, 91, 94, 91, 89, 87, //94, 91, 89, 91, 94, 91, 89, 87,
84, 87, 89, 91, 84, 87, 89, 91, //84, 87, 89, 91, 84, 87, 89, 91,
84, 87, 89, 91, 94, 91, 89, 87, //84, 87, 89, 91, 94, 91, 89, 87,
84, 62, 64, 82, 79, 82, 84, 60, //84, 87, 84, 82, 79, 82, 84, 87,
84, 62, 64, 82, 79, 82, 79, 77, //84, 87, 84, 82, 79, 82, 79, 77,
84, 62, 64, 82, 79, 82, 79, 77, //84, 87, 84, 82, 79, 82, 79, 77,
60, 72, 75, 77, 79, 82, 84, 87, //75, 72, 75, 94, 91, 89, 87, 84,
72, 75, 77, 79, 82, 84, 87, 89, //72, 75, 77, 79, 82, 84, 87, 89,
91, 94, 91, 89, 87, 84, 82, 79, //91, 94, 91, 89, 87, 84, 82, 79,
77, 75, 72, 75, 77, 79, 82, 84, //77, 75, 72, 75, 77, 79, 82, 84,
87, 60, 91, 94, 60, 94, 91, 60, //87, 89, 91, 94, 91, 94, 91, 94,
91, 60, 91, 94, 60, 94, 91, 60, //91, 94, 91, 94, 91, 94, 91, 94,
91, 94, 91, 94, 91, 94, 91, 94
};
// The note duration, 8 = 8th note, 4 = quarter note, etc.
int durations[] = {
// intro glockenspiel
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
// drum saw canjo
16, 16, 16, 16, 16, 16, 16, 16,
8, 8, 8, 16, 16, 16, 16, 8,
16, 16, 8, 8, 16, 16, 8, 8,
16, 16, 16, 16, 16, 16, 16, 16,
// glockenspiel
8, 8, 8, 8, 8, 8, 8, 8,
8, 5, 16, 8, 8, 8, 8, 8,
8, 5, 16, 8, 8, 8, 8, 8,
8, 5, 16, 8, 8, 8, 8, 8,
8, 5, 16, 8, 16, 16, 16, 16,
8, 5, 16, 8, 16, 16, 16, 16,
8, 5, 16, 8, 16, 16, 16, 16,
8, 8, 8, 8, 8, 8, 8, 8,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16,
32, 32, 32, 32, 32, 32, 32, 32
};
// determine the length of the arrays to use in the loop iteration
int songLength = sizeof(melody) / sizeof(melody[0]);
void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
Wire.begin();
}
void loop() {
int sensorReading = (sensor.read()); //readData(sensor.read());
SRF02::update();
if ((sensorReading > 25) && (sensorReading < 100))
{
// Iterate through both arrays
// Notice how the iteration variable thisNote is created in the parenthesis
// The for loop stops when it is equal to the size of the melody array
for (int thisNote = 0; thisNote < songLength; thisNote++) {
// determine the duration of the notes that the computer understands
// divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
int duration = 1000 / durations[thisNote];
MIDI.sendNoteOn(melody[thisNote], 127, 1);
// pause between notes
int pause = duration * 4; //1.8
delay(pause);
// stop the note
MIDI.sendNoteOff(melody[thisNote], 0, 1);
}
}
}