Advertisement
obernardovieira

[Arduino] Who is ringing the bell?

Aug 21st, 2015
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. /*
  2. The basic example of a buzzer is from arduino.cc
  3. by Tom Igoe
  4.  
  5. This example code is in the public domain.
  6. http://arduino.cc/en/Tutorial/Tone
  7. */
  8. /*
  9. Here is the image from Fritzing
  10. http://1drv.ms/1Jqhdu3
  11. link to OneDrive
  12.  
  13. I know, this here puts lots of things together. Maybe it's hard for beginners. But, it's not so hard as it looks
  14.  
  15. the buzzer example was the link above
  16. the push button example  - https://www.arduino.cc/en/Tutorial/Pushbutton
  17. the led blinking comes in arduino sketches
  18. about multi tasking - https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview
  19. and you can also search for other songs. Just make a search on google, using "carols for an arduino driven buzzer"
  20. */
  21.  
  22. #include "pitches.h"
  23.  
  24. //BUZZER
  25. const int buzzerPin = 10;
  26. int melodySize = 8;
  27. int theNote = 0;
  28.  
  29. //BELL
  30. const int bellPin = 9;
  31. boolean bellRinging = false;
  32. int bellState = LOW;
  33. boolean someoneInBell = false;
  34. unsigned long int previousMillisBuzzer = 0;
  35.  
  36. //HANG OUT
  37. const int hangoutPin = 7;
  38. int hangoutState = LOW;
  39.  
  40. //LED
  41. const int ledPin = 8;
  42. unsigned long int previousMillisLED = 0;
  43. int delayLED = 500;
  44. boolean ledState = false;
  45.  
  46. // notes in the melody:
  47. int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
  48. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  49. int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
  50.  
  51. void setup() {
  52.     pinMode(bellPin, INPUT);
  53.     pinMode(ledPin, OUTPUT);
  54.     pinMode(hangoutPin, INPUT);
  55. }
  56.  
  57. boolean playTone() {
  58.     if(theNote < melodySize) {
  59.         // to calculate the note duration, take one second
  60.         // divided by the note type.
  61.         //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  62.        
  63.         int noteDuration = 1000/noteDurations[theNote];
  64.         unsigned long int currentMillisBuzzer = millis();
  65.        
  66.         if(theNote == 0) {
  67.             tone(buzzerPin, melody[theNote],noteDuration);
  68.             theNote ++;
  69.             previousMillisBuzzer = currentMillisBuzzer;
  70.         }
  71.         else if(currentMillisBuzzer - previousMillisBuzzer > noteDuration) {
  72.             previousMillisBuzzer = currentMillisBuzzer;
  73.            
  74.             // stop the tone playing:
  75.             noTone(buzzerPin);
  76.            
  77.             tone(buzzerPin, melody[theNote],noteDuration);
  78.             theNote ++;
  79.         }
  80.         return true;
  81.     }
  82.     else {
  83.         theNote = 0;
  84.     }
  85.     return false;
  86. }
  87.  
  88. void loop() {
  89.     bellState = digitalRead(bellPin);
  90.     hangoutState = digitalRead(hangoutPin);
  91.    
  92.     if(bellState == HIGH && bellRinging == false) {
  93.         bellRinging = true;
  94.         someoneInBell = true;
  95.         previousMillisLED = millis();
  96.         ledState = true;
  97.         digitalWrite(ledPin, HIGH);
  98.     }
  99.    
  100.     if(bellRinging == true) {
  101.         if(playTone() == false) {
  102.            
  103.             bellRinging = false;
  104.         }
  105.     }
  106.        
  107.     if(someoneInBell == true) {
  108.         unsigned long int currentMillis = millis();
  109.         if(currentMillis - previousMillisLED > delayLED) {
  110.            
  111.             previousMillisLED = currentMillis;
  112.             if(ledState == false) {
  113.                 ledState = true;
  114.                 digitalWrite(ledPin, HIGH);
  115.             }
  116.             else {
  117.                 ledState = false;
  118.                 digitalWrite(ledPin, LOW);
  119.             }
  120.         }
  121.     }
  122.    
  123.     if(hangoutState == HIGH && someoneInBell == true) {
  124.         if(bellRinging == true) {
  125.             noTone(bellPin);
  126.         }
  127.         theNote = 0;
  128.         bellRinging = false;
  129.         someoneInBell = false;
  130.         digitalWrite(ledPin, LOW);
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement