
keyestudio
www.keyestudio.com
60
Sample Code
The program compiling is simple. An analogRead () Statement can read the value of the interface.
The A/D acquisition of Arduino 328 is in 10 bits, so the value it reads is among 0 to 1023. One
difficulty in this project is to display the value on the screen, which is actually easy to learn.
First, you need to set the baud rate in voidsetup (). Displaying the value is a communication
between Arduino and PC, so the baud rate of the Arduino should match the the one in the PC's
software set up. Otherwise, the display will be messy codes or no display at all.
In the lower right corner of the Arduino software monitor window, there is a button for baud rate
set up. The set up here needs to match the one in the program. The statement in the program is
Serial.begin(); enclosed is the baud rate value, followed by statement for displaying. You can
either use Serial.print() or Serial.println() statement.
//////////////////////////////////////////////////////////
int potpin=0;// initialize analog pin 0
int ledpin=13;// initialize digital pin 13
int val=0;// define val, assign initial value 0
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin as “output”
Serial.begin(9600);// set baud rate at 9600
}
void loop()
{
digitalWrite(ledpin,HIGH);// turn on the LED on pin 13
delay(50);// wait for 0.05 second
digitalWrite(ledpin,LOW);// turn off the LED on pin 13
delay(50);// wait for 0.05 second
val=analogRead(potpin);// read the analog value of analog pin 0, and assign it to val
Serial.println(val);// display val’s value
}
//////////////////////////////////////////////////////////
Test Result
The Sample Code uses the built-in LED connected to pin 13.
Each time the device reads a value, the LED blinks. When you rotate the potentiometer knob, you
can see the displayed value change. The reading of analog value is a very common function since
most sensors output analog value. After calculation, you can get the corresponding value you
need.
Below figure shows the analog value it reads.
Содержание KS0077
Страница 1: ...keyestudio www keyestudio com 1 Super Learning Kit for Arduino...
Страница 14: ...keyestudio www keyestudio com 14 Connection for UNO R3 Connection for 2560 R3...
Страница 21: ...keyestudio www keyestudio com 21 Circuit Connection Connection for UNO R3 Connection for 2560 R3...
Страница 32: ...keyestudio www keyestudio com 32 Connection for 2560 R3...
Страница 35: ...keyestudio www keyestudio com 35 Circuit Connection Connection for UNO R3 Connection for 2560 R3...
Страница 42: ...keyestudio www keyestudio com 42 Connection for MEGA 2560...
Страница 45: ...keyestudio www keyestudio com 45 Connection for UNO R3 Connection for 2560 R3...
Страница 59: ...keyestudio www keyestudio com 59 Connection for 2560 R3...
Страница 66: ...keyestudio www keyestudio com 66 Connection for 2560 R3...
Страница 72: ...keyestudio www keyestudio com 72 Manual for LED Segment Display Connection for UNO R3...
Страница 85: ...keyestudio www keyestudio com 85 8 bit Connection Method Connection for UNO R3...
Страница 94: ...keyestudio www keyestudio com 94 Connection for 2560 R3 Connect the motor to digital pin 9...
Страница 105: ...keyestudio www keyestudio com 105 Circuit Connection Connection for UNO R3 Connection for MEGA 2560 R3...