Back to Parent

///              MP3 PLAYER PROJECT EXAMPLE
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////
/* CONNECTIVITEA BUILD
 * 2 thermistor circuits
 * 1 switch circuit
 * 1 led circuit
 * 1 speaker & mp3 as in example above
 */

/***********************DO NOT TOUCH***********************/

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
int buttonPrevious = 4;
boolean isPlaying = false;
# define ACTIVATED LOW;
/*****************/


bool is_on = false;
bool is_lit = false;
int last_flash = 0;

int flash_val = 300;
int state = 0;
int tru_volume = 25;
int kettle_input = A0; //thermistor pin
int cup_input = A1; //thermistor pin
int button_input = 2; //button pin
int led_output =  4; //led pin

int timer = 0;
int last_timer = 0;
int last_button_read = 0;

int temp_val = 480;

bool kettle_print = false;
bool kettle_present = false;
int kettle_count = 0;
int kettle_period = 2000;

bool cup_print = false;
bool cup_present = false;
int cup_count = 0;
int cup_period = 2000;


// setup() runs once, when the device is first turned on.

void setup(){

  pinMode(kettle_input, INPUT);
  pinMode(cup_input, INPUT);
  pinMode(button_input, INPUT_PULLUP);
  pinMode(led_output, OUTPUT);
  temp_val = analogRead(cup_input);
  Serial.begin(19200);
  mySerial.begin (9600);
  delay(300);
 
  playFirst();
  
}

void loop(){
  // this is a skeleton, choose when to play your mp3 files, make sure that your miniSD is 100% empty
  // before you use the functions
  if (digitalRead(button_input) < 1){
    digitalWrite(led_output, HIGH);
    delay(1000);
    if (isPlaying == false){
      isPlaying = true;
    }
    
  }
  cupDown();
  if (cup_present){
    Serial.print("yes");
    digitalWrite(led_output,LOW);
    delay(1000);
  }
  last_flash += 1;
}



bool cupDown(){

    if (analogRead(cup_input) >= temp_val){
      //Serial.print("cupDown");
      cup_count += 1;
      if (cup_count > cup_period){
        cup_present = true;
      }
    }
    else if (analogRead(cup_input) < temp_val){
      cup_count -= 1;
      if ((cup_count) < 0){
        cup_present = false;
        cup_count = 0;
      }
    }
}


bool kettleDown(){

    if (analogRead(kettle_input) >= temp_val){
      //Serial.print("cupDown");
      kettle_count += 1;
      if (kettle_count > kettle_period){
        cup_present = true;
      }
    }
    else if (analogRead(kettle_input) < temp_val){
      kettle_count -= 1;
      if ((kettle_count) < 0){
        kettle_present = false;
        kettle_count = 0;
      }
    }
}
/**********AUDIO**********************************/
void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(tru_volume);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void play()
{
  execute_CMD(0x0D,0,1); 
  delay(500);
}

void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}


void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}


/********DONT TOUCH**********************************/
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0