2
$\begingroup$

How to get Mathematica to trigger an event when a variable inside a Dynamic[] changes?

I want Mathematica to Beep[] when the colour of a pixel on the screen changes. I recommend wiggling some multicolour window in the middle of the screen to test this program. On Mac you can find the coordinates of pixels at the cursor with Command+Shift+4.

<< GUIKit`;

num := Evaluate[numb[thePixel]];
thePixel := GUIScreenShot[{{1280, 1281}, {730, 731}}];
numb = Flatten[ImageData[#, "Byte"]] &;


Dynamic[
    Refresh[
        Clear[z];
        z := num // Total;
        Evaluate[z],

        UpdateInterval -> .1
     ]
]    

Now you should see a number changing as you change the colours of the pixel.


Now how do I get Mathematica to trigger a Beep[] when the number changes?

clues welcome :)

I tried both of the solutions in Detecting changes of variables' values

$\endgroup$
1
$\begingroup$

Maybe this way

Dynamic[Refresh[trigger = Total[numb[thePixel]], UpdateInterval -> 0.1, TrackedSymbols :> {}]]
Dynamic[Refresh[trigger; Beep[]; event, TrackedSymbols :> {trigger}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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