trouble working a PWM tone generator


i'm trying write tone generator led lighting using pwm pins first arduino project.  i've got pair of 8 1 muxes working , lights match tones fine, tone speeds playtone function keep coming out short.  i've been trying debug couple of hours , feel retarded can't working yet.  anyone able spot error?  i'm guessing it's within playtone function itself, i'm not positive.


int s1[] = {7, 8, 9};      //select lines first mux, tone light selector
int s2[] = {4, 5, 6};      //select lines second mux, rhythm light selector
int speakerout1 = 11;   //sets first audio channel
int lightout = 3;            //sets led channel

//int pulsewidth[] = {7644, 7215, 6810, 6428, 6067, 5726, 5405, 5102, 4815, 4545, 4290, 4049, 3822, 3607, 3405, 3214, 3033, 2863, 2702, 2551, 2407, 2272, 2145, 2024, 1911, 1803, 1702, 1607, 1516, 1431, 1351, 1275};
int pulsewidth[] = {3822, 3405, 3033, 2863, 2551, 2272, 2024, 1911};
int bincodes[] = {000, 1, 10, 11, 100, 101, 110, 111};


int tempo = 100;                //tempo in bpm
long beat = (60 * 1000 * 1000)/tempo;   //the length of 1 beat, in microseconds

void setup ()
{
 for (int = 0; < 3; i++)
 {
   pinmode(s1, output);
   pinmode(s2, output);
 }
 pinmode(speakerout1, output);
 pinmode(lightout, output);
}
void lightsoff()
{
 digitalwrite(lightout, low);
}

void playtone(int x, int t)           //when passed int, plays tone corresponding number.  tone values stored
{                                     //in pulsewidth[] array pulse widths, not frequencies!  7644 = c3
long elapsed_time = 0;
long duration = beat / t;    //duration of desired note in microseconds
if (x >= 0)
 {
    while (elapsed_time < duration)
    {
     digitalwrite(speakerout1, high);
     delaymicroseconds(pulsewidth
  • /2);
         digitalwrite(speakerout1, low);
         delaymicroseconds(pulsewidth
  • /2);
         elapsed_time += (pulsewidth
  • );
        }
     }
    }

    void toneoff()                //silences tone channel
    {
     digitalwrite(speakerout1, low);  
    }

    void light(int x)        //when passed decimal value, lights appropriate led
    {
     lightsoff();
     int y = bincodes
  • ;
     int a0 = y & 0x01;
     int a1 = (y >> 1) & 0x01;
     int a2 = (y >> 2) & 0x01;
     digitalwrite(s1[0], a0);
     digitalwrite(s2[0], a0);
     digitalwrite(s1[1], a1);
     digitalwrite(s2[1], a1);
     digitalwrite(s1[2], a2);
     digitalwrite(s2[2], a2);
     digitalwrite(lightout, high);
    }

    void loop()
    {
     toneoff();
     lightsoff();
     for (int = 0; <= 7; i++)
     {
       light(i);
       playtone(i, 4);  
       lightsoff();
     }
     delay (1000);
    }

oh , meant include playtone variables passed should in form (int x, int t) representing pitch , duration of notes.  i wanted when t = 1 note whole note, t = 4 means note quarter note, etc.  pitch 0 c3 (at least think).  i'm going major scale tones right since having things in 8's easy work (especially when have 8 1 muxes).


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > trouble working a PWM tone generator


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums