Hello everyone,
I was wondering what was wrong with my program. Whenever I run it, it works fine but doesn't output sound. I am using a UDOO (modified Arduino Due) which means that I can't use the tone command. Here is the program:
int speakerPin = 9;
int length = 1; // the number of notes
char notes[] = "c"; // a space represents a rest
int beats[] = { 1 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(23, INPUT); //Make buttons here---|
pinMode(27, INPUT); // |
pinMode(31, INPUT); // |
pinMode(35, INPUT); // |
pinMode(39, INPUT); // |
pinMode(43, INPUT); // |
pinMode(47, INPUT); //--------------------|
}
void loop() {
if (23 == HIGH) {
playNote(notes['g'], 1);
}
if (27 == HIGH) {
playNote(notes['f'], 1);
}
if (31 == HIGH) {
playNote(notes['e'], 1);
}
if (35 == HIGH) {
playNote(notes['d'], 1);
}
if (39 == HIGH) {
playNote(notes['c'], 1);
}
if (43 == HIGH) {
playNote(notes['b'], 1);
}
if (47 == HIGH) {
playNote(notes['a'], 1);
}
}
If anyone could post help, that would be great!
Thanks, Kelley