You could make your code much nicer and simpler by using a switch/case statement rather than all the nested if's.
Your diagram does not show how you can control the external voltage. Can you explain that please?
If you are applying 9V as shown then the voltage on the analog pin is 9V * ( 10/16.2) = 5.56V which is too high.
Also you need to connect the regulator output to the 5V pin; Vin needs >7V
Can you show the raw analog values you are getting as a function of supply voltage?
1 Like