Вы находитесь на странице: 1из 13

Printing message

to the computer

Engr. Rudy Siguenza


April2017
PRINTING MESSAGE TO THE COMPUTER

Serial Communication is one way that two devices, like a computer


and an Arduino board, can send and receive data to each other. One
piece of data is sent at a time.

Serial Monitor Button


PRINTING MESSAGE...

The serial monitor button

1. Create a program.
2. Connect the board to the computer using USB
cable.
3. Click serial monitor button

Serial Monitor Button


PRINTING MESSAGE...

Sample program code for Serial Communication:


void setup() {
// to start serial communication
Serial.begin(9600);
Serial.println("Hello, this is from setup");
delay(3000); //a delay so that message aren't quick to read
}
void loop() {
// printing a message and then waiting a second
Serial.print("This is from loop, with a print.");
delay(1000);
Serial.println("And this is from loop with a println.");
delay(1000);
} note: The sketch need to match the rate you choose in the
serial monitor.
Note:Debugging locating the cause of any errors in your computer program code
and fix them.
The Arduino can talk with the computer using serial communication over the USB
cable. One bit of data is sent at a time. You can think of itas one letter of a word being
sent at a time, eventually to spell out the whole word.
9600 is a good number to use.

Communication requires three functions:


1. Serial.begin(9600);
This function is one that you only need to call
once in the set up () function.
Its important that this number is the same
speed as the speed that the computer and
Arduino UNO are sending and receiving data.

Quotation marks ( ). A piece of text in a code that you


dont want the computer to interpret as code.
PRINTING MESSAGE...

These are the function that send messages from the


Arduino board to the computer.
2. Serial.print(text);
It does not send a newline character at the end of
the message; in other words, the cursor is not
moved down to the next line at the end of the
message.

3. Serial.println(text);
It includes a newline character, you can imagine
this as a message with an Enter at the end of it.

Note: A newline character is like pressing the Enter or


Return key on your keyboard.
The integer (int) Function

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:

if (potValue < 341) {


digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
}
The analogRead ( ) Function

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).

The format for anlogRead ( ) function is:

variable = analogRead(pin)
int potPin = A0;

void setup() { Note:analog pins unlike digital ones, do not


Serial.begin(9600); need to be first declared as INPUT nor
} OUTPUT

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.

You should read numbers displayed ranging from 0


at one end to 1023 at the other end
The sensorValue is the variable and
the corresponding value is the analog
output of the potentiometer in pin A0.
Using the serial monitor button, the value of the
potentiometer will be printed.
Arduino Command Quick Reference

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.

Serial.print ( ) Sends a message without a newline character at the end.

Serial.println ( ) Sends a mesage with a newline at the end.

analogRead( ) Reads in the voltage on the specified pin and assign it a number from 0 (for
ground) to 1023 (for 5 V).

if ( ) Used to determine whether a section of code will be executed


Activity #3
1.Create a program that will change the speed of flashing LED
by turning the potentiometer. (5 levels).
2.Construct a circuit for the flashing LED with potentiometer

Arduino

R1
5V

A0 1k
50 %
Key=A

Grnd

Вам также может понравиться