Don’t reinvent the wheel, Im sure there is are simple methods using the existing components, maybe check out: