Detect when no data send by Serial

Hi community !

Sorry for my broken language, I'm French :)

I try to detect when there is no data which pass via Serial. First, I think to do it like this :

void setup () {
   Serial.begin(9600);
};
void loop () {
   if ( Serial.available() > 0 ) {
      Serial.println ( "+" );
   } else {
      Serial.println ( "-" );
   }
}

When I begin to transfer data from PureData by Serial. It works : When data are send, Serial print some '+', and when there is nothing to send, it prints '-'.

But, if I change the program like this, and replace the Serial print by the switching light of a LED, to avoid interferences.

#define ledPin 13;
void setup () {
   Serial.begin(9600);
   pinMode ( ledPin, OUTPUT );
};
void loop () {
   if ( Serial.available() > 0 ) {
      int bytes = Serial.read();
      digitalWrite ( ledPin, HIGH );
   } else {
      digitalWrite ( ledPin, LOW );
   }
}

If I open a Serial communication with PureData and begin to send data, the Led brights, but if I stop the Communication or when there is no data send, the LED do not shutdown... So, why ?

I certainly use a bad way, but I don't found some documentation about it...

My goal is to fetch data from PureData by Serial, to sync the luminosity of leds with sound ; and, when there is no data send from Pure data, throw a function.

I'm confused.

So if anybody can help me....

Thank you.

kny.

Your second program does not compile due to the semicolon in

#define ledPin 13;

I'm confused.

Hi UKHeliBob !

Yes, it's a mistake when I write the post (I write it directly, I didn't copy/paste my program :)). So just delete the semicolon :)

I have slightly reformatted your code and got rid of 2 redundant semi-colons

#define ledPin 13
void setup () 
{
   Serial.begin(9600);
   pinMode ( ledPin, OUTPUT );
}

void loop () 
{
   if ( Serial.available() > 0 ) 
   {
      int bytes = Serial.read();
      digitalWrite ( ledPin, HIGH );
   } 
   else 
   {
      digitalWrite ( ledPin, LOW );
   }
}

It works as I would expect but of course, unless you put a really long string in the Serial monitor the LED turns off almost a soon as it turns on.

The only problem is that the LED is on for such a short time you can't see it. Try temporarily adding delay(500); in loop().

When you print to the screen it retains a record that you can see.

I wonder, however, if your code will do what your Title suggests. Whether there is a byte in the Serial buffer only tells you that a byte was received. It says nothing about whether data is "being sent". Equally, the fact that the buffer was empty at the instant you checked does not say that "no data is being sent".

Perhaps you can explain why you want to know when no data is being sent.

...R

Well.
I don’t know why, but I retest my program and it works. Not as I want, but it works.

So I will explain what I need.

I send from PureData some datas ( 0 to 127 random int datas) to Arduino.
I send these datas or I don’t do it.

In Arduino, I have to fetch these datas to vary the luminosity of some LEDs.
When there is no datas sended, I have to throw a function that will redefined the group of LEDs to active.

Maybe with a simple plan :

PD

Sometimes, send some datas associated to a sound wave
datas = random int >= 0

ARDUINO

1 - define a random group of LEDs
2 - get datas from PD
WHILE datas {
vary luminosity of LEDs
}
3- when no data receive, define a new random group of LEDs

Voila !

I hope for detect these interruption of signal (datas not send) with the Serial.available. But it seems that’s not the good way…

Maybe some tips or advice ?

kny.

kuronoyurei: 3- when no data receive, define a new random group of LEDs

I wonder if what you need is to say "if no data has been received for X milliseconds" the new random group is defined

One way to do that is to set up some timing code (as in the Blink Without Delay example) - something like this

unsigned long prevByteMillis = 0;
unsigned long curMillis;

void loop() {
   curMillis = millis();
   if (curMillis - prevByteMillis >= X) {
       // set random stuff
   }

   if (Serial.available() > 0) {
       prevByteMillis = curMillis; // restarts the countdown

       // other stuff when data is received
   }


}

...R