Go Down

Topic: Help needed: Making Piezo speaker whistle (Read 2337 times) previous topic - next topic

teelah17

Hey guys, I'm having a bit of trouble.  I'm attempting to make the Piezo speaker "whistle" (perhaps like whistling at a girl).

I've got it whistling up (the 1st part of the whistle), but I'm unsure how to make it stop and begin the whistling down (the 2nd part of the whistle).

Any help or guidance would be appreciated.

Here's my coding so far:

void setup () {
pinMode(12,OUTPUT);
}

void loop () {
int i;
int x;
 
  for ( i=500; i>100; i--)
  {
   for  ( x=0; x<5;  x++)
   {
   digitalWrite (12,HIGH);
   delayMicroseconds (i);
   digitalWrite (12,LOW);
   delayMicroseconds (i);
   }
}

for (i=1; i<700; i++)
{
   digitalWrite (12,HIGH);
   delayMicroseconds(i);
   digitalWrite(12,LOW);
   delayMicroseconds(i);
   delay(200);
  }
}

Grumpy_Mike

When posting code please select it and hit the # icon to get it formatted correctly.

Try this, you need a delay between each section, it might be best to start the down bit at the same frequency as the up bit finished.

Code: [Select]
void setup () {
pinMode(12,OUTPUT);
}

void loop () {
int i;
int x;

  for ( i=500; i>100; i--)
  {
   for  ( x=0; x<5;  x++)
   {
   digitalWrite (12,HIGH);
   delayMicroseconds (i);
   digitalWrite (12,LOW);
   delayMicroseconds (i);
   }
}
    delay(200);
for (i=100; i<700; i++)
{
   digitalWrite (12,HIGH);
   delayMicroseconds(i);
   digitalWrite(12,LOW);
   delayMicroseconds(i);
  }
     delay(500);
}



Hi teelah 17:

This is my best shoot with the whistle. Actually i'm working in a whistling mirror for a girl. i want to plug it in a girl's toilet and when they goes to see herself in front of the mirror for a while. the mirror whistle for her.
Code: [Select]


int buzz = 12;
int PIR = 2;
int senal = 0;
int tiempo;

void silba()
{
int i;
int x;

  for ( i=200; i>100; i--)
  {
   for  ( x=0; x<5;  x++)
   {
   digitalWrite (12,HIGH);
   delayMicroseconds (i);
   digitalWrite (12,LOW);
   delayMicroseconds (i);
   }
}
    delay(500);

  for ( i=300; i>100; i--)
  {
   for  ( x=0; x<4;  x++)
   {
   digitalWrite (12,HIGH);
   delayMicroseconds (i);
   digitalWrite (12,LOW);
   delayMicroseconds (i);
   }
}   


for (i=100; i<300; i++)
{
   digitalWrite (12,HIGH);
   delayMicroseconds(i);
   digitalWrite(12,LOW);
   delayMicroseconds(i);
  }
 
}

void setup ()
{
Serial.begin(9600);
pinMode(12,OUTPUT);
}

void loop ()
{
  senal= analogRead(PIR);

  if(senal>500)
  {
    tiempo ++;
    Serial.println("Presencia detectada en:");
    Serial.println(tiempo);
  }
 
 
  if( tiempo > 11)
  {
    silba();
   
   
    for(int x = 0 ; x < 1;x++)
    {
     delay(12000);
    }
   
    tiempo =0;
  }
  delay(1000);
 
}


Go Up