14 March 2021

LoZ Music Empanada Box

 



My music box currently plays Minuet of Forest from The Legend of Zelda: Ocarina of Time. I really struggled in this project with the coding aspect, learning a lot of new methods. I ended up using a simple song I enjoyed and employed it through tones basically overwriting the previous tone in the playground express. For the box, I actually had trouble finding a box that wasn't being used in my household so I went out and bought some empanadas and emptied the box out.



CODE:


 //* Nicholas Suarez
 //* NIME Music Box Project


#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Servo.h>

//variables 
//Servo
Servo myServo;
int pos = 0; 
int inc = 1; 

//potentiometer
const int potent = A1; //Potentiometer connected
const int valueThreshold = 500;
int sensorValue = 0; //Value from potentiometer
int mappedPotent = 0;

//Check to see if song is playing
bool playSong = false; 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  CircuitPlayground.begin();
  
  //Servo Stuff
  delay (1000);
  myServo.attach(10); //10 = A3
  pinMode(13, OUTPUT);
  pinMode(4, INPUT_PULLDOWN);
            }

void loop() {
    
  // put your main code here yadda yadda

  //press left button to start song
  if(CircuitPlayground.leftButton()) {   // checking if left button is true blue
    playSong = true;
    PlaySong();
  }
  //Hold right button down to stop Servo's off-tempo precussion
  else if(CircuitPlayground.rightButton()) { // checking if right button is true blue
    if (pos == 0) inc = 1;
    if (pos == 180) inc = -1;
    myServo.write (pos);
    pos += inc;
    delay(sensorValue);
    //delay(15);
    digitalWrite(13, digitalRead(4)); 
  } 

//control which direction of Servo based on what Potentiometer does
  sensorValue = analogRead(potent);
  if (sensorValue > valueThreshold)
  {
    myServo.write(90); }
    else
    {myServo.write(0);}

}  

void PlaySong()
{
  if (playSong == true)
  {
  CircuitPlayground.playTone(500,150);
    CircuitPlayground.playTone(1200,150);
    CircuitPlayground.playTone(1000,500);
    delay(200);
    CircuitPlayground.playTone(900,150);
    CircuitPlayground.playTone(1000,150);
    CircuitPlayground.playTone(900,600);
  }

  playSong = false;
}


No comments:

Post a Comment

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