Академический Документы
Профессиональный Документы
Культура Документы
http://www.hobbytronics.co.uk/arduino-tutorial7-piezo-beep
1 of 2
01-Oct-13 1:16 AM
http://www.hobbytronics.co.uk/arduino-tutorial7-piezo-beep
Connect the transducer to pin 9 and 0V on the Arduino In the Arduino Sketch shown below we have created a separate function beep() which sends the PWM signal to the transducer, waits for a small delay, then turns the transducer off, then has another small delay. Thus, it beeps once. The delay (in milliseconds) is passed as a parameter. The PWM pulse duration (20 in sketch below) should not be important as it is the frequency that matters; set it to somewhere in the middle of the PWM range. The sketch below beeps 3 times at startup and then beeps continuously at a slower rate. /* Piezo This example shows how to run a Piezo Buzzer on pin 9 using the analogWrite() function. It beeps 3 times fast at startup, waits a second then beeps continuously at a slower pace */ void setup() { // declare pin 9 to be an output: pinMode(9, OUTPUT); beep(50); beep(50); beep(50); delay(1000); } void loop() { beep(200); } void beep(unsigned char delayms){ analogWrite(9, 20); // Almost any // experiment delay(delayms); // wait for a analogWrite(9, 0); // 0 turns it delay(delayms); // wait for a }
value can be used except 0 and 255 to get the best tone delayms ms off delayms ms
Tutorial 8 - Nightlight
2 of 2
01-Oct-13 1:16 AM