14 March 2021

Music Box Project

 



  My project is a simple music box, but I wanted to add more customization to the tune through the use of a servo and potentiometer. The servo has jingle bells attached to it, so when you press the left button it shakes it around to make noise. I decided that it would make the most noise if the degree of rotation on the servo was smaller, so it is set to move 80 degrees and then return to 0 degrees. The potentiometer is mapped for values of 0 to 255, and taking that number and subtracting it from the hertz of each individual note which leads to an increase or decrease in pitch. The music I chose is a simplified version of Gwyn, Lord of Cinder that plays on loop as long as the switch pin is on. I think the best part about this project was learning the notes required to create the song, as well as learning how the Circuit Playground Express calculates the notes with hertz and the duration of each sound. I feel like the servo could be improved to be more impactful, and unfortunately the box muffles the sound of the jingle bells, what would you do differently?

 

#include <Servo.h>

#include <Adafruit_CircuitPlayground.h>

#include <Adafruit_Circuit_Playground.h>


Servo myservo;

int pot = A1;

int hertz;

int pos = 0;

void setup() {

  Serial.begin(9600);

  delay(1000);

  CircuitPlayground.begin();

  myservo.attach(A3); //pin for servo

}

void loop() {

  hertz = analogRead(pot); //potentiometer mapping

  hertz = map(hertz, 0, 1023, 0, 255);

  Serial.println(hertz);

  

  if(digitalRead(CPLAY_SLIDESWITCHPIN)) {

    //plays music

    CircuitPlayground.playTone((659 - hertz), 200);

    CircuitPlayground.playTone((587 - hertz), 200);

    CircuitPlayground.playTone((440 - hertz), 200);

    delay(200);

    CircuitPlayground.playTone((659 - hertz), 200);

    CircuitPlayground.playTone((587 - hertz), 200);

    CircuitPlayground.playTone((784 - hertz), 200);

    delay(200);

    CircuitPlayground.playTone((698 - hertz), 200);

    CircuitPlayground.playTone((659 - hertz), 200);

    delay(500);

  }

  if(digitalRead(CPLAY_LEFTBUTTON)) {

    //controls servo

    myservo.write(80);

    delay(100);

    myservo.write(0);

  }


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.