Go Down

Topic: Pinging (literally) (Read 1 time) previous topic - next topic

amacmullen14

Yesterday, I made a cool thing that could be the beginning of a synth.  It works with one piezo (or speaker thru a transistor) and doesn't just beep, but makes a pinging noise, sort of like a plucking noise.  I'll post the code soon.  (I'm typing on my iPod right now).

amacmullen14

Here's the code:
Code: [Select]
/*
const int CC = 956;
const int BB = 1014;
const int AA = 1136;
const int GG = 1275;
const int FF = 1432;
const int EE = 1519;
const int DD = 1700;
const int cc = 1915;
*/
int tones[] = {
 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 
int tones2[]  = {
 956, 1014, 1136, 1275, 1432, 1519, 1700, 1915 };

float vols[] = {
 50,100,
 115,107,100,87,75,67,60,
 57,55,53,50,47,45,43,40,
 37,35,33,30,27,25,25,23,23,20,20,
 17,17,15,15,13,13,10,10,5,5
};

int speakerPin = 9;

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

void loop()  {
 for(int x = 0; x < 8; x++)  {
   for(int y = 0; y < 36; y++)  {
     playTone(tones[x], vols[y] / 10);
   }
   digitalWrite(9, LOW);
 }
 for(int x = 0; x < 8; x++)  {
   for(int y = 0; y < 36; y++)  {
     playTone(tones2[x], vols[y] / 10);
   }
   digitalWrite(9, LOW);
 }
}


void playTone(int tone, int volume) {
 for (long i = 0; i < 10000; i += tone * 2) {
   int num = map(volume, 0, 255, 0, tone);
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(num);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone - num);

 }
}


Guci22

That is really cool! Just tried it and it is pretty wild. It is a lot more pleasant than a normal piezo buzz.

amacmullen14

#3
Aug 23, 2009, 11:34 pm Last Edit: Aug 23, 2009, 11:35 pm by amacmullen14 Reason: 1
Thanks!

I'm ordering an ultrasonic range sensor, and I hope to make a theremin-like-thing!
(Ping the sensor to make a ping!)

By the way, did you use a speaker?

Currently, I have a speaker wired like this:

Digital pin 9 to base of transistor
Collector to 5v
Emitter to speaker
Speaker to ground

I also have a diode parallel to the speaker, with the anode to the base of the transistor, for extra safety.  Is this necessary?
I put it there since the speaker is essentially a coil of wire.

dh1ao

#4
Aug 24, 2009, 12:00 am Last Edit: Aug 24, 2009, 12:01 am by dh1ao Reason: 1
is it something like this?



The diode makes sense, sure. But I would be concerned about the power dissipation of the transistor if you switch it on for more than a millisecond or so. After the first change in potential the speaker acts like a very low ohm wire rising the current through transistor and speaker. You will see which one will give signs of smoke first  :o

amacmullen14

Oh.  But in this application of the speaker, could anything bad happen?

dh1ao

Hi again

you wrote

Quote
I also have a diode parallel to the speaker


That is what I tried to sketch

and than you wrote
Quote
with the anode to the base of the transistor

??? Than the diode is not in parallel to the speaker.

You should limit the current through the collector with a resistor with at least 100 ohms 250 mW

amacmullen14

#7
Aug 24, 2009, 02:37 am Last Edit: Aug 24, 2009, 03:58 am by amacmullen14 Reason: 1
Oh! Sorry.  I meant the emitter

And I didn't really understand what you said.  I'm no electronics expert.  If this circuit is bad, can you show me a better one?

dh1ao

#8
Aug 24, 2009, 07:24 pm Last Edit: Aug 24, 2009, 07:24 pm by dh1ao Reason: 1
Hi sciguy

just a rough sketch



I guess you want some sound out of the speaker so DC current isn't necessary? Therefore the capacitor. If the sound is to low you could resize R2 to 220 ohms or 100 ohms. Sure there much better circuits, my one maybe used as a guide  :o

Cheers
Peter

amacmullen14

#9
Aug 24, 2009, 11:56 pm Last Edit: Aug 24, 2009, 11:59 pm by amacmullen14 Reason: 1
OK, thanks.
Couple questions:

Is the thing under the 5v is the output pin?
Do I need to use an external battery?  (it is a small speaker, maybe 2" diameter)

dh1ao

Hi sciguy

so sorry, ehm as you see (and surely you guessed it by my bad english) I'm from europe ;D
The symbol you asked for here is a symbol for common ground.

I used an external power supply for the sketch. I'm pretty sure you can use the Arduino 5V. The current drawn from it will be far below 500 mA.
Not sure how USB supply acts to sudden impulses when the capacitor is charged/decharged. Worth a try, your PC will take no harm.

As mentioned there's surely much better circuitry...

cu
Peter

Go Up