Incorrect Readings When Digital and Analog Sensor Share Common GND/PWR

Thanks for fixing the picture. Now I can see it all I can positively say I am non the wiser as to what it shows.

However if your problem is one of things not working when you join grounds it is unlikely to be your software that is at fault, it is likely to be the hardware. Can you produce a schematic showing what you have wired please.