Sonic Pi Midi Question

I am brand new to sonic pi and I’m requesting some assistance with some code pertaining to midi. I have an external sequencer connected to sonic pi via usb midi. When I press start/stop on my sequencer I see the corresponding messages and know they are connected via midi. I also hear my sequencer audio through my computer so I am good there too. When I run my code in sonic pi it waits for the incoming midi signal and pressing play on my sequencer starts the amen break code (which is the behavior I’m looking for), however pressing stop on my sequencer does not stop playback on sonic pi. Any and all help appreciated! Below is my code:

live_loop :midi_control do
use_real_time

start_msg = sync “/midi:m8_m8_midi_1_24_0/start”
if start_msg
puts “MIDI START RECEIVED”
run_sequence
end

stop_msg = sync “/midi:m8_m8_midi_1_24_0/stop”
if stop_msg
puts “MIDI STOP RECEIVED”
stop
end
end

live_loop :midi_notes do
use_real_time

note, velocity = sync “/midi:m8_m8_midi_1_24_0:1/note_on”
if note == 36
puts “Note_on RECEIVED”
set :sequence_running, true
run_sequence
end

note, velocity = sync “/midi:m8_m8_midi_1_24_0:1/note_off”
if note == 36
puts “Note_off RECEIVED”
set :sequence_running, false
end
end

define :run_sequence do
in_thread(amen: :my_sequence_thread) do
while get(:sequence_running) do
# Jungle

  # Coded by Sam Aaron
  use_bpm 50
  
  with_fx :lpf, cutoff: 90 do
  with_fx :reverb, mix: 0.5 do
  with_fx :compressor, pre_amp: 40 do
  with_fx :distortion, distort: 0.4 do
  live_loop :jungle do
  use_random_seed 667
  4.times do
  sample :loop_amen, beat_stretch: 1, rate: [1, 1, 1, -1].choose / 2.0, finish: 0.5, amp: 0.5
  sample :loop_amen, beat_stretch: 1
  sleep 1
end

end
end
end
end
end

end
end
end

define :stop_sequence do
set :sequence_running, false
end

If I’m reading this code right, you’re not calling stop_sequence but just stop the :midi_control loop.

Also, if you format the code as preformatted text it makes it easier to read.