special loop current frequency

For some time now I’ve been wondering how to make a closed loop system for
let arduino generate a square wave signal that hooks up to a variable load
in frequency.
I was thinking about pid or if loop.
In essence, the square wave must be generated at the frequency where the impedance is lower,
moreover, it must be taken into consideration that the frequency where the impedance is low is not fixed but moves both towards lower frequencies and towards higher frequencies continuously as indicated in the attached drawing in three different moments represented on the graph.
Currently I don’t know how to do support I ask not so much for the values ​​at stake at the hardware level
but precisely on the writing of a software capable of making continuous frequency brushing and hooking in the point where there is a higher current absorption.

your image:

6716236932dd0c3fd21fe121df9d87e6a939e14c.jpg

Measure your resistance
Feed resistance into a formula, send the o/p to the tone() function.