sterretje:
if(Serial) will not work to my knowledge; once the Arduino has detected that Serial is there, it does not seem to refresh it. It's definitely the case when using an external power supply and connecting / disconnecting the USB.
Something like this should be able to prove it
const byte ledPin = LED_BUILTIN;
void setup()
{
Serial.begin(57600);
while (!Serial);
// switch on led
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}
void loop()
{
// switch led off when serial disappears
if (!Serial)
{
digitalWrite(ledPin, LOW);
}
}
Also note that, if I recall correctly, you will need to open a serial port (e.g. Serial monitor).
Yes, sorry but I didn't read that. You are right, I can not use if(Serial).
I still haven't found the solution to my problem. If I print (UDADDR & _BV(ADDEN)) to my serial port I get "128", which I assume is the address that has been given to my Arduino. But I can't make that change even pluging it out and in again. It is still "128" and I don't know why...
I'm going to keep reading now.
sterretje:
If it's not a problem that you need to have a serial port open on the PC, the below should be able to detect when serial comms goes down.
void loop()
{
// if there is space to write
if (Serial.availableForWrite() > 0)
{
// send something
Serial.print("A");
// indicate serial not full
digitalWrite(ledPin, HIGH);
}
else
{
// indicate serial is full
digitalWrite(ledPin, LOW);
}
}
Thank you for your reply. I tried that and I can't make it work since Serial in the Leonardo is not the hardware serial but the USB port and I can't use "availableForWrite()" with that one... I get an error.