3

Hi I am trying to using different sound frequencies on an mbed application shield to create a tone from a song. Although I have gotten all the frequencies I cannot seem to slow the tempo down, as it just cycles through all of the frequencies really fast. I have tried to use the wait(); function but that does not seem to be working correctly. I would appreciate some ideas on how to correct this or alternative solutions.

Here is my code

#include "mbed.h"
#include "C12832.h"   // for the LCD
#include "LM75B.h"   //for the temperature sensor
#include "MMA7660.h" //For the accelerometer

/***************************************************************************
Global Variables
***************************************************************************/

C12832 shld_lcd (D11, D13, D12, D7, D10);   // LCD on the application shield
PwmOut spkr(D6); //speaker

float c = 262.0; //These are the different frequencies
float d = 294.0;
float e = 330.0;
float f = 349.0;
float g = 392.0;
float a = 440.0;
float b = 494.0;
float C = 523.0;


void hotLineBling() {

    //Here I am trying to use the frequencies to play the tone

    spkr.period(1/e);
    spkr.period(1/e);
    spkr.period(1/e);
    wait(0.5f);
    spkr.period(1/C);
    spkr.period(1/a);
    spkr.period(1/e);
    wait(0.5f);
    spkr.period(1/d);
    spkr.period(1/a);
    spkr.period(1/d);
    wait(1.0);
    spkr.period(1/C);
    spkr.period(1/a);
    spkr.period(1/e);
    wait(0.5f);
    spkr.period(1/d);
    spkr.period(1/a);
    spkr.period(1/d);
    spkr.period(1/c);
    wait(1.0);
    spkr.period(1/C);
    spkr.period(1/a);
    spkr.period(1/e);
    wait(0.5f);
    spkr.period(1/d);
    spkr.period(1/a);
    spkr.period(1/d);
    wait(5.0);

}

int main()
{

    hotLineBling();

    while (1) {

        spkr = 0.5;
        wait(0.2f); //wait a little
    }
}
user5647516
  • 1,083
  • 1
  • 9
  • 19

2 Answers2

2

You need to set the value of the PWM pin to something to play, can also control volume that way. Second thing is that you need to wait between setting the period otherwise there's no time for the note to actual do anything on that frequency. Try this:

#include "mbed.h"

PwmOut spkr(D3);

float c = 262.0; //These are the different frequencies
float d = 294.0;
float e = 330.0;
float f = 349.0;
float g = 392.0;
float a = 440.0;
float b = 494.0;
float C = 523.0;
float _ = 0.0;

void hotLineBling() {
    float notes[] = { e, e, e, _, C, a, e, _, d, a, d, _, _,
                      C, a, e, _, d, a, d, c, _, _, C, a, e,
                      _, d, a, d };

    for (int i = 0; i < sizeof(notes) / sizeof(float); i++) {
        if (notes[i] == _) {
            spkr = 0.0f;
        }
        else {
            spkr = 0.3f;
            spkr.period(1 / notes[i]);
        }
        wait(0.5f);
    }

    spkr = 0.0f;
}

int main() {
    hotLineBling();
}

Although I don't know if this sounds anything like you want it to sound :-)

Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
0

I'm neither very familiar with the mbed nor with the environment you are using - But some things seem obvious:

  • You are talking to a PWM generator and setting up frequencies
  • You seem to be waiting (pausing) between groups of notes - At least you want to. You're not telling the PWM to be silent (frequency = 0) for those periods.
  • You also seem to have set the PWM duty cycle (I assume the "spkr = 0.5" line does that)

All O.K, but: - You do not seem to be waiting for the PWM to actually play the note depending on how long you want the tone to be present (there should be a delay after the PWM frequency is set for each note - That is like placing your fingers on the holes in a tin whistle and not blowing...)

You should definitely spread some wait()s between the spkr.period () lines depending on how long you want each note to play.

You should also set the PWM frequency to 0 if you want a period of silence.

What I don't know is whether the mbed requires you to actually "start" the PWM or if it is free-running.

tofro
  • 5,640
  • 14
  • 31