Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Melody
- Plays a melody
- circuit:
- - 8 ohm speaker on digital pin 8
- created 21 Jan 2010
- modified 30 Aug 2011
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/Tone
- */
- #include "pitches.h"
- int melody[] = {
- NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4,
- NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4};
- // note durations: 4 = quarter note, 8 = eighth note, etc.:
- int noteDurations[] = {
- 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
- int buzzerPin = 9;
- void setup() {
- }
- void loop() {
- // iterate over the notes of the melody:
- for (int thisNote = 0; thisNote < 8; thisNote++) {
- int sensorReading = analogRead(A0);
- int thisPitch = map(sensorReading, 400, 1000, 1800, 100);
- // to calculate the note duration, take one second divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = thisPitch / noteDurations[thisNote];
- // to distinguish the notes, set a minimum time between them.
- // the note's duration + 30% seems to work well:
- int pauseBetweenNotes = noteDuration / 1.30;
- tone(9, melody[thisNote], noteDuration);
- delay(pauseBetweenNotes);
- Serial.println(sensorReading);
- Serial.println(thisPitch);
- if (pauseBetweenNotes > 100) {
- noTone(9, melody[thisNote], noteDuration);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement