Skip to content

Instantly share code, notes, and snippets.

@nicoknoll
Created January 4, 2015 14:43
Show Gist options
  • Save nicoknoll/371ff0383bf98fd21c49 to your computer and use it in GitHub Desktop.
Save nicoknoll/371ff0383bf98fd21c49 to your computer and use it in GitHub Desktop.
#include <math.h>
#include "oh_tannenbaum.h"
/* This melody is defined in the header above. */
int const melody[] = MELODY;
/********************************************************/
/* 3.) Define a constant LOUDSPEAKER_PIN containing the
number of the digital pin you just connected the
"+" pin of your loudspeaker to.
*/
int const LOUDSPEAKER_PIN = 8;
/********************************************************/
/********************************************************/
/* 7.) Define a new function waitForTap,
that does nothing until the analog port A0 has
some value greater than 10.
Call that function just before you call play(int, long).
*/
void waitForTap() {
if(analogRead(0) > 10) {
// Do something
}
}
/********************************************************/
void play(int note, long duration) {
pinMode(LOUDSPEAKER_PIN, OUTPUT);
/********************************************************/
/* The variable note gives you a frequency in Hz.
The variable duration is given in milliseconds.
5.) Generate a rectangular wave with the given
frequency and duration.
To do so, change the value at the loudspeaker pin
to HIGH and to LOW for a sufficient number of
times and wait an appropriate time after you set
each value.
Hint: Think about the resolutions you need!
You may want to use more percise functions than the
ones from the LEDFlash example
*/
/*
1.000.000mikrosekunden = 1sec
Hz = Schwingung pro Sekunde
*/
int i;
for(i = 0; i < duration; i++) {
digitalWrite(LOUDSPEAKER_PIN, HIGH);
delayMicroseconds(1000000/note);
digitalWrite(LOUDSPEAKER_PIN, LOW);
delayMicroseconds(1000000/note);
}
/********************************************************/
pinMode(LOUDSPEAKER_PIN, INPUT);
}
/* the setup routine runs once when you press reset: */
void setup() {
pinMode(LOUDSPEAKER_PIN, INPUT);
}
/* the loop routine runs over and over again forever: */
void loop() {
/********************************************************/
/* 4.) Look at the included melody.
How can you determine how long it is?
Call play(int, long) with each note in the melody.
Play each note for 125ms.
*/
int arraySize, i;
arraySize = sizeof(melody)/sizeof(int);
for(i = 0; i < arraySize; i++) {
waitForTap();
play(melody[i], 125);
delay(20);
}
/********************************************************/
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment