Hardware Reference
In-Depth Information
The loop() reads the sensor, and
when it crosses a threshold, it triggers
a method called playStinger() that
plays a sequence of notes.
8
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
Serial.println(sensorReading);
// if the sensor's higher than the threshold and
// was lower than the threshold last time,
// then play the stinger:
if (sensorReading <= threshold
&& lastReading > threshold ) {
playStinger();
}
// save the current reading for next time:
lastReading = sensorReading;
}
8
playStinger() plays a sequence
of notes with a sequence of pauses in
between. To do this, it calls notes from
an array using noteOn() and noteOff() ,
and then pauses, according to the rest
times given by an array.
void playStinger() {
int note[] = {43, 41, 49};
int rest[] = {70, 180, 750};
// loop over the three notes:
for (int thisNote = 0; thisNote < 3; thisNote++) {
// Turn on note:
noteOn(9, note[thisNote], 60);
delay(rest[thisNote]);
//Turn off the note:
noteOff(9, note[thisNote], 60);
// a little pause after the second note:
if (thisNote == 1) {
delay(50);
}
}
}
//NOTE: the noteOn(), noteOff() and sendMidi() methods from the previous
//example go here.
noteon() , noteOff() , and sendMidi() are
the same as in the previous example,
and you can copy them from there.
8
resetMidi() takes the reset from
the previous example out of setup()
and puts it in its own method.
void resetMidi(int thisPin) {
// Reset the Musical Instrument Shield:
pinMode(thisPin, OUTPUT);
digitalWrite(thisPin, LOW);
delay(100);
digitalWrite(thisPin, HIGH);
delay(100);
}
When you've uploaded this sketch, say
the following:
"A guy walks into a bar and says
'OUCH!'"
Then wave your hand over the IR
ranger. You're a comedian! And you've
made your first MIDI instrument!
 
Search WWH ::




Custom Search