Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The basic example of a buzzer is from arduino.cc
- by Tom Igoe
- This example code is in the public domain.
- http://arduino.cc/en/Tutorial/Tone
- */
- /*
- Here is the image from Fritzing
- http://1drv.ms/1Jqhdu3
- link to OneDrive
- I know, this here puts lots of things together. Maybe it's hard for beginners. But, it's not so hard as it looks
- the buzzer example was the link above
- the push button example - https://www.arduino.cc/en/Tutorial/Pushbutton
- the led blinking comes in arduino sketches
- about multi tasking - https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview
- and you can also search for other songs. Just make a search on google, using "carols for an arduino driven buzzer"
- */
- #include "pitches.h"
- //BUZZER
- const int buzzerPin = 10;
- int melodySize = 8;
- int theNote = 0;
- //BELL
- const int bellPin = 9;
- boolean bellRinging = false;
- int bellState = LOW;
- boolean someoneInBell = false;
- unsigned long int previousMillisBuzzer = 0;
- //HANG OUT
- const int hangoutPin = 7;
- int hangoutState = LOW;
- //LED
- const int ledPin = 8;
- unsigned long int previousMillisLED = 0;
- int delayLED = 500;
- boolean ledState = false;
- // notes in the melody:
- int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
- // note durations: 4 = quarter note, 8 = eighth note, etc.:
- int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
- void setup() {
- pinMode(bellPin, INPUT);
- pinMode(ledPin, OUTPUT);
- pinMode(hangoutPin, INPUT);
- }
- boolean playTone() {
- if(theNote < melodySize) {
- // to calculate the note duration, take one second
- // divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = 1000/noteDurations[theNote];
- unsigned long int currentMillisBuzzer = millis();
- if(theNote == 0) {
- tone(buzzerPin, melody[theNote],noteDuration);
- theNote ++;
- previousMillisBuzzer = currentMillisBuzzer;
- }
- else if(currentMillisBuzzer - previousMillisBuzzer > noteDuration) {
- previousMillisBuzzer = currentMillisBuzzer;
- // stop the tone playing:
- noTone(buzzerPin);
- tone(buzzerPin, melody[theNote],noteDuration);
- theNote ++;
- }
- return true;
- }
- else {
- theNote = 0;
- }
- return false;
- }
- void loop() {
- bellState = digitalRead(bellPin);
- hangoutState = digitalRead(hangoutPin);
- if(bellState == HIGH && bellRinging == false) {
- bellRinging = true;
- someoneInBell = true;
- previousMillisLED = millis();
- ledState = true;
- digitalWrite(ledPin, HIGH);
- }
- if(bellRinging == true) {
- if(playTone() == false) {
- bellRinging = false;
- }
- }
- if(someoneInBell == true) {
- unsigned long int currentMillis = millis();
- if(currentMillis - previousMillisLED > delayLED) {
- previousMillisLED = currentMillis;
- if(ledState == false) {
- ledState = true;
- digitalWrite(ledPin, HIGH);
- }
- else {
- ledState = false;
- digitalWrite(ledPin, LOW);
- }
- }
- }
- if(hangoutState == HIGH && someoneInBell == true) {
- if(bellRinging == true) {
- noTone(bellPin);
- }
- theNote = 0;
- bellRinging = false;
- someoneInBell = false;
- digitalWrite(ledPin, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement