BeatBot_using_Arduino_with_Vibration_Switch_and_buzzer_code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Beat bot Code Rev 1
//used in article BeatBot using Arduino with Vibration Switch and buzzer
//Property of BreakoutBros
//Visit www.breakoutbros.com for more tutorials and reviews
 
int vibrationPin=7; //Pin For Sensing Vibration Switch
int vibrationVal;  //variable to store Vibration State
int buzzerPin = 6;  //Pin used to Play the Buzzer
int notes = 8;// Number of notes // Number of Notes used
double store[9]; //store 0-19 // Add 1 to the number of notes
                 //This variable will store the time data between notes
 
//Loop Variables
int i = 0; // I is used for number of beats
int b = 0; //b is used for loop for timing beats
 
//Buzzer Information
int rateHIGH = 1; // Used for Buzzer Ton - 1 milisecond High - change to alter pitch of tone
int rateLOW = 3;  //Used for Buzzer Tone - 3 miliseconds Low - change to alter pitch of tone
unsigned char d; //Used for the Loop for how long the tone is played
void setup()
{
  pinMode(vibrationPin,INPUT); //Set the Vibration pin as an input to sense the swithc
  pinMode(buzzerPin, OUTPUT);  //Set the buzzer pin as an output to control the buzzer sound
  Serial.begin(9600);          //Start serial Print to help with diagnostics
}
void loop()
{
  vibrationVal = digitalRead(vibrationPin); //Read the vibration switch and store into value
  if(vibrationVal == LOW) // The switch is active low - meaning if a vibration is sensed we will enter this loop
  {
    store[i] = millis(); //store the current time into our time store variable
    playBuzzer(); //Play the buzzer subroutine to indicate that it sensed the vibration
    Serial.println("Vibration Detected"); // Print to diagnostics that a "Vibration was detected
    i++; // incriment i since a time stamp was stored
  }
  if(i == notes)//If i is equal to the number of notes we wanted, this means its time to playback the notes
  {
    Serial.println("Starting Song"); // Indicated on the diagnostic that the "BeatBot" sound is starting
    delay(750); // Wait 3/4 a second to give an audible pause between the last beat indication and the start of the playback
    for(b = 0; b < (notes - 1); b++) // This will run the loop to play all the notes
    {
     playBuzzer(); //Play Buzzer function
     delay(store[b+1]- store[b]-(d*(rateHIGH+rateLOW))); // This will take the Array we stored minus the previous position to get the
     Serial.print(b);                                   //"Elapsed time" between the two. We also subtract the time it takes to play
     Serial.println(" = Playing");                      //the buzzer. This is the  time the buzzer is high(rateHIGH) + the time the buzzer is low(rateLOW)
    }                                                   // multiplied by the length of time (d)
    playBuzzer(); // Play the last beat since it will exit the for loop before the last beat is played
    i = 0;
    b = 0;
    Serial.println("Done");
   }
}
void playBuzzer() // Function to play the buzzer TOne
{
 
  for(d = 0; d < 50; d++) // This deterimines the length of the sound - this seemed to be quick enough
  {                       // The buzzer works by imposing a frequency on to it by changing the 5V pin high and low
                          // Experiment around with this to get the tone you want
    digitalWrite(buzzerPin,HIGH); // Set the buzzer High
    delay(rateHIGH);              //Delay by the High Tone setting
    digitalWrite(buzzerPin,LOW);  // Set the Buzzer Low
    delay(rateLOW);              //Delay by the Low Tone settnig
  }
 
}