# Using variable with delay in WhenEvents in NDSolve

I have a system of differential equations which is solved correctly. However, I want to trigger two events, one of which depends on the "time elapsed" after of the first event trigger. Is it possible to ever satisfy the second WhenEvent function:

Manipulate[Plot[Evaluate[y[t] /. NDSolve[{
y'[t] == n[t]*b - a y[t],
y == 0,
n == 1,
WhenEvent[y[t] > y0, {n[t] -> 0, timeEvent1 = t}],
WhenEvent[Mod[t, timeEvent1 + delay] == 0 && y[t] < y0, n[t] -> 1]},
y, {t, 0, 100}, DiscreteVariables -> {n[t] \[Element] {0, 1}},
MaxSteps -> \[Infinity]]],
{t, 0, 100}],
{b, .2, 2}, {a, .1, 2}, {delay, 1, 10}, {y0, 1.5, 20}]


where n[t] is a DiscreteVariable. In my system, when n[t]->0, y[t] begins to decrease. What I would like to happen can be summarized in the below pseudo-code:

when y[t] reaches a certain level, y[t] begin to decay,
once y[t] decays after a given time elapse of "delay", if y[t] falls below the
above mentioned level, it beings to be produced again.
Rinse and repeat.


I hope this was clear enough. I'm hoping to get oscillations like this:  I think my issue is that the Mod[t,time+delay]==0 is never satisfied due to the nature of the variable t during the integration and n[t] never gets switched back to 1. Event 1 is triggered, but Event 2 is never triggered even though y[t] falls below yo and a time of time+delay has elapsed. Please let me know if I need to provide more information.

you need to declare timeEvent as a DiscreteVariable:

 Manipulate[
Plot[Evaluate[
y[t] /. (ff =
NDSolve[{y'[t] == n[t]*b - a y[t], y == 0, n == 1,
timeEvent1 == 0,
WhenEvent[y[t] > y0, { n[t] -> 0, timeEvent1[t] -> t}],
WhenEvent[t - timeEvent1[t] == delay, n[t] -> 1 ]},
y, {t, 0, 100},
DiscreteVariables -> {n[t] \[Element] {0, 1}, timeEvent1[t]},
MaxSteps -> \[Infinity]])], {t, 0, 100}], {b, .2, 2},
{a, .1,   2}, {delay, 1, 10}, {y0, 1.5, 20}] • Great, thank you for your answer! – tquarton May 22 '14 at 20:32