Piano Program

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

    delayMicroseconds(tone);

What is tone? As far as I know, tone is a function pointer. I can't imagine that delayMicroseconds() expects a function pointer.

Saying I'am using a modified due is not help full What are the mods, why can't you use tone?

Mark

tone is here just a variable, but please note there is also a tone() function - tone() - Arduino Reference -

With request to your output question:
What kind of speaker do you use?
You cannot just connect a 8 ohm speaker to any uProc.!

(just a check of code)
an A = 440 Hz ==> so the (micro) delay should indeed be about 1136 microseconds.

if (23 == HIGH)

That doesn't seem very likely, does it?

To answer everyone's questions, the program I used is just the melody tutorial (http://www.arduino.cc/tutorial/melody) edited. The delaymicroseconds(tone) is just one of the things I just don't understand. The tone() function is a function that doesn't work with the arduino due. It just says error: tone is not defined My udoo doesn't have anything different with it involving the arduino due side of it also. The speaker I use is a Panasonic sb-pm19 speaker. I didn't say this earlier, but I am trying to make a piano with these buttons. When I press the buttons, they don't make any sound.

When I press the buttons, they don't make any sound.

Buttons generally don't.

Did you read my last reply?

So, what about the questions raised in the other replies?

What is tone that you are now using?
Where are you actually reading the pins that the switches are attached to?