/* A program that displays the correct voltage reading from the potentiometer as you
dial it on the serial monitor.*/
/* Notes:
The integrated models from the Uno,
Mega and other normal Arduinos have a 10-bit resolution.
This means that a voltage between 0 and 5 V on 5V Arduinos will
be represented by a corresponding value between 0 and 1023. A
voltage of 2.5 V will be equal to 512, which is half of the range.
The analogRead() function only provides integers from 0 to 1023.
The range 0 to 1023 is mapped to 0 to 5V. That comes built into
the Arduino. We can then calculate the voltage as follows:
V = 5 * (analogRead() value / 1023)
*/
// Declare global variables
int analogValue = 0;
int previousAnalogValue = 0;
float voltValue;
void setup() {
// put your setup code here, to run once:
// initialize serial communication
Serial.begin(9600);
pinMode(A0, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Read the analog value on the potentiometer
analogValue = analogRead(A0);
// Only convert the value if it is different to the previously read value
if (analogValue != previousAnalogValue) {
// convert the analog value (between 0-1023) to voltage (0-5.0V)
voltValue = 5 * (analogValue / 1023.);
// print the results
Serial.print("The voltage reading is: ");
Serial.print(voltValue);
Serial.println("V.");
Serial.println("");
delay(100);
// record the current value as the previous value
previousAnalogValue = analogValue;
}
}