Example Code
/*
Chirp Owl written by Becky Stern and T Main for Adafruit Industries
Tutorial: http://learn.adafruit.com/chirping-plush-owl-toy/
Modified by Michael Backus on 27 March 2017
for use in Teeland Middle School's Advanced
Robotics Club and Computer classes
*/
const int buttonPin = 12; // the number of the pushbutton pin
const int speakerPin = 14; // the number of the LED pin
const int ledPin = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin)) {
digitalWrite(ledPin, LOW);
chirp();
delay(500);
} else {
digitalWrite(ledPin, HIGH);
}
}
// Generate the Bird Chirp sound
void chirp() {
for (uint8_t i = 200; i > 180; i--)
playTone(i, 9);
}
// Play a tone for a specific duration. value is not frequency to save some
// cpu cycles in avoiding a divide.
void playTone(int16_t tonevalue, int duration) {
for (long i = 0; i < duration * 1000L; i += tonevalue * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tonevalue);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tonevalue);
}
}
|
Robotics >