Arduino Code
ID: 305
Kategorie: Treasure Chest
Menge: 0
Position:

 

#include <SoftwareSerial.h>

SoftwareSerial mp3(5, 6); // RX TX - on board
/************Command byte**************************/
#define CMD_NEXT_SONG         0X01  // Play next song.
#define CMD_PREV_SONG         0X02  // Play previous song.
#define CMD_PLAY_W_INDEX      0X03
#define CMD_VOLUME_UP         0X04
#define CMD_VOLUME_DOWN       0X05
#define CMD_SET_VOLUME        0X06

#define CMD_SINGLE_CYCLE_PLAY 0X08  // Single Cycle Play.
#define CMD_SEL_DEV           0X09
#define CMD_SLEEP_MODE        0X0A
#define CMD_WAKE_UP           0X0B
#define CMD_RESET             0X0C
#define CMD_PLAY              0X0D
#define CMD_PAUSE             0X0E
#define CMD_PLAY_FOLDER_FILE  0X0F

#define CMD_STOP_PLAY         0X16
#define CMD_FOLDER_CYCLE      0X17
#define CMD_SHUFFLE_PLAY      0x18 //
#define CMD_SET_SINGLE_CYCLE  0X19 // Set single cycle.

#define SINGLE_CYCLE_ON       0X00
#define SINGLE_CYCLE_OFF      0X01
#define CMD_SET_DAC           0X1A
#define DAC_ON                0X00
#define DAC_OFF               0X01
#define CMD_PLAY_W_VOL        0X22
#define CMD_PLAYING_N         0x4C

#define CMD_QUERY_STATUS      0x42
#define CMD_QUERY_VOLUME      0x43
#define CMD_QUERY_FLDR_TRACKS 0x4e
#define CMD_QUERY_TOT_TRACKS  0x48
#define CMD_QUERY_FLDR_COUNT  0x4f

#define DEV_TF                0X02

#define ROTARY_ANGLE_SENSOR A1

#define ADC_REF 5//reference voltage of ADC is 5v
#define VCC     5    //the default value of VCC of the control interface is 5v
#define FULL_ANGLE 1023//full value of the rotary angle is 280 degrees

static uint8_t cmdbuf[8] = {0};
static uint8_t ansbuf[10] = {0};
uint8_t i = 0;
static int8_t pre_vol = 0x03; 
//static int8_t pre_vol = 0x30; 

int tracks;

long RandomTime = 0;

void setup() {
  Serial.begin(115200);
  
  mp3.begin(9600);
  delay(500);
  
  sendCommand(CMD_SEL_DEV, DEV_TF); // Wybieramy karte SD jako zrodlo
  delay(200);

  if (mp3.available()) {
   i = 0;
   while(mp3.available() && (i < 10)) {
      delay(20);
      uint8_t b = mp3.read();
      ansbuf[i] = b;
      i++;
    }
    if (ansbuf[3] == 0x3A) { Serial.println("znaleziono karte"); }
    if (ansbuf[3] == 0x3B) { Serial.println("nie ma karty"); }
  }
  
  sendCommand(CMD_QUERY_TOT_TRACKS, 0); // zliczenie plików na karcie
  delay(200);

  if (mp3.available()) {
    i = 0;
    while(mp3.available() && (i < 10)) {
      delay(20);
      uint8_t b = mp3.read();
      ansbuf[i] = b;
      i++;
    }
    if (ansbuf[3] == 0x48) {
      tracks = int(ansbuf[6]);
      Serial.print("liczba plików: "); Serial.println(tracks);
    }
  }
  
  sendCommand(CMD_SET_VOLUME, pre_vol);
  delay(20);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {

  int pot_degrees;
  pot_degrees = getDegree();
  
  int sensorValue = analogRead(A0);
  if (sensorValue < 700) { 
    Serial.println("play sensor");
    sendCommand(CMD_SHUFFLE_PLAY, 0); // Przetasowanie utworów
    delay(100);
    sendCommand(CMD_PLAY, 0); // Następny utwór
    delay(100);
  }
  if (millis() > RandomTime){
    Serial.println("play random");
    sendCommand(CMD_SHUFFLE_PLAY, 0); // Przetasowanie utworów
    delay(100);
    sendCommand(CMD_PLAY, 0); // Następny utwór
    delay(100);
    RandomTime = millis() + 10L * random(90000L);     // 15 min.
  } else {}
  
  int8_t volume;
  volume = map(pot_degrees, 0, 1023, 0, 30); 
  if(volume != pre_vol) {
    sendCommand(CMD_WAKE_UP, 0);
    digitalWrite(LED_BUILTIN, LOW);
    sendCommand(CMD_SET_VOLUME, volume);
    pre_vol = volume;
  }
  
  if(volume == 0) {
    sendCommand(CMD_SLEEP_MODE, 0);
    digitalWrite(LED_BUILTIN, HIGH);
  }
  
}

int getDegree()
{
  int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
  float voltage;
  voltage = (float)sensor_value*ADC_REF/1023;
  float degrees = (voltage*FULL_ANGLE)/VCC;
  return degrees;
}

void playsound() {
    sendCommand(CMD_SHUFFLE_PLAY, 0); // Przetasowanie utworów
    delay(100);
    sendCommand(CMD_PLAY, 0); // Następny utwór
    delay(100);
}

void sendCommand(int8_t cmd, int16_t dat) {
  delay(20);
 
  cmdbuf[0] = 0x7e; // bajt startu
  cmdbuf[1] = 0xFF; // wersja
  cmdbuf[2] = 0x06; // liczba bajtow polecenia
  cmdbuf[3] = cmd;  // polecenie
  cmdbuf[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
  cmdbuf[5] = (int8_t)(dat >> 8);//datah
  cmdbuf[6] = (int8_t)(dat); //datal
  cmdbuf[7] = 0xef; // bajt konczacy
 
  for (uint8_t i = 0; i < 8; i++) {
    mp3.write(cmdbuf[i]);
  }
}