5

I am trying to build an interactive model but I am facing some problems.

for example:

Column[
 {Manipulator[Dynamic[s], {1, 10, 1}, Appearance -> "Open"],
  Trigger[Dynamic[n], {0, 200, 1}, Dynamic[s], 
   AnimationRepetitions -> \[Infinity], 
   AppearanceElements -> {"PlayPauseButton", "ResetButton"}],
  Dynamic[n]}]

If you run this code and start the Trigger, when you try to change the speed of the Trigger using Manipulator, the value of Trigger jumps up and down.

I want to change the speed of rate of change of n starting from the current value of n.

Any help (other solution) will be appreciated

Thank you

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • i dont know if this solves your problem, but...try this: Animator[Dynamic[n], {0, 10, 1}, AnimationRate -> 20, AnimationRunning -> False] – Stratus Dec 17 '15 at 23:44
  • I think you are misunderstanding Trigger. It internally uses one Clock[]. Changing s only changes how the current value of that Clock[] is displayed. – Karsten7 Dec 18 '15 at 00:08
  • @Karsten7. Thank you for your answer to the question. it really helps me. can you explain your comment in more details. Thank you. – Basheer Algohi Dec 18 '15 at 05:33

1 Answers1

2

Here is one potential alternative to using Trigger.

DynamicModule[{n = 1, ss = Infinity, s},
 Column[{Manipulator[Dynamic[s, TrackedSymbols :> {}], {1, 10, 1}, Appearance -> "Open"], 
   Row[{Dynamic[Refresh[n++, UpdateInterval -> Max[1/s, ss], TrackedSymbols :> {}], 
      n--, TrackedSymbols :> {ss}, UpdateInterval -> Infinity], Spacer[10], 
     ButtonBar[{"reset" :> (n = 0; ss = -ss; ss = -ss;), "stop" :> (ss = Infinity; n--;), 
       Dynamic@If[ss == Infinity && n == 1, "start", "continue"] :> (ss = 0;)}]}]}]]

ScreenGIF

This one is a little nicer

DynamicModule[{n = 0, playQ = False, s},
 Column[{Manipulator[Dynamic[s, TrackedSymbols :> {}], {1, 10, 1}, Appearance -> "Open"], 
   Row[{DynamicWrapper[Dynamic[n], 
      Refresh[If[playQ, n = Mod[n + 1, 200]], UpdateInterval -> 1/s, 
       TrackedSymbols :> {}], TrackedSymbols :> {playQ}, UpdateInterval -> Infinity], 
     Spacer[10], 
     ButtonBar[{Tooltip[Framed[Graphics[{Line[{{-0.75, 0}, {-0.75, 1}}], EdgeForm[Black], 
            Transparent, Polygon[{{0, 0}, {0, 1}, {-0.7, 0.5}}]}, 
           PlotRange -> {{-0.9, 0.2}, {-0.1, 1.1}}, ImageSize -> 10]],
          "Reset"] :> (playQ = ! playQ; n = 0; playQ = ! playQ;), 
       Dynamic@Which[! playQ && n == 0, 
          Tooltip[Framed[Graphics[{Polygon[{{0, 0}, {0, 1}, {1, 0.5}}]}, ImageSize -> 10]], 
           "Start"], playQ, 
          Tooltip[Framed[Graphics[{Thick, Line[{{0.35, 0.2}, {0.35, 0.8}}], 
              Line[{{0.6, 0.2}, {0.6, 0.8}}]}, PlotRange -> {{0, 1}, {0, 1}}, 
            ImageSize -> 10]], "Stop"], ! playQ, 
          Tooltip[Framed[Graphics[{EdgeForm[Black], Transparent, 
              Polygon[{{0, 0}, {0, 1}, {1, 0.5}}]}, ImageSize -> 10]],
            "Continue"]] :> (playQ = ! playQ;)}]}]}]]

screenGIF2

These two examples increase n directly roughly s times per second.
If it is important for your application to use a more precise timing, than these examples could be modified along the lines of this or this answer.

Trigger isn't working the way as you seem to be expecting it to work. It doesn't directly increase n in steps of one s times per second, but, similar to Animator, uses something like Clock internally. The state of that Clock is then used to set n. In your example that concrete means, that changing s only changes how n is calculated based on that Clock. For example, if s is increased by a factor of 10, then n will not only change 10 times faster, but also will be calculated as if it was increasing 10 times faster before.
This should be an illustrative example of the situation.

DynamicModule[{s},
 {Manipulator[Dynamic[s], {1, 10, 1}, Appearance -> "Open"], 
  Dynamic[Mod[Round[Clock[{0, Infinity}]*s], 200]]}]

ClockExample

Karsten7
  • 27,448
  • 5
  • 73
  • 134