DATE: Sep 25, 2023

TEAM: Yuri Shin, Rain Fu, Seungmi Lee

MATERIALS USED: Paper box, mini broom stick, wires, servo motor, buzzer, breadboard, photo sensor, resister

IDEATION & PROCESS

Since Yuri had the servo motor in her Arduino package, our initial idea was to create a mini LP player that rotates using the servo motor and plays a melody through the buzzer. However, when we connected the servo motor to the breadboard, we realized it could only rotate 180°. So, we began searching for a motor capable of 360° movement, which led us to a DC motor we were fortunate enough to find at VFL. Unfortunately, after numerous trial and error attempts, we had to admit that we couldn't use this motor due to the additional equipment and knowledge required. Consequently, we made the decision to change the assignment's concept to that of a metronome since I had been contemplating an object that regularly rotates below 180°. Given that our buzzer melody was programmed for a Harry Potter theme song, the idea of a broomstick ticking instead of a metronome's needle came to mind. Additionally, we wanted to incorporate a photosensor capable of adjusting the sound's speed based on light intensity.

CODE

#include <pitches.h>
#include <Servo.h>
#define buzzerPin 9
Servo myservo;
int pos = 0;
int brightness = 0;
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousMillis = 0;
unsigned long previousMotorMillis = 0;  // will store last time the LED was updated
int TimeInterval = 0;
int note = 0;
int addAmount = 10;
int pauseBetweenNotes = 2;
int melody[] = {
  REST, NOTE_D4,
  NOTE_G4, NOTE_AS4, NOTE_A4,
  NOTE_G4, NOTE_D5,
  NOTE_C5,
  NOTE_A4,
  NOTE_G4, NOTE_AS4, NOTE_A4,
  NOTE_F4, NOTE_GS4,
  NOTE_D4,
  NOTE_D4,
  NOTE_G4, NOTE_AS4, NOTE_A4,
  NOTE_G4, NOTE_D5,
  NOTE_F5, NOTE_E5,
  NOTE_DS5, NOTE_B4,
  NOTE_DS5, NOTE_D5, NOTE_CS5,
  NOTE_CS4, NOTE_B4,
  NOTE_G4,
  NOTE_AS4,
  NOTE_D5, NOTE_AS4,
  NOTE_D5, NOTE_AS4,
  NOTE_DS5, NOTE_D5,
  NOTE_CS5, NOTE_A4,
  NOTE_AS4, NOTE_D5, NOTE_CS5,
  NOTE_CS4, NOTE_D4,
  NOTE_D5,
  REST, NOTE_AS4,
  NOTE_D5, NOTE_AS4,
  NOTE_D5, NOTE_AS4,
  NOTE_F5, NOTE_E5,
  NOTE_DS5, NOTE_B4,
  NOTE_DS5, NOTE_D5, NOTE_CS5,
  NOTE_CS4, NOTE_AS4,
  NOTE_G4
};
int durations[] = {
  2, 4,
  4, 8, 4,
  2, 4,
  2,
  2,
  4, 8, 4,
  2, 4,
  1,
  4,
  4, 8, 4,
  2, 4,
  2, 4,
  2, 4,
  4, 8, 4,
  2, 4,
  1,
  4,
  2, 4,
  2, 4,
  2, 4,
  2, 4,
  4, 8, 4,
  2, 4,
  1,
  4, 4,
  2, 4,
  2, 4,
  2, 4,
  2, 4,
  4, 8, 4,
  2, 4,
  1
};
void setup() {
  //Serial.println( sizeof(durations) );
  Serial.begin(9600);  // in case we need serial monitor
  pinMode(buzzerPin, OUTPUT); //Set buzzer pin as output
  myservo.attach(5);
}
void loop() {
  brightness = analogRead(A0);
  currentMillis = millis();
  MusicTime();
  motor();
}
void playMusic()
{
    if(note>61)
    {
      note = 0;
    }
    int size = sizeof(durations) / sizeof(int);
    float frequency = map(brightness, 300, 900, 1.0, 3.0);
    float duration = 1000 / durations[note];
    tone(buzzerPin, melody[note], duration * frequency);
    pauseBetweenNotes = duration * frequency * 1.3;
    note++;
}
void updateTime()
{
  if(currentMillis - previousMillis >= TimeInterval)
  {
    previousMillis += TimeInterval;
  }
}
void motor()
{
  int frequencyMotor = map(brightness, 300, 900, 167, 500);
  if(currentMillis - previousMotorMillis >=  frequencyMotor)
  {
    previousMotorMillis = currentMillis;
    if(pos>90||pos<0){
      addAmount = -addAmount;
    }
    pos+=addAmount;
    myservo.write(pos);
    //Serial.println(pos);
  }
}
void MusicTime()
{
  if(currentMillis - previousMillis >= pauseBetweenNotes){
    playMusic();
    previousMillis = currentMillis;
  }
}

FINAL PROTOTYPE

https://youtube.com/shorts/hEMvVyP9gTg?feature=share