/*Header
Hardware and programming to better understand and use analogRead
In this cricuit we have a potentiometer which is a variable voltage divider
As you turn the knob you are chainging the resistance of both resistors in the divider circuit:
One is increasing and the other decreases
Thus, by turning the knob we alter the voltage disippated by the first resistor
This allows us to use the potentiometer like a nice, compact, analog sensor
Dont forget: Analog (many values between 0 and 5V) and sensors we READ from
For this coding demo, think about how we could display and view readings
and how we could use those readings to alter other functions (i.e., this is controls!!!)
In this example we want to use those readings to let us know
IF the reading is below a value, and IF so, we should print
a statement to the serial monitor
*/
//Global variable and pin delcaration
//No pin declarations are needed --> our analog sensing pins
//A0-A5 do not need to be declared or initialized if using analogRead
//only need to specify which pin IN analogRead
//Need a variable for the voltage reading from the potentiometer
int VoltReading; //I did not assign it a starting value - that is fine
//could also start it at zero
//in VoltReading = 0;
int VoltageMapped1; //mapping the analog read to a voltage value (volts)
int VoltageMapped2; //map to different range for demo
int VoltageMapped3; //map to third range from demo
void setup() {
// put your setup code here, to run once:
//want to print values and debug code --> want to use serial print
//if we want serial print --> we need to intialize it with Serial Begin
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//now read/sense voltage from circuit | sensor/ read-command
//Want an analog signal - analogRead
//Analog read values are 10bit | 2^10 | 1024 values | 0 - 1023
//those values correspond to 0 - 5 volts
VoltReading = analogRead(A0);
Serial.println(VoltReading);//need ln for plotter - it must just read the monitor and then use that
//may also be screwed up by text prints
//*now show them how the serial plotter can be useful*//
//*be sure to mention how the serial plotter scales to max and min values*//
//I want to display 'LEFT' IF the value is below half-the 10bit value
//and 'right' if the value is above that 10-bit value
if(VoltReading <= 512){
Serial.println("Left");
} else{
Serial.println("Right");
}
//if (condition){
//what to do with that condition
//} else - which means anything else{
//do something ELSE
//}
//Now these values correspond to what range of voltages? 0-5
//what if I wanted that voltage instead of the 10bit value
//we can MAP a value in a specified range to a value within another range
//map funciton map(value,Range1Val1,Range1Val2,Range2Val1,Range2Val2)
VoltageMapped1 = map(VoltReading,0,1023,0,5);
//min and max analog reading to min and max voltage
Serial.println(VoltageMapped1);
//ok only discrete - 0,1,2,3,4,5 - b/c map is only interger math (int)
//will get into more complex math later with decimals, ect.
//quick hack, how could I get a voltage reading from 0-5 with 2 decimal places?
VoltageMapped2 = map(VoltReading,0,1023,0,500);
//now 5V --> 500 so could divide output by 100 and get 5.00, ect.
Serial.println(VoltageMapped2);
//*get to numbers from the crowd to map between*//
//*important to show them map can map in the reverse direction - high to low and low to high*//
//give me two numbers: 12 and -638 SURE
VoltageMapped3 = map(VoltReading,0,1023,12,-638);
//can map() map in the opposite direction? YES
//map can do any direction and even negative values
Serial.println(VoltageMapped3);
delay(100);
}