0
$\begingroup$

I have the following code:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)
If[a==1, Print["true"]]

But this doesn't work, it instead prints the following If[a==0, Print["true"]] and then changes to If[a==1, Print["true"]] and then back.

How can I get the value from the clock?

$\endgroup$
3
$\begingroup$

Well, looking at the other answers, I am not sure if I correctly understand what you want. But I'll give it a go:

a = Dynamic[Floor[Clock[2]]](*same thing*)
Dynamic[If[Setting@a == 1, "true", "false"]]

Does this yield what you wanted? The problem in your case is, that a is no "plain" integer, see FullForm[a]. Thus, the test a==1 is not executing. Using Setting lets you retrieve the value of a Dynamic var, see e.g. here: Dynamic Values

$\endgroup$
1
$\begingroup$

Well, the problem locates in the difference between Equal and SameQ. When Equal can't determin whether the args are equal or not, it returns the expression back with no changes. What you really need is SameQ here, and the code looks like this:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)If[a === 1, Print["true"]]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.