From 380f045454e1a8732e44f8907a5675de695adcbb Mon Sep 17 00:00:00 2001
From: Foxunderground0 <umerirfan1205@gmail.com>
Date: Tue, 25 Apr 2023 00:35:38 +0100
Subject: [PATCH] Updated Tone.cpp to allow specifying pin state on noTone

---
 cores/arduino/Arduino.h |  1 +
 cores/arduino/Tone.cpp  | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h
index 91eeb16bc..1828ddc2b 100644
--- a/cores/arduino/Arduino.h
+++ b/cores/arduino/Arduino.h
@@ -246,6 +246,7 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 10
 
 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
 void noTone(uint8_t _pin);
+void noTone(uint8_t _pin, bool _exit_state);
 
 // WMath prototypes
 long random(long);
diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp
index 1bfb3e379..0125313e6 100644
--- a/cores/arduino/Tone.cpp
+++ b/cores/arduino/Tone.cpp
@@ -494,6 +494,23 @@ void noTone(uint8_t _pin)
   digitalWrite(_pin, 0);
 }
 
+
+void noTone(uint8_t _pin, bool _exit_state)
+{
+  int8_t _timer = -1;
+
+  for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
+    if (tone_pins[i] == _pin) {
+      _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
+      tone_pins[i] = 255;
+      break;
+    }
+  }
+
+  disableTimer(_timer);
+  digitalWrite(_pin, _exit_state);
+}
+
 #ifdef USE_TIMER0
 ISR(TIMER0_COMPA_vect)
 {