Piezo & arduino

What I want to achieve: http://www.youtube.com/watch?v=UI2_ab68lTY

What I will use:

arduino uno R2
a red jumper wire
a black jumper wire
a piezo digisound B/C17
a breadboard
a led

How did I connect it all:

The red wire goes from arduino’s digital pin (7) to the plus side of the piezo
The black wire goes from arduino’s ground (gnd) to the minus side of the piezo
all connected through the breadboard
The led is connected to arduino’s digital pin (13) and ground conveniently next to eachother

The first test:

http://www.arduino.cc/en/Tutorial/PlayMelody


/* Play Melody
 * -----------
 *
 * Program to play melodies stored in an array, it requires to know
 * about timing issues and about how to play tones.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = 1/(2 * toneFrequency) = period / 2
 *
 * where the different tones are described as in the table:
 *
 * note 	frequency 	period 	PW (timeHigh)	
 * c 	        261 Hz 	        3830 	1915 	
 * d 	        294 Hz 	        3400 	1700 	
 * e 	        329 Hz 	        3038 	1519 	
 * f 	        349 Hz 	        2864 	1432 	
 * g 	        392 Hz 	        2550 	1275 	
 * a 	        440 Hz 	        2272 	1136 	
 * b 	        493 Hz 	        2028	1014	
 * C	        523 Hz	        1912 	956
 *
 * (cleft) 2005 D. Cuartielles for K3
 */

int ledPin = 13;
int speakerOut = 7;               
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

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

void loop() {
  analogWrite(speakerOut, 0);     
  for (count = 0; count < MAX_COUNT; count++) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          analogWrite(speakerOut,500);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
    }
  }
}

The second test:

http://www.faludi.com/2007/04/23/buzzer-arduino-example-code/ & http://www.faludi.com/itp/arduino/buzzer_example.pde


// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com

void setup() {
  pinMode(4, OUTPUT); // set a pin for buzzer output
}

void loop() {
  buzz(4, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
  delay(1000); // wait a bit between buzzes
}


void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to 
  //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}



My modified code is based on the code I found and used on the second test


/* Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
 by Rob Faludi
 http://www.faludi.com

edited to use morse code
by kontsu
https://kontsu.wordpress.com/
*/

int ledPin=13;
int speakerOut =7;
int beepFreq=2500;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(7, OUTPUT);
}

void shortMessage()
{
  digitalWrite(ledPin, HIGH);
  delay(0);//needed?!
  buzz(speakerOut, beepFreq, 150);
  delay(150);
  digitalWrite(ledPin, LOW);
  delay(100);
}

void longMessage()
{
  digitalWrite(ledPin, HIGH);
  delay(0);//needed?!
  buzz(speakerOut, beepFreq, 400);
  delay(400);
  digitalWrite(ledPin, LOW);
  delay(100);
}

void loop()
{
m();
o();
r();
s();
e();
delay(1000);
c();
o();
d();
e();
delay(1000);
v();
two();
delay(10000);
}

void buzz(int targetPin, long frequency, long length)
{
  long delayValue = 1000000/frequency/2;
  long numCycles = frequency * length/ 1000;
 
 for (long i=0; i < numCycles; i++)
 {
    digitalWrite(targetPin,HIGH);
    delayMicroseconds(delayValue);
    digitalWrite(targetPin,LOW);
    delayMicroseconds(delayValue);
  }
}

void a()
{
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void b()
{
  longMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void c()
{
  longMessage();
  shortMessage();
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void d()
{
  longMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void e()
{
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void f()
{
  shortMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void g()
{
  longMessage();
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void h()
{
  shortMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void i()
{
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void j()
{
  shortMessage();
  longMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void k()
{
  longMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void l()
{
  shortMessage();
  longMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000); 
}

void m()
{
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void n()
{
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000); 
}

void o()
{
  longMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void p()
{
  shortMessage();
  longMessage();
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void q()
{
  longMessage();
  longMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void r()
{
  shortMessage();
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void s()
{
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void t()
{
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void u()
{
  shortMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void v()
{
  shortMessage();
  shortMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void w()
{
  shortMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void x()
{
  longMessage();
  shortMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void y()
{
  longMessage();
  shortMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void z()
{
  longMessage();
  longMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void zero()
{
  longMessage();
  longMessage();
  longMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void one()
{
  shortMessage();
  longMessage();
  longMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void two()
{
  shortMessage();
  shortMessage();
  longMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void three()
{
  shortMessage();
  shortMessage();
  shortMessage();
  longMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void four()
{
  shortMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  longMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void five()
{
  shortMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void six()
{
  longMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void seven()
{
  longMessage();
  longMessage();
  shortMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void eight()
{
  longMessage();
  longMessage();
  longMessage();
  shortMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

void nine()
{
  longMessage();
  longMessage();
  longMessage();
  longMessage();
  shortMessage();
  digitalWrite(13, LOW);
  delay(1000);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s