2

I’m trying to write Android Uno code to send HEX values via SoftwareSerial but when watching the Serial Monitor the output isn’t what I expected or wanted, and I am not experienced enough yet to know what to do next.

I don’t know where the FFFFs come from to be able to eliminate them.

My desired output is:

<br>
AA130120<br>
AA0400AE

What I get is:

<br>
FFFFFFFFAA1311E<br>
FFFFFFAA20

My code:

#include <SoftwareSerial.h>
// Define the RX and TX pins to establish UART communication with the MP3 Player Module.
#define Pin_RX 0 // to RX
#define Pin_TX 1 // to TX
#define d_sleep 10000

// Define the required MP3 Player Commands:
static byte ccPlay[]   = {0xAA, 0x02, 0x00, 0xAC}; // Play:
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20}; // Set Volume (00-30):  

// Define the Serial MP3 Player Module.
SoftwareSerial MP3(Pin_RX, Pin_TX);

void setup() {
  // Initiate the serial monitor.
  Serial.begin(9600);
}

void loop() { // Play a song.
  ccSetVol[3] = {0x1E};
  send_command_to_MP3_player(ccSetVol, 4);
  delay(d_sleep);
  send_command_to_MP3_player(ccPlay, 4);
  delay(d_sleep);
}

void send_command_to_MP3_player(int8_t command[], int len){
  //Serial.print("\nMP3 Command => ");
  for (int i=0; i<len; i++) {
    MP3.write(command[i]);
    Serial.print(command[i], HEX); 
  }
  delay(100);
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
bill z
  • 23
  • 4
  • This doesn't compile for me; void send_command_to_MP3_player(int8_t command[], int len) should be void send_command_to_MP3_player(byte command[], int len) or void send_command_to_MP3_player(uint8_t command[], int len) – ocrdu Dec 10 '20 at 17:19

1 Answers1

0

Could you set the baud rate in the serial monitor to 9600Bd, run this code, and see if it spits out the bytes you expect in the serial monitor?

#define d_sleep 10000

static byte ccPlay[]   = {0xAA, 0x02, 0x00, 0xAC};
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20};

void send_command_to_MP3_player(byte command[], int len) {
  for (int i=0; i<len; i++) {
    //Serial.println(command[i], HEX);
    Serial.print(command[i], HEX);
  }
  //Serial.println();
  delay(100);
}

void setup() {
  Serial.begin(9600);
  delay(1500);
}

void loop() {
  ccSetVol[3] = 0x1E;
  send_command_to_MP3_player(ccSetVol, 4);
  delay(d_sleep);
  send_command_to_MP3_player(ccPlay, 4);
  delay(d_sleep);
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • Looks to output the same thing. – bill z Dec 10 '20 at 23:56
  • AA 13 1 1E AA 2 0 AC – bill z Dec 10 '20 at 23:58
  • Allow me to explain please. These HEX values are displayed in Serial Monitor vertically but when I try to send you what I see, this utility arranges them horizontally with a space. – bill z Dec 11 '20 at 00:00
  • I don't want the CR/LF but I do want the leading zeros please. – bill z Dec 11 '20 at 00:12
  • I'll edit the code. I can't give you leading zeroes though, but that doesn't matter for sending to the MP3 player; the right bytes will be sent if you add the MP3 player code back in, they will just display without the leading zeroes in the serial monitor. – ocrdu Dec 11 '20 at 00:15
  • Is there an advantage to using Softwareserial lib and send TX/RX on different pins or not and use the recognized pins 0 & 1 on the UNIO? – bill z Dec 11 '20 at 00:52
  • I don't know, I never use SoftwareSerial. You could ask on Arduino. – ocrdu Dec 11 '20 at 00:55
  • You're welcome. Don't forget to put the SoftwareSerial stuff back in, and the MP3.write(command[i]);, and so on; I removed some things to isolate the problem. – ocrdu Dec 11 '20 at 01:08
  • It was suggested to add a line to my code so when the Hex character is less than 16, to serial.print("0");. That makes Serial Monitor look good, but it doesn't do positive things communicating with the MP3 Player. – bill z Dec 11 '20 at 17:29
  • No, it just looks better. The MP3 player just gets a byte, and doesn't care if that byte looks like 0 or 00 to you; the byte sent is simply 0. – ocrdu Dec 11 '20 at 17:33