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:


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