Weirdness using altSerial on Uno

I am using the altSerial library on an Uno and I get an error when I try to write a zero.

altSerial.write (0)

or

altSerial.write (0x00)

Assigning a zero to a variable and then writing the variable works.

Writing any value other than zero also works.

Is this a bug in the altSerial library or am I missing something?

Sorry if this is a noob question but I am a noob :confused:

edit: ya, I guess the error message would be nice DOH!

Arduino: 1.6.5 (Linux), Board: "Arduino Uno"

IO_Expander_2.ino: In function 'void loop()':
IO_Expander_2:1479: error: call of overloaded 'write(int)' is ambiguous
IO_Expander_2.ino:1479:35: note: candidates are:
In file included from IO_Expander_2.ino:2:0:
/home/jim/Arduino/libraries/AltSoftSerial/AltSoftSerial.h:53:9: note: virtual size_t AltSoftSerial::write(uint8_t)
size_t write(uint8_t byte) { writeByte(byte); return 1; }
^
In file included from /home/jim/Arduino/hardware/arduino/avr/cores/arduino/Stream.h:26:0,
from /home/jim/Arduino/hardware/arduino/avr/cores/arduino/HardwareSerial.h:29,
from /home/jim/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h:224,
from /home/jim/Arduino/libraries/AltSoftSerial/AltSoftSerial.h:30,
from IO_Expander_2.ino:2:
/home/jim/Arduino/hardware/arduino/avr/cores/arduino/Print.h:49:12: note: size_t Print::write(const char*)
size_t write(const char *str) {
^
call of overloaded 'write(int)' is ambiguous

What's the error you get? Dontcha think that might be important? Or shall we just guess at it?

It's not just altSerial. All the stream-based classes do it. Here's two ways...

const byte ZERO = 0;
...
  Serial.write(ZERO);  //method 1, not used very often as you don't normally need a name for zero
  Serial.write((byte)0); //method 2. Kinda klunky but it works

Thanks for confirming what I suspected. +1 for you :slight_smile:

The clue is here:

IO_Expander_2.ino: In function 'void loop()':
IO_Expander_2:1479: error: call of overloaded 'write(int)' is ambiguous

When altSerial.write(...) is called with a variable, the type of the variable determines which overloaded version to call. When altSerial.write(...) is called with a zero, the zero could be a byte, an int, a pointer, etc.

Why this works with other values I don't know. 259 is obviously not a byte, but the poster didn't tell us what other values work.

In any case, to force the behavior, cast the zero to an appropriate type. For example:
altSerial.write((int)0) ;