Software serial - limit at 4 chars

Dear reader

I am going to send 8 or 10 chars from Arduino uno to MP3 player FN-M16P.
When i send only 4 chars every thing works fine, if i send 5 i get error codes from arduino.

call of overloaded 'write(int)' is ambiguous

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
const int byte8 = 7;
const int byte10 = 6;
char x = 0;
char y = 0;
//int start = 0x7H

void setup() {
    pinMode(byte8, INPUT);
    pinMode(byte10, INPUT);
  // put your setup code here, to run once:
  // set the data rate for the SoftwareSerial port
    mySerial.begin(9600);
}

void loop() {
  // spiller nr 10, når 7 sættes til vcc
x=digitalRead(byte8);
y=digitalRead(byte10);

  if (x = 1){
    mySerial.write (0x7E);
      mySerial.write (0xFF);
        mySerial.write (0x06);
          mySerial.write (0x03);} 
/*  if (x = 1){       
           mySerial.write (0x00);
              mySerial.write (0x00);
                mySerial.write (0x0A);
                  mySerial.write (0xEF); }*/

// spiller nr 4, når 6 sættes til vcc
  if (y = 1){
    mySerial.write (0x7E);
      mySerial.write (0xFF);
        mySerial.write (0x06);
          mySerial.write (0x03);}
/*  if (y = 1){
            mySerial.write (0x00);
              mySerial.write (0x00);
                mySerial.write (0x04);
                  mySerial.write (0xEF);
                    mySerial.write (0xEE);
                      mySerial.write (0xEF); }*/

}

Software serial doesn't like 0x00 for some reason.
Do the following:
mySerial.write (byte(0x00));

1 Like

Thank You Jim

that closed the topic, How do i rank the answer

No need to.
Just have fun!

@sigurd22

if you want to understand the reason :

The SoftwareSerial class inherits from the Stream class which inherits from the Print class.

The Print class has two versions of the write() function with one parameter

    size_t write(uint8_t) ; // virtual
    size_t write(const char *str);

When the compiler sees mySerial.write (0x00); it needs to decide which function to call.

in C++ when you don't specify the type of a integer literal, the default type is int if it fits the value, so 0x00 is not a byte, it's an int.

The compiler sees both options remains possible — an int could be promoted down to a byte or could represent an address in memory and thus it does not know which function to pick and you get a compilation message

call of overloaded 'write(int)' is ambiguous

by writing mySerial.write (byte(0x00)); you tell the compiler that 0x00 is actually of byte type - which is the same thing as uint8_t and so there is no more ambiguity, the compiler knows which function to call.

BTW side notes:

don't write

unless you want to assign 1 to x. testing is done with the == as in if (x == 1)

also you can send multiple bytes in one go with

const byte command_x1[] = {0x7E, 0xFF, 0x06, 0x03) ;
mySerial.write(command_x1, sizeof command_x1); // sends all the bytes from the array

As @J-M-L pointed out x=1 should be x==1

You can write an array of bytes instead of using individual bytes:

const byte xData[] = { 0x7E, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x0A, 0xEF};

void loop() {
  // spiller nr 10, når 7 sættes til vcc
  x = digitalRead(byte8);
  y = digitalRead(byte10);

  if (x == 1) {
    mySerial.write (xData, sizeof(xData));
  }

yes, as already mentioned in post #7 :wink:

Ah, I see now.
Been having problems the last few weeks where the page does not automatically refresh and show the latest posts while I'm off working on my response.

OK - yes I've had that too from time to time

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.