Back to Parent

Dog Collar Particle Code
# 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 volume;


String toneVoice = "";  

void tricksRoll(const char *event, const char *data){
    Serial.println(data);
    String sdata = data;
    execute_CMD(0x16,0,0);
    delay(1000);
    if (sdata.equals("0")){
        execute_CMD(0x03,0,1);
    }
    else if (sdata.equals("1")){
        execute_CMD(0x03,0,2);
    }
    else if (sdata.equals("2")){
        execute_CMD(0x03,0,3);
    }
    
    delay(2000);
    
}

void tricksStay(const char *event, const char *data){
    String sdata = data;
    execute_CMD(0x16,0,0);
    delay(1000);
    if (sdata.equals("0")){
        execute_CMD(0x03,0,4);
    }
    else if (sdata.equals("1")){
        execute_CMD(0x03,0,5);
    }
    else if (sdata.equals("2")){
        execute_CMD(0x03,0,6);
    }
    delay(2000);
}

void tricksSit(const char *event, const char *data){
    String sdata = data;
    execute_CMD(0x16,0,0);
    delay(1000);
    if (sdata.equals("0")){
        execute_CMD(0x03,0,7);
    }
    else if (sdata.equals("1")){
        execute_CMD(0x03,0,8);
    }
    else if (sdata.equals("2")){
        execute_CMD(0x03,0,9);
    }
    delay(2000);
  
}

void setup ()
{
    
 Particle.subscribe("tricks_roll_over",tricksRoll, ALL_DEVICES);
 Particle.subscribe("tricks_stay", tricksStay, ALL_DEVICES);
 Particle.subscribe("tricks_sit", tricksSit, ALL_DEVICES);
 Serial.begin(9600);
 Serial1.begin(9600);
 
 
 execute_CMD(0x3F, 0, 0); // Send request for initialization parameters

 //while (Serial1.available()<10) // Wait until initialization parameters are received (10 bytes)
 delay(1000); // Pretty long delays between succesive commands needed (not always the same)

 // Initialize sound to very low volume. Adapt according used speaker and wanted volume
 execute_CMD(0x06, 0, 0x30); // Set the volume (0x00~0x30)
 delay(1000);
 // execute_CMD(0x0D,0,1);

// execute_CMD(0x11, 0, 0x01);
}


void loop()
{


}


// By Ype Brada 2015-04-06
// https://community.particle.io/t/a-great-very-cheap-mp3-sound-module-without-need-for-a-library/20111
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
 // Calculate the checksum (2 bytes)
 int16_t 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, checksum >> 8, checksum & 0xFF, End_Byte};

 //Send the command line to the module
 for (byte k=0; k<10; k++)
 {
  Serial1.write( Command_line[k]);
 }
}
Chileshe Otieno, Justin Kufro, Kevin Thies Click to Expand

Content Rating

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

0