ATG6_AnalogSerial sketch

This Arduino sketch reads the value of voltage on pin A0, translates it to a value the LED can understand, and then sends it out to pin 9. As is other sketches we have seen, there is an initialization section, a setup() function, and a loop() function. Here’s a screenshot of the sketch. (We’ve cut out the comments at the top of the sketch.)

ATG6_AnalogSerial initialization

Our initialization section declares and sets an initial value for some variables we will need in our sketch. As we learned in the last chapter, when we declare a variable, we give the variable a name, say what type of information it will hold, give it a value, and in some cases add a qualifier that indicates whether it is a constant.

As you can see from the excerpted lines of code above, our sketch includes 4 variables. Here are the details of what each one does:

analogInPin: Holds the pin number that we take the potentiometer reading from. We set this to pin A0.

analogOutPin: Holds the pin number which is connected to our LED.This is set to pin 9.

sensorValue: This variable is initially set to 0; it holds the voltage value coming from the potentiometer.

outputValue: initially set to 0; this holds the value we are sending to the LED, which determines how brightly it shines.

Our initialization section creates these variables so we can use them later on, in the loop section. Next up, the setup section.

ATG6_AnalogSerial setup

The setup section for our sketch is only one line long, but it is a new Arduino function we have not talked about: Serial.begin(). This uses the Serial object.

The Serial object is a set of functions and variables that allows us to communicate with other devices. In this sketch we are going to use it to communicate with our computer. “begin()” is a function of the Serial object.

Quick reminder: a function is a way of organizing code or blocks of instructions to the computer. We talked about this in chapter 3 when we discussed the setup() and loop() functions. Here’s how it appears in our setup code:

This line of code tells the Arduino to open a line of communication with your computer (they will communicate through the USB cord that connects them). It also sets a rate of communication for the Arduino and your computer to communicate: 9600 bauds per second, or bps. The exact baud rate is not important at this point as long as your Arduino and your computer have a shared rate of communication.

We will look at Serial communication more closely in a few pages; for now, let’s move on to the loop() section of the code.

begin() is a function of the Serial object that sets up communication between devices.

ATG6 AnalogSerial loop code

Here’s an overview of what our loop code does:

  1. takes an analog reading from the pin our potentiometer is connected to and stores it in a variable
  2. translates that value into something the LED can understand (a value on the 0-255 scale)
  3. writes the adjusted value to the LED (pin 9)
  4. sends the two values to our computer (sensorValue and outputValue) so that we can see how they change over time
  5. wait a short amount of time (2 milliseconds) before our next reading

These steps happen in this order repeatedly for as long as your Arduino has power.

Let’s look at the code again.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>