# Using Hardware Serial with Interrupt

**URL:** <https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570>\
**Category:** General Guidance\
**Created:** [April 5, 2023, 8:34pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570 "2023-04-05T20:34:49Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 6, 2023, 5:28pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/21 "2023-04-06T17:28:42Z")

</div>

> [@eddyhodak](#):
>
> I wanted all the critical code to be interrupt driven to train the students that were in my class.

What does "critical code" mean? It should not be in an ISR, which should be as short and fast as possible.

---

<div class="post-metadata">

**Author:** ![eddyhodak](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@eddyhodak](https://forum.arduino.cc/u/eddyhodak)\
**Post date:** [April 6, 2023, 6:31pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/22 "2023-04-06T18:31:23Z")

</div>

Clearly, you understand the issue (an ISR should NOT have much inside) and I was NOT clear about what I actually do in the code. I should have said that all the code controlling the robot's performance should be interruptable in case of critical situations such as emergencies. Typing input is so slow and potentially unclear, sorry.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 6, 2023, 6:35pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/23 "2023-04-06T18:35:51Z")

</div>

OK.

I think most people on this forum would understand the term "critical code" as meaning "time critical", i.e. code that _can't_ be interrupted or it will fail to perform some action that must be precisely timed.

---

<div class="post-metadata">

**Author:** ![eddyhodak](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@eddyhodak](https://forum.arduino.cc/u/eddyhodak)\
**Post date:** [April 6, 2023, 10:31pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/24 "2023-04-06T22:31:12Z")

</div>

My only excuse is that I am a newbie on this forum but now I am aware of the protocol and jargon. Thank you again.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [April 7, 2023, 10:40am UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/25 "2023-04-07T10:40:26Z")

</div>

You could put a **Serial.peek()** inside _your_ ISR, just to test the first character in the buffer.

if it’s **not** your special character, just leave it alone and process your serial inout when you have time,

---

<div class="post-metadata">

**Author:** ![eddyhodak](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@eddyhodak](https://forum.arduino.cc/u/eddyhodak)\
**Post date:** [April 7, 2023, 3:02pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/26 "2023-04-07T15:02:36Z")

</div>

> [@lastchancename](#):
>
> **Serial.peek()**

What an excellent suggestion. I had ChatGPT suggest code to test this out. Given it is based on Serial.available(), I think I can get the responsiveness I need for my robot cart project. Many thanks to all who have replied.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 4, 2023, 3:02pm UTC](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570/27 "2023-10-04T15:02:57Z")

</div>

Closed on October 4, 2023, 3:02pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/using-hardware-serial-with-interrupt/1111570.md?page=1)
