0

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? Others have tried it and it works for them? Using an Alien Aurora R5 running Win10 64bit Home. Added some delays after another person tried it and it hung on them before adding delay and then ran for them. They are using Win10 32bit and when tried on their 64bit laptop it hangs.

//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();
    delay(100);
    Serial.print("the blink rate is: ");
    Serial.println(value);
    Serial.println("Enter a new blink rate: ");
    newtime = value * 1000000;
    Timer1.initialize(newtime);
    Timer1.attachInterrupt(blinkme);
    delay(500);
  }
}

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

Serial Monitor

  • Why delay(100); when you already initialize the Timer1 with the right blink time? Delaying in ISRs should be avoided at all costs. – Maximilian Gerhardt Nov 28 '19 at 16:23
  • Delays were added after in attempt to get it to run. Was advised by another that it hung for them UNTIL they added the delay at end of loop function. – SamR Nov 28 '19 at 16:26
  • 4
    Never ever use a delay in an interrupt – chrisl Nov 28 '19 at 16:26
  • It hangs without any interrupts. – SamR Nov 28 '19 at 16:32
  • Please change int state = 0; to volatile int state = 0; and remove all delay() calls. Before Timer1.initialize(...), add the line Serial.flush();. Does it work once now but not repeatedly? – Maximilian Gerhardt Nov 28 '19 at 16:37
3

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 because the Timer fires without a pause.

| improve this answer | |
  • BINGO! Changed the monitor to no line end and now works fine! Thanks! – SamR Nov 28 '19 at 17:14
  • @SamR, accept he answer to mark it solved – Juraj Nov 28 '19 at 17:14
  • 1
    @SamR, Changed the monitor to no line end ... that is not the way to correct the problem ... instead, insert code that prevents newtime from being zero – jsotola Nov 30 '19 at 19:03
0

Change io 13 to (suggestion) 10 and attach led + 1k resister to io 10. 13 seems to be in use. Also No line end in Serial.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.