I am just starting in your book and trying to do project 1-4 on page 31, reading analog inputs
I tried typing the code provided and then copied it from the files give here but I get the same error.
'pot_value' was not declared in this scope
analog_input.ino: In function 'void loop()':
analog_input:13: error: 'pot_value' was not declared in this scope
analog_input:15: error: 'monitor' was not declared in this scope
analog_input:16: error: expected ';' before '}' token
the code was copied from your site files and I believe I hooked everything up as described, but to be honest, this part of the book is really vague and not helpful. The stuff on digital inputs/outputs was helpful as well as other chapters I have read so far, but your explanation on analog inputs and outputs is very sparse to say the least and not only can I not make this do anything from the instructions, I still have no idea what the analog pins would be used for at all other than a potentiometer as you mention.
Hope I am not being rude.
Please help me:
1. Figure out what I am doing wrong here.
2. Can you give me more help or a link to find out more about what the analog input/outputs are used for? The digital ones seem to do everything.
Thanks, and I am enjoying the bood so far
scrapmasterjason
Hi scrapmasterjason,
I believe you found a typo in the code for that example...
The first line in that code sample declares the variable "pot_val", while the reference to that variable in the loop() function tries to access "pot_value". Since the two are not the same, so the compiler is complaining that the one referenced in the loop is not valid.
Change the first line to read:
int pot_value;
And all should be well.. Sorry for the confusion.
As for a better explanation of what analog values are used for...
In my book, there are several examples of what the analog input pins can be useful for. In each case, the analog input pin takes any voltage input between 0v and 5v and converts it into a number between 0-1023 - where a 2.5v signal would result in an analog value of approximately 512 read by the arduino. They are useful when we need to read a specific value that is not simply On or Off like a digital input.
In chapter 4 "Linus the line bot", the line following robot uses infrared (IR) sensors to detect reflected IR light from the surface of the ground - in that case, a piece of white posterboard with black tape. Each IR sensor has an emitter LED (that shines infrared light from the bottom of the robot) and a detector which looks like another LED, but is actually a transistor switch that turns on (variably) when infrared light is shined into it. In this case, the detector passes more voltage to the analog input pin as it detects more IR light, instead of just being On or Off like a digital switch. We read these IR detectors using the Analog input pins on the Arduino to determine how much infrared light is being reflected from the posterboard (a lot if it is on top of the white part, not very much if it is on top of the black tape).
In chapter 5, "Wally the wall-bot", we used the analog input pins again, but this time to read Ultrasonic distance sensors that output a variable voltage (again 0-5v) depending on how far away an object is in front of the sensor. These sensors will only output a small amount of voltage (less than 1 volt) if the object is less than 6" away - while it outputs a higher voltage (closer to 5v) when the object is further away, up to 20 feet. So using the analog value from the input pin, we can determine exactly how far away (within 1" of actual distance) an object is in front of the ultrasonic sensor based on the analog value that is read by the Arduino.
In chapter 8, "The Explorer bot", we used the analog input pins to read the value of a current-sensor on the motor-controller. The current sensors chips output an analog voltage that is proportional to the current being passed through it, so you can determine exactly how much amperage is passing through the motor controller at any given time... if it exceeds a dangerous level, then the Arduino was programmed to send a stop signal to the motor-controller so it wouldn't overheat.
In chapter 11, "The Segbot", the analog inputs were used again to read the values of the Accelerometer and Gyroscope sensors to keep the segbot balanced while riding. The accelerometer output an analog signal proportional to how much it was tilted in either direction, while the gyroscope output an analog signal proportional to how fast the sensor is rotating at any given time. Together, these signals could be combined to get an accurate determination of how many degrees the segbot frame is tilted in either direction, and then compensate for that angle using the motors to keep it upright.
There are other popular analog sensors not used in the book that are common, so if you have any questions about a specific one, feel free to ask.
Here are some other resources that might be helpful:
a short video on using analog inputs for Arduino
http://arduino.cc/en/Reference/analogRead
Thanks for buying my book and feel free to ask any more question you have.
Cheers,
JD