# Control the stop of a live\_loop with a condition

**URL:** https://in-thread.sonic-pi.net/t/control-the-stop-of-a-live-loop-with-a-condition/3479
**Category:** \[P\] Patreon Support
**Created:** [March 21, 2020, 10:42am UTC](https://in-thread.sonic-pi.net/t/control-the-stop-of-a-live-loop-with-a-condition/3479 "2020-03-21T10:42:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)
#### Post date: [March 21, 2020, 10:42am UTC](https://in-thread.sonic-pi.net/t/control-the-stop-of-a-live-loop-with-a-condition/3479/1 "2020-03-21T10:42:05Z")

</div>

Hi everybody,

I’m working on a apckey 25 project and try to start or stop a live\_loop if a toggle is on.  
So i try with this piece of code to try to understand what i’m doing 🙂

```ruby
live_loop :metronome do
  sleep 1
end

live_loop :foo do
  sync :metronome
  stop if get[:toggleOn]
  sample :bass_dnb_f
  # sleep 1
end

live_loop :toggle_inspector do
  sync :metronome
  tick :boo
  if look(:boo)==3
    set :toggleOn, true
    tick_set :boo, 0
  else
    set :toggleOn, false    
  end
end

```

So why the live\_loop doesn’t restart after stopping. You see it’s a big mess in my head today…  
Sure there’s an answer.

---

<div class="post-metadata">

### Author: ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)
#### Post date: [March 21, 2020, 10:51am UTC](https://in-thread.sonic-pi.net/t/control-the-stop-of-a-live-loop-with-a-condition/3479/2 "2020-03-21T10:51:36Z")

</div>

Hi @nlb,

well, the problem is, that once you’ve stopped the `live_loop` you’d have to (re)evaluate the code to start it again. One way how to circumvent this is, to let the `live_loop` run but stop only the sample (or synth) code from being run:

```ruby
live_loop :foo, sync: :metronome do # sync just once: http://in-thread.sonic-pi.net/t/live-loops-sync-questions/1172/16
  sample :bass_dnb_f if get(:toggleOn) == false
  sleep 1
end

```

(not tested)

---

<div class="post-metadata">

### Author: ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)
#### Post date: [March 21, 2020, 12:42pm UTC](https://in-thread.sonic-pi.net/t/control-the-stop-of-a-live-loop-with-a-condition/3479/3 "2020-03-21T12:42:23Z")

</div>

thanks @Martin.  
yes i forgot that point : we can’t stop a live\_loop and relaunch it. so let’s the live\_loop living and play the  
sample **if the toggle is on**. It’s a way.  
But finally i find an old code source that solves my problem, i will need to adapt to my purpose. Of course it comes from @robin.newman 🙂

```ruby
live_loop :p1 do
  
  toggle_1_state = sync "/midi:apc_key_25_midi_1:4:1/note_on"
  set :t1, toggle_1_state[0] # 1 if pushed 0 when released
  doLoopAmen if toggle_1_state[0]==1
end

define :doLoopAmen do # function to deal with loop_amen sample
  set :kill1,false
  live_loop :controlLoopAmen do
    s1 = sample :loop_amen #s1 stores reference to the running sample
    
    in_thread do #in a thread poll for button up cue (:C1=>0)
      loop do
        if get(:t1)==0 #if button up cue then
          kill s1 #kill the sample pointed to by the s1 reference
          set :kill1,true #set kill1 flag true
          stop # quit loop
        end
        sleep 0.15 #time between checks for button up cue
      end
    end
    40.times do # split sleep time to insert poll for kill1
      sleep (sample_duration :loop_amen) / 40.0
      stop if get(:kill1)
    end
  end #of live loop
end #of function

```
