Week 10: A Drum with electronic sounds 🤖 [Aliya & Gautham]

For this week’s assignment, we have created a drum with an electronic sound. The idea is that you can control drum sticks (pencils) by tapping the press sensors. To move the drum sticks, we attached them to servo controllers and taped them to the box.

We also have added the button as a digital read, so that a user can change the producing sound. There are four modes of playing the instrument. You can play it by producing sound yourself or by adding extra notes to your play. 

To provide a louder sound, we used a speaker instead of a buzzer. After finishing with the functioning part of the work, we decided to add led lights, which would light up whenever the drum sticks touch the box.

#include <Servo.h>
#include "pitches.h"

Servo drum1;
Servo drum2;

int drum1Pin = 5;
int drum2Pin = 3;
int ledPin1 = 8;
int ledPin2 = 2;
int switchPin = 7;
int speaker = 6;

int prevButton;

// play sound or not
bool buzzer = false;


int interval = 250;
unsigned long timer1 = 0;
unsigned long timer2 = 0;

// play different notes with drum beat
int index = -1;
int notes[] = {NOTE_D5, NOTE_F4, NOTE_C4};

void setup() {
  drum1.attach(drum1Pin);
  drum2.attach(drum2Pin);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(speaker, OUTPUT);
  pinMode(speaker2, OUTPUT);
}

void loop() {
  int force1 = analogRead(A0);
  int force2 = analogRead(A1);

  int button = digitalRead(switchPin);

  if(button == 1 && prevButton == 0){
    if (index == -1){
      buzzer = true;
      index++;
    }
    else if (index == 2){
      buzzer = false;
      index = -1;
    }
    else{
      index++;
    }
  }

  prevButton = button;

  if(force1 > 800){
    // move the servo
    drum1.write(0);
    digitalWrite(ledPin1, HIGH);
    if(buzzer){
      if (index >= 0){
        // play sound as well
        tone(speaker, notes[index], interval*0.25);
      }
    }
  }
  else{
    // timer to allow the servo to return 
    if(millis() > timer1){
        drum1.write(40);
        digitalWrite(ledPin1, LOW);
        timer1 = millis() + interval;
    }
  }

  // for the second servo
  if(force2 > 800){
    drum2.write(0);
      digitalWrite(ledPin2, HIGH);
      if(buzzer){
      tone(speaker, notes[index], interval*0.25);
    }
  }
  else{
    if(millis() > timer2){
        drum2.write(50);
          digitalWrite(ledPin2, LOW);
        timer2 = millis() + interval;
    }
  }
}

 

Leave a Reply