Strange behaviour if Serial Monitor on

Hi evrybody!

Not quite sure, if this Topic is in the right section, but I’ll post it anyway.
I wrote the following little program for my Arduino Leonardo. It turns a relay on and off very fast and sends sensor Values to the Serial Monitor. The program works fine for the first 30 sekonds or so, but then the relay doesn’t klick really fast anymore, but only like twice per second. The Serial Monitor doesn’t get any information anymore either. As soon as I close down the Serial Monitor, the Relay seems to act correctly again. Turning on the Serial Monitor brings me back to the same problem again. I just can not explain this strange behaviour. Can the Serial Monitor maybe only take so much information? But that would’nt axplain why the program doesn’t work. Woult it?

Thanks for helping,
Daylen

int RelayPin=2,Sensor=A0;

void setup(){
 Serial.begin(9600); 

 pinMode(RelayPin,OUTPUT);
 pinMode(Sensor,INPUT);
 analogReference(EXTERNAL); //about 1V
}

void loop(){
  
 if(Serial.available()){
  
  digitalWrite(RelayPin,HIGH);
  delay(20);
  
  int icount=0,result;  
  for(;icount<=100;++icount){
   result+=analogRead(Sensor);
  }
  result/=icount;
  
  Serial.println(result);
  
  digitalWrite(RelayPin,LOW);
  delay(20);
 } 
}

Well it is odd code. You need a while(!Serial); first thing in the setup function.

result/=icount;

will always use the same value of icount.

What is this doing?

if(Serial.available())

However having said that it runs for three minutes on my system and shows no sign of slowing down. I changed the relay to the on board LED.

Thank you for answering so quickly!

I know that “result/=icount” always uses the same value of icount. It is just, that if i want to change the times the sensor is read, I don’t have to change two things.

The program should only work if a Serial connection is available. It only needs to send values to the Serial Monitor. Nothing else.

if(Serial.available())

I will try out the while(!Serial); at the beginning. It does make sense.

I find it strange, that the program works for you. It doesn’t really slow down slowly though. It suddenly just doesn’t behave correctly. The Serial Monitor seems to block the Program.

Thank you,
Daylen

result needs to be initalised.

Yes you are filling up serials output buffer, in which case it will hang waiting for space to become available.

Restarting the serial monitor resets the micro.

Why have you put this if(Serial.available())

in and what do you think it does?

Mark

I find it strange, that the program works for you.

It might be that I was using a faster computer for my IDE, I used a Mac Book Pro.

holmes4: Why have you put this if(Serial.available())

in and what do you think it does?

He said so that the sketch only outputs stuff once you send it something.

Grumpy_Mike: What is this doing?

if(Serial.available())

From here it looks like it's being used as a switch. Nothing happens in loop() until a char is in the serial input buffer and then the code in loop() tries to stuff the serial output every 21 ms or so.

Will the anomaly Daylen gets go away if serial is set to run at 115200? Will Daylen get the same results if Daylen switches from relay to led, though I don't see why? IMO there may be something to learn including a total system lesson.

Reading and summing 100 values from an analog pin can easily overflow an int. Adifferentdatatypeforresultisinorder. There's a hint, too, in that previous sentence, in case you missed it.

Will the anomaly Daylen gets go away if serial is set to run at 115200?

No it's a Leanardo the serial rate you specify has absoloutly no effect on the serial transfer rate.

I'll stick with a Teensy then.

Hi!

The "Serial.available()" in my code was a stupid mistake: I thought the function tells me if there is an active Serial connection to my Computer. I do not want the Arduino to do anything, if it does not have this connection. Ooops. But is there a different way of doing this?

I tried out the following things: 1. put in the

while(!Serial)

at the beginning of the Setup function 2. left out that stipid "if(Serial.available() )" 3. initialized "return" with 0. 4. changed the "delay(20)" to "delay(40)", to chec if the Error has to do something with Speed. 5. only reading 10 values instead of 100.

None of these things solved my problem though. Changing the delay function didn't change the time it needs for the program to come to an error, so I do not think it has anything to do with filling up a buffer. The error accours after about 1min 10 sek no matter what I do.

"holmes 4" wrote, that the micro gets reset, if I start a serial connection. But as far as I know this doesn't happen on a Leonardo, or am I totally wrong on that?

Could the problem maybe not be the program, but my Arduino, my Computer or the Arduino Software, wich I do not think?

Thanks to evrybody helping, Daylen

Have you tried replacing the relay with just lighting up the on board LED?

It is very rapid switching for a relay and you could be into some mechanical or electrical problems with it going so fast.

I tried out replacing the relay with the on board LED. The program woks fine this way. Using the Relay again and setting the delay function to 200ms didn't work though. But this way the error comes after about 3min instead of 1min 10sek this way. This is really strange. This speed should be more than fine for the relay. I really don't know what could be the problem now. I will try out the Program without connecting the RelayPin to anything yet.

Daylen

This speed should be more than fine for the relay.

For what kind of relay? An SSR, perhaps. Not a mechanical relay.

This gets more interesting to me. I have not begun to play with relays. My ideas are more in the stone wheel category of not switching house AC faster than 1 second apart and try to engineer needs for longer periods than that.

I am using a mechanical relay. It is one on some old electronic kit I happened to have laying around. Is this really too fast for the Relay? The values I read from the Sensor depend on the relay, and they look just right to me. But even if the relay is the problem: Would that explain, why my program only seems to work if the Serial Monitor is closed once the Error accours? The fast clicking Sound of the Relay changes to a click like once a Second.

Would that explain, why my program only seems to work if the Serial Monitor is closed once the Error accours?

Since the code you posted way back hasn't been superseded, I'll assume that the code is still current. What, exactly, does that code do if there is no serial input? Without the Serial Monitor application providing input, where does the serial data needed to cause the sketch to do something come from?

Is this really too fast for the Relay

Yes. If the code works with and LED but not with a relay then your problem is not your code, it is your hardware. Have you got a diode across the coil? What current does the coil take? How are you driving the relay, transistor or FET. Most relays need more current than an arduino should be permitted to supply.

I tried out my code again as I already did before:

int RelayPin=2,Sensor=A0;
void setup(){
  while(!Serial);
 Serial.begin(9600); 
 pinMode(RelayPin,OUTPUT);
 pinMode(Sensor,INPUT);
 analogReference(EXTERNAL);
}

void loop(){
  
  digitalWrite(RelayPin,HIGH);
  delay(200);
  
  int icount=0,result=0;  

  for(;icount<=10;++icount){
   result+=analogRead(Sensor);
  }
  result/=icount;
  
  Serial.println(result);
  
  digitalWrite(RelayPin,LOW);
  delay(200);

}

Don’t ask me why, but it suddenly worked. No Error.

Although I was using the “delay(200)”. It worked just fine. I did not chagne a thing.

I will have to get a faster relay. Not a mechanic one. Probably a Photorelay.

Grumpy_Mike:
Most relays need more current than an arduino should be permitted to supply.

I did not think of this problem (you can tell I am new to this stuff). I will use a transistor in the future.

If anybody still has an idea what might have happened, and why the Serial Monitor seemed to block the program, I would be really glad to know.

Thanks to all of you helping me, :slight_smile:
Daylen

Did you have the diode across the relay? If not that would have caused back EMF spikes that could have disrupted either the arduino's code or the serial monitor.

No, I did not have a diode across. But as the code did work, I didn't have one either. (But I will in the future)

But isn't it strange, that the problem always accoured 1min 10sek after starting the Program. Why not much earlier?