Interruption of Serial Communications on Arduino

Thread Starter

SamR

Joined Mar 19, 2019
1,507
For the following program. Compiles and runs. I start the Serial Monitor (shown following code) and it asks me to input the Blink Rate. This runs in setup. Then it enters the loop and prints to the serial monitor the rate and starts the >Serial.println("the bl And it hangs! I can exit the serial monitor and restart it and it does the same thing. So the program/sketch seems to be running fine but somehow the serial communication hangs?
Code:
//Testing the Serial Port

#include <TimerOne.h>

int state = 0;
int value;
long int newtime;

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, state);
Serial.println("Enter the Blink Rate: ");
}

void loop() {
if (Serial.available()) {
value = Serial.parseInt();
Serial.print("the blink rate is: ");
Serial.println(value);
Serial.println("Enter a new blink rate: "); //This is where the serial monitor? hangs The LED does NOT blink
//so is the code hanging here? If so the setup println should not repeat
//when the serial monitor is closed and reopened?
newtime = value * 1000000;
Timer1.initialize(newtime);
Timer1.attachInterrupt(blinkme);
}
}

void blinkme() {
state = !state;
digitalWrite(13, state);
}

Serial Monitor:
monitor.JPG
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
Hi @ericgibbs

Nope... I even went back to GitHub and loaded in a new TimerOne library with the exact same results. I had already tried putting in some delay after the functions and even swapped boards, all with the same problem? Odd that it apparently works for you...
 

ericgibbs

Joined Jan 29, 2010
9,116
hi,
It works fine with the delay, I can hit the Enter key with no time value in the Serial send box and it works.
Without the delay the program hangs,, as you show, when I do that without a Time entry.

Win 10 32 Bit, Sept 2019 Arduino, downloaded the TimerOne today.

E
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
K, strange... I'm using the Win 10 Home 64bit but don't think that is a problem. Maybe I'll post it over on the Arduino forum and fight the trolls there.
 

ericgibbs

Joined Jan 29, 2010
9,116
hi,
OK,
Do you have any other sketches that are giving problems.? if yes, please post and we can run another comparative test.

BTW: I did copy and paste your original sketch listing for the test I did, so no other changes than the delay.

E
 

djsfantasi

Joined Apr 11, 2010
5,841
Timer1 is used to generate the serial communications clock.

By changing the time, you’re messing up serial comms.

So don’t use Timer1 for your interrupt if your sketch is using serial communications. Eric, not sure why it works for you.
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
All the other sketches/exercises have been working fine. I've worked with the serial port before and I2C and others but this is the first time using the TimerOne library.
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
Tried adding this and it did compile with it although with no code to parse I'm not surprised:
ISR(TIMER1_COMPA_vect)
{
//insert your code here that you want to run every time the counter reaches OCR1A
}
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
Using UNO clones both the DIP and the SOT versions. Just tried a Mega with the same results so it doesn't seem to be hardware related.
 

ericgibbs

Joined Jan 29, 2010
9,116
hi,
Do you have the latest version of TimerOne.? I used the Arduino site this morning to install that lib.
What is bugging me is that the fault you had, did show on my set-up until I added the delay, since the taking out the delay it still works.??
Sounds like a timing problem.?
E
 

Thread Starter

SamR

Joined Mar 19, 2019
1,507
Got the answer on Stack Exchanges Arduino board! It was the line end setting on my serial monitor! Changed it to no line end and now works fine.

If you enter 1 in Serial Monitor and you have line ends selected, the Serial Monitor sends "1\r\n". parseInt() reads 1 and in next loop \r or \n is available and parses as 0. You then set the Timer to 0. The Serial can't finish printing.
 

ericgibbs

Joined Jan 29, 2010
9,116
hi Sam,
Looking at the serial monitor on the 32 bit , no line ending and on the 64 bit it was crlf.
Making the 64bit no line ending, the program runs OK.
Thanks for the feedback. ;)
E
 
Top