The code I’m using for the balancing robot is based on a sketch that is all over the internet in various guises. I can’t find any mention of the original author. The version I am working on was part of a project by Aalto University in Helsinki. It is the only one that tried to use the encoders on the dc motors for additional stability. I say tried because their project ultimately failed to produce a stable robot. I can but try, at least it’s a starting point. The Aalto University project can be found here.