Josh From BreakoutBros.com
Published © CC BY-NC

BeatBot using Arduino with Vibration Switch and Buzzer

What is the BeatBot? The BeatBot is a BreakoutBros toy invented to entertain people who love tapping their fingers to a beat.

BeginnerFull instructions provided2 hours5,368
BeatBot using Arduino with Vibration Switch and Buzzer

Things used in this project

Story

Read more

Schematics

Untitled file

File missing, please reupload.

snip20171224_7_NUXGeqxkPq.png

Code

BeatBot code

C/C++
//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
  }
 
}

Credits

Josh From BreakoutBros.com

Josh From BreakoutBros.com

14 projects • 88 followers
At BreakoutBros we try to be a link between electronics designers and hobbyists. We make electronics tinkering easy for everyone but teach professional methods.

Comments