2

Using this code to try and play a random song when someone walks by my distance sensor I get an error, the same song (track 0001) plays every time. Any advice?

#include <SoftwareSerial.h>
SoftwareSerial Geno(7, 8); // Rx, Tx

unsigned char Data[10];
unsigned char i;

// defines pins numbers
const int trigPin = 8;
const int echoPin = 9;

// defines variables
long duration;
int distance;

void setup() {
  delay(1000);
  Geno.begin(9600);
  delay(1000);
  SetVolume(30);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}

void playTrack(int num) {
    delay(100);

    Data[0] = 0x7E;
    Data[1] = 0x04;
    Data[2] = 0xA0;
    Data[3] = 0x00;
    Data[4] = 0x00 + num;
    Data[5] = 0x7E;
    Command(Data,5);

    play_pause();

    delay(10000);
}

void SetVolume(int vol) {
   Data[0] = 0x7E; // START
   Data[1] = 0x03; // Length Not 0x02
   Data[2] = 0xA7; // Command
   Data[3] = vol;  // new volume
   Data[4] = 0x7E; // END
   Command(Data,5);
}

void play_pause() {
  Data[0] = 0x7E; // START
  Data[1] = 0x02; // Length
  Data[2] = 0xA3; // Command
  Data[3] = 0x7E; // Mode parameter
  Command(Data,4);
}

void Command(unsigned char *Data, int length) {
  for (int i=0; i<length; i++) {
    Geno.write(Data[i]);
  }
}

void loop() {
  //Assigns a random number 
  int song = random(0,200);
  Serial.print("Song is:");
  Serial.print(song);
  Serial.print("\n");
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);
  if (distance < 20) {
    playTrack(random(1,25));
  }
  //playTrack(1);
  //playTrack(2);
}
4
  • 1
    what is the error?
    – jsotola
    Commented Feb 14, 2018 at 1:26
  • That was bad phrasing on my aprt, the error is it plays the same thing over and over again
    – Pagaley 12
    Commented Feb 14, 2018 at 1:28
  • 2
    start with basic code .... no distance measurement, no random track selection .... only code that plays track 03 (anything other than 01) ... once that works, add random selection .... when that works, add distance sensor code
    – jsotola
    Commented Feb 14, 2018 at 1:33
  • by its self the play random works but once I add in the code for the distance it play the same song like before, any ideas?
    – Pagaley 12
    Commented Feb 14, 2018 at 2:43

2 Answers 2

0

random(1,25) is deterministic. It's PRNG should produce the same sequence of outputs in the same order each time. You need to call randomSeed() at some point, passing it an unpredictable value. Something like:

if(distance < 20){
  randomSeed(micros());
  playTrack(random(1,25));
} 
0

Disclaimer: I know nothing about this media player except what's in this question. I'd never heard of it before. The following is simply from observation of the code you posted.

Look at your play_pause() function where the meanings of all the elements are described.

Data[0] = 0x7E;          // START
Data[1] = 0x02;          // Length
Data[2] = 0xA3;          // Command
Data[3] = 0x7E;          //Mode parameter
Command(Data,4);

It seems a message starts with 0x7E, followed by the length , followed by a command and whatever else, followed by another 0x7E.

The length field is 1 less than the total length, so I guess it doesn't include the 'start', or whatever - there are various equivalent ways you could look at it.

In playTrack() you set 6 elements, but with the length element set to 4. You then tell Command() there are 5. So the last element (0x7E) won't get sent, and the length element which you have sent is wrong anyway.

The receiver is going to get into some weird state, I presume. Perhaps that explains the problem you're seeing.

2
  • This is almost definitely a dumb question but I am new to this, where is the length element and where is it set to 4?
    – Pagaley 12
    Commented Feb 15, 2018 at 1:08
  • I have explained in more detail above. Hope that makes more sense.
    – Mark Smith
    Commented Feb 15, 2018 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.