Final Turntable – Luisa Ruiz & Aryel Owens

IMG_1192

 

Materials :

  • Arduino UNO board
  • Motor
  • Music maker shield board
  • 1k ohm resistor
  • cardboard
  • spray paint
  • SD card
  • speakers
  • jumper wires

Code :

// Voltages
//const int minV = 5; // minimum voltage to start walkman
//const int maxV = 180; // maximum output voltage to walkman
const int maxFan = 100; // maximum input my fan can provide manually

// Pins
int fanPin = A1;
//int walkmanPin = 6;
//int ledPins[5] = {8, 9, 10, 11, 13};

// Smoothing
const int numReadings = 2;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

int fanSpeed;

//// These are the pins used for the breakout example
//#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
//#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
//#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

int printDirectory(SD.open(“/”), 0);
void pD(File dir, int numTabs);

void setup() {
Serial.begin(9600);
// initial pin configuration
pinMode(fanPin, INPUT);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

Serial.println(“Adafruit VS1053 Simple Test”);
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F(“Couldn’t find VS1053, do you have the right pins defined?”));
while (1);
}
Serial.println(F(“VS1053 found”));

if (!SD.begin(CARDCS)) {
Serial.println(F(“SD failed, or not present”));
while (1); // don’t do anything more
// list files
int printDirectory(SD.open(“/”), 0);
}

// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20, 20);

// Timer interrupts are not suggested, better to use DREQ interrupt!
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
// musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
}

void loop() {
// Read fan current input
int fanValue = smoothReading(analogRead( fanPin ));
Serial.print( “Fan value smoothed:” );
Serial.println( fanValue, DEC );
// delay(10);

/*
ADD IN YOUR SCRATCH TRACK IN THIS FIRST SECTION – SO IT HAPPENS WHEN
SOMEONE JUST BARELY SPINS THE TURNTABLE
*/
if (( fanValue >= 1) && (fanValue < 3)) {
Serial.println(F(“Playing track 001”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~1.MP3”);
//delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (fanValue >= 3) {
return;
}
}
}

if ((fanValue >=4) && (fanValue < 6)) {
Serial.println(F(“Playing track 002”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~2.MP3”);
//delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (( fanValue < 4) || (fanValue > 6)) {
return;
}
}
}

if ((fanValue >= 7) && (fanValue < 9)) {
Serial.println(F(“Playing track 003”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~3.MP3”);
// delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (( fanValue < 7) || (fanValue > 9)) {
return;
}
}
}
if ((fanValue >= 10) && (fanValue < 12)) {
Serial.println(F(“Playing track 004”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~4.MP3”);
// delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (( fanValue < 10) || (fanValue > 12)) {
return;
}
}
}
if ((fanValue >= 14) && (fanValue < 16)) {
Serial.println(F(“Playing track 005”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~5.MP3”);
// delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (( fanValue < 14) || (fanValue > 16)) {
return;
}
}
}
if (fanValue >= 18) {
Serial.println(F(“Playing track 006”));
musicPlayer.stopPlaying();
delay(100);
musicPlayer.startPlayingFile(“TRACK0~6.MP3”);
// delay(3000);
while (musicPlayer.playingMusic) {
int fanValue = smoothReading(analogRead( fanPin ));
if (fanValue < 18) {

return;
}
}
}
}

// function to smooth the readings from the fan
int smoothReading( int value ) {
// subtract the last reading and add the current one
total = total – readings[index];
readings[index] = value;

// add the reading to the total and advance to the next pos in array
total = total + readings[index];
index = index + 1;

// if we’re at the end of the array wrap around to the beginning:
if (index >= numReadings) index = 0;

// calculate the average:
average = total / numReadings;
return average;
}

/// File listing helper
void pD(File dir, int numTabs) {
while (true) {

File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println(“**nomorefiles**”);
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print(‘\t’);
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(“/”);
int printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print(“\t\t”);
Serial.println(entry.size(), DEC);
}
entry.close();
}
}

Leave a Reply