/ Chirping_Plush_Owl_Toy / Chirping_Plush_Owl_Toy.ino
Chirping_Plush_Owl_Toy.ino
1 // SPDX-FileCopyrightText: 2018 Becky Stern for Adafruit Industries 2 // SPDX-FileCopyrightText: 2018 T Main for Adafruit Industries 3 // 4 // SPDX-License-Identifier: MIT 5 6 /* 7 Chirp Owl written by Becky Stern and T Main for Adafruit Industries 8 Tutorial: http://learn.adafruit.com/chirping-plush-owl-toy/ 9 10 Includes animal sounds by Anne Barela 11 http://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds 12 13 based in part on Debounce 14 created 21 November 2006 15 by David A. Mellis 16 modified 30 Aug 2011 17 by Limor Fried 18 modified 28 Dec 2012 19 by Mike Walters 20 21 This example code is in the public domain. 22 23 http://www.arduino.cc/en/Tutorial/Debounce 24 */ 25 26 // constants won't change. They're used here to 27 // set pin numbers: 28 const int buttonPin = 0; // the number of the pushbutton pin 29 const int speakerPin = 2; // the number of the LED pin 30 const int ledPin = 1; 31 32 // Variables will change: 33 int ledState = HIGH; // the current state of the output pin 34 int buttonState; // the current reading from the input pin 35 int lastButtonState = LOW; // the previous reading from the input pin 36 37 // the following variables are long's because the time, measured in miliseconds, 38 // will quickly become a bigger number than can be stored in an int. 39 long lastDebounceTime = 0; // the last time the output pin was toggled 40 long debounceDelay = 50; // the debounce time; increase if the output flickers 41 42 void setup() { 43 pinMode(buttonPin, INPUT_PULLUP); 44 pinMode(speakerPin, OUTPUT); 45 //digitalWrite(speakerPin, HIGH); 46 digitalWrite(ledPin, LOW); 47 //digitalWrite(buttonPin, HIGH); 48 // set initial LED state 49 //digitalWrite(speakerPin, ledState); 50 //Serial.begin(9600); 51 } 52 53 void loop() { 54 // read the state of the switch into a local variable: 55 int reading = digitalRead(buttonPin); 56 57 // check to see if you just pressed the button 58 // (i.e. the input went from LOW to HIGH), and you've waited 59 // long enough since the last press to ignore any noise: 60 61 // If the switch changed, due to noise or pressing: 62 if (reading != lastButtonState) { 63 // reset the debouncing timer 64 lastDebounceTime = millis(); 65 } 66 67 if ((millis() - lastDebounceTime) > debounceDelay) { 68 // whatever the reading is at, it's been there for longer 69 // than the debounce delay, so take it as the actual current state: 70 71 // if the button state has changed: 72 if (reading != buttonState) { 73 buttonState = reading; 74 75 // only toggle the LED if the new button state is HIGH 76 //Serial.println("chirp"); 77 chirp(); // change this line to change animal sound 78 //meow(); 79 //meow2(); 80 ////mew(); 81 //ruff(); 82 //arf(); 83 } 84 } 85 86 // set the LED: 87 //digitalWrite(speakerPin, ledState); 88 89 // save the reading. Next time through the loop, 90 // it'll be the lastButtonState: 91 lastButtonState = reading; 92 } 93 94 95 // Generate the Bird Chirp sound 96 void chirp() { 97 for(uint8_t i=200; i>180; i--) 98 playTone(i,9); 99 } 100 101 // Play a tone for a specific duration. value is not frequency to save some 102 // cpu cycles in avoiding a divide. 103 void playTone(int16_t tonevalue, int duration) { 104 for (long i = 0; i < duration * 1000L; i += tonevalue * 2) { 105 digitalWrite(speakerPin, HIGH); 106 delayMicroseconds(tonevalue); 107 digitalWrite(speakerPin, LOW); 108 delayMicroseconds(tonevalue); 109 } 110 } 111 112 void meow() { // cat meow (emphasis ow "me") 113 uint16_t i; 114 playTone(5100,50); // "m" (short) 115 playTone(394,180); // "eee" (long) 116 for(i=990; i<1022; i+=2) // vary "ooo" down 117 playTone(i,8); 118 playTone(5100,40); // "w" (short) 119 } 120 121 void meow2() { // cat meow (emphasis on "ow") 122 uint16_t i; 123 playTone(5100,55); // "m" (short) 124 playTone(394,170); // "eee" (long) 125 delay(30); // wait a tiny bit 126 for(i=330; i<360; i+=2) // vary "ooo" down 127 playTone(i,10); 128 playTone(5100,40); // "w" (short) 129 } 130 131 void mew() { // cat mew 132 uint16_t i; 133 playTone(5100,55); // "m" (short) 134 playTone(394,130); // "eee" (long) 135 playTone(384,35); // "eee" (up a tiny bit on end) 136 playTone(5100,40); // "w" (short) 137 } 138 139 void ruff() { // dog ruff 140 uint16_t i; 141 for(i=890; i<910; i+=2) // "rrr" (vary down) 142 playTone(i,3); 143 playTone(1664,150); // "uuu" (hard to do) 144 playTone(12200,70); // "ff" (long, hard to do) 145 } 146 147 void arf() { // dog arf 148 uint16_t i; 149 playTone(890,25); // "a" (short) 150 for(i=890; i<910; i+=2) // "rrr" (vary down) 151 playTone(i,5); 152 playTone(4545,80); // intermediate 153 playTone(12200,70); // "ff" (shorter, hard to do) 154 }