Академический Документы
Профессиональный Документы
Культура Документы
to the computer
1. Create a program.
2. Connect the board to the computer using USB
cable.
3. Click serial monitor button
3. Serial.println(text);
It includes a newline character, you can imagine
this as a message with an Enter at the end of it.
Data type that creates a new variable that is an integer (whole numbers).
example: int text = 13 //declares the variable text as the
integer 13.
Sample program code using int
int LED = 13
Note: Variable declaration all variables have to be
void setup() { declared before they can be used. Declaring a variable
pinMode(LED, OUTPUT); means defining its value type, as in int, long, float, etc.,
} setting a specified name, and optionally assigning an
initial value. This only needs to be done once in a
void loop() { program but the value can be changed at any time using
digitalWrite(LED, HIGH); arithmetic and various assignments.
delay(100);
digitalWrite(LED, LOW);
delay(1000);
}
The if ( ) Function
if statements test whether a certain condition has been reached, such as an
analog value being above a certain number, and executes any statements
inside the brackets if the statement is true. If false the program skips over the
statement.
The format for an if test is:
if (variable ?? Value) { Statements; }
Example:
Reads the value from a specified analog pin with a 10-bit resolution. This function
only works on the analog in pins (A0 - A5). The resulting integer values range from
0 (for ground) to 1023 (5 V).
variable = analogRead(pin)
int potPin = A0;
void loop() {
int potValue = analogRead(potPin);
Serial.print("Potentiometer value is:");
Serial.println(potValue);
delay(1000);
}
Exercises: Reading data from Potentiometer
Procedures:
Arduino
1. Set up the circuit shown in the figure.
R1
2. Connect the USB cable from Arduino board to
5V the computer.
A0 1k 3. Open Arduino IDE.
50 %
Key=A
4. Go to: File 01. Basic AnalogReadSerial
Grnd
5. Upload the sketch to Arduino board.
6. Click Open the Serial Monitor Button to open the
Serial monitor.
Command Description
Int Data type that creates a new variable that is an integer (whole number).
Serial.begin ( ) Starts the serial communication so messages can be sent and received.
analogRead( ) Reads in the voltage on the specified pin and assign it a number from 0 (for
ground) to 1023 (for 5 V).
Arduino
R1
5V
A0 1k
50 %
Key=A
Grnd