0% found this document useful (0 votes)
2 views21 pages

CT295-06 Arduino Programming

This document is a lecture on Arduino programming, covering topics such as Arduino sketches, input and output, and examples of light controlling applications. It includes basic code structure, tasks for students, and the use of variables and conditions in programming. The document also outlines assignments and tasks for practical application of the concepts taught.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

CT295-06 Arduino Programming

This document is a lecture on Arduino programming, covering topics such as Arduino sketches, input and output, and examples of light controlling applications. It includes basic code structure, tasks for students, and the use of variables and conditions in programming. The document also outlines assignments and tasks for practical application of the concepts taught.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

05/02/2023

Chapter 5
Arduino Programming
(Lập trình Arduino)
Software Engineering Dept., CICT,
Can Tho University
Lecturer: Trương Minh Thái
email: tmthai@[Link]

Outline

• Arduino Sketch
• Input and Output
• Example of light controlling application

Truong Minh Thai 1


05/02/2023

Arduino Programming

Digital I/O (2 – 13) Serial Transfer (0 -1)

USB (Data & Power)

Power Source Reset


Jumper

Alternate Power (9V)

5V / 9V / GND (x2) Analog Input (0 – 5)

Compile Upload to controller Serial Monitor

Arduino
IDE
Window

Code Editor

Text Output (Serial Data)

Truong Minh Thai 2


05/02/2023

Arduino Code Basics

• Commands and other information are sent to LED’s, motors


and from sensors through digital and analog input & output
pins

Setup - Adding an LED

Truong Minh Thai 3


05/02/2023

Sketch

• Source code for Arduino is called void setup()


a sketch. The process that takes {
a sketch and converts it into a pinMode(13, OUTPUT);
form that will work on the board is }
called compilation. The IDE uses void loop()
a number of command-line tools {
behind the scenes to compile a digitalWrite(13, HIGH); // set the LED on
sketch delay(2000); // wait for two seconds
digitalWrite(13, LOW); // set the LED off
delay(2000); // wait for two seconds
}

Arduino Code Basics

Arduino programs run on two basic sections:


void setup() {

//setup motors, sensors etc

}
void loop() {

// get information from sensors


// send commands to motors

Truong Minh Thai 4


05/02/2023

SETUP

setup() void setup() {


The setup() function is called when a port #
sketch starts. Use it to initialize variables,
pin modes, start using libraries, etc. The
setup() function will only run once, after
pinMode(13, OUTPUT);
each powerup or reset of the Arduino
board.
• The setup section is used for assigning } Input or Output
input and outputs (Examples: motors,
LED’s, sensors etc) to ports on the
Arduino
• It also specifies whether the device is
OUTPUT or INPUT
• To do this we use the command
[Link]
“pinMode”

LOOP

loop() void loop() { Port # from setup


After creating a setup() function,
which initializes and sets the
initial values, the loop() function digitalWrite(13, HIGH);
does precisely what its name delay(2000);
suggests, and loops
consecutively, allowing your digitalWrite(13, LOW);
program to change and respond. delay(2000);
Use it to actively control the Turn the LED on
Arduino board. or off
} Wait for 2 second
or 2000 milliseconds

10

10

Truong Minh Thai 5


05/02/2023

TASK 1

• Using 3 LED’s (red, yellow and green) build a traffic light that
– Illuminates the green LED for 5 seconds
– Illuminates the yellow LED for 2 seconds
– Illuminates the red LED for 5 seconds
– repeats the sequence

• Note that after each illumination period the LED is turned off!

11
11

11

TASK 2

• Modify Task 1 to have an advanced green (blinking green LED)


for 3 seconds before illuminating the green LED for 5 seconds

12
12

12

Truong Minh Thai 6


05/02/2023

DECLARING A VARIABLE

int val = 5;

assignment
Type “becomes”
value
variable name
13

13

Task

• Replace all delay times with variables


• Replace LED pin numbers with variables

14
14

14

Truong Minh Thai 7


05/02/2023

USING VARIABLES

int delayTime = 2000;


int greenLED = 13;
void setup() {
Declare delayTime
Variable
pinMode(greenLED, OUTPUT);

void loop() {

digitalWrite(greenLED, HIGH);
delay(delayTime); Use delayTime
Variable
digitalWrite(greenLED, LOW);
delay(delayTime);

15 }

15

Using Variables

int delayTime = 2000;


int greenLED = 13;

void setup() {
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
digitalWrite(greenLED, LOW);
delayTime = delayTime - 100;
delay(delayTime);
} subtract 100 from
delayTime to gradually
increase LED’s blinking
speed
16

16

Truong Minh Thai 8


05/02/2023

Conditions

• To make decisions in Arduino code we


use an ‘if’ statement

• ‘If’ statements are based on a TRUE or


FALSE question

17

VALUE COMPARISONS

GREATER THAN GREATER THAN OR EQUAL

a>b a >= b

LESS LESS THAN OR EQUAL

a<b a <= b

EQUAL NOT EQUAL

a == b a != b
18

18

Truong Minh Thai 9


05/02/2023

IF Condition

if(true)
{
“perform some action”
}

19

IF Example

int counter = 0;

void setup() {
[Link](9600);
}
void loop() {

if(counter < 10)


{
[Link](counter);
}
counter = counter + 1;

20

20

Truong Minh Thai 10


05/02/2023

TASK

• Create a program that resets the delayTime to 2000 once it has


reached 0

21
21

21

Outline

• Arduino Sketch
• Input and Output
• Example of light controlling application

22

22

Truong Minh Thai 11


05/02/2023

Input & Output

• Serial is Used for communication between the Arduino board and a computer or other
devices. All Arduino boards have at least one serial port (also known as a UART or USART),
and some have several.
• Transferring data from the computer to an Arduino is done using Serial Transmission

[Link]():
Sets the data rate in bits per second (baud) void setup() {
for serial data transmission.
Syntax: [Link](9600);
[Link](speed)
[Link](speed, config)
}
[Link]
23

23

Writing to the Console

void setup() {
[Link]()
Prints data to the serial port as human- [Link](9600);
readable ASCII text followed by a carriage [Link](“Hello World!”);
return character (ASCII 13, or '\r') and a
newline character (ASCII 10, or '\n'). This }
command takes the same forms
as [Link](). void loop() {}

[Link]

24

24

Truong Minh Thai 12


05/02/2023

Task

• Modify your traffic light code so that each time a new LED is
illuminated the console displays the status of the stoplight
• Example
Advanced Green
Green: on
Yellow: off
Red : off
Advanced Yellow
Green: off
Yellow: on
Red : off

25
25

25

IF - ELSE Condition

if( “answer is true”)


{
“perform some action”
}
else
{
“perform some other action”
}

26

Truong Minh Thai 13


05/02/2023

IF - ELSE Example
int counter = 0;
void setup() {
[Link](9600);
}
void loop() {

if(counter < 10)


{
[Link](“less than 10”);
}
else
{
[Link](“greater than or equal to
10”);
[Link]();
}
27
counter = counter + 1;
}
27

IF - ELSE IF Condition

if( “answer is true”)


{
“perform some action”
}
asdfadsf
else if( “answer is true”)
{
“perform some other action”
}

28

Truong Minh Thai 14


05/02/2023

IF - ELSE Example
int counter = 0;
void setup() {
[Link](9600);
}
void loop() {

if(counter < 10)


{
[Link](“less than 10”);
}
else if (counter == 10)
{
[Link](“equal to 10”);
}
else
{
[Link](“greater than 10”);
[Link]();
}
counter = counter + 1;
}
29

29

BOOLEAN OPERATORS - AND

• If we want all of the conditions to be true we need to use ‘AND’


logic (AND gate)
• We use the symbols &&

– Example

if ( val > 10 && val < 20)

30
30

30

Truong Minh Thai 15


05/02/2023

BOOLEAN OPERATORS - OR

• If we want either of the conditions to be true we need to use


‘OR’ logic (OR gate)
• We use the symbols ||

– Example

if ( val < 10 || val > 20)

31
31

31

TASK

• Create a program that illuminates the green LED if the counter


is less than 100, illuminates the yellow LED if the counter is
between 101 and 200 and illuminates the red LED if the counter
is greater than 200, and reset counter if it is greater than 300.

32
32

32

Truong Minh Thai 16


05/02/2023

INPUT

• We can also use our Serial connection to get input


from the computer to be used by the Arduino
int val = 0;
You can also send void setup() {
data from the Serial [Link](9600);
Monitor to Arduino by }
entering text in the void loop() {
text box to the left of if([Link]() > 0)
the Send button. {
val = [Link]();
[Link](val);
}
}
33

33

Task
• Using input and output commands find the
ASCII values of

# ASCII # ASCII @ ASCII @ ASCII @ ASCII @ ASCII

1 49 6 54 a 97 h 104 o 111 v 118


2 50 7 55 b 98 i 105 p 112 w 119
3 51 8 56 c 99 j 106 q 113 x 120
4 52 9 57 d 100 k 107 r 114 y 121
5 53 e 101 l 108 s 115 z 122
f 102 m 109 t 116
g 103 n 110 u 117
34

34

Truong Minh Thai 17


05/02/2023

INPUT EXAMPLE

int val = 0;
int greenLED = 13;
void setup() {
[Link](9600);
pinMode(greenLED, OUTPUT);
}
void loop() {
if([Link]()>0) {
val = [Link]();
[Link](val);
}
if(val == 53) {
digitalWrite(greenLED, HIGH);
} else {
digitalWrite(greenLED, LOW);
}
35
}

35

Task

• Create a program so that when the user enters 1 the green light
is illuminated, 2 the yellow light is illuminated and 3 the red light
is illuminated
• Create a program so that when the user enters ‘b’ the green
light blinks, ‘g’ the green light is illuminated ‘y’ the yellow light is
illuminated and ‘r’ the red light is illuminated

36
36

36

Truong Minh Thai 18


05/02/2023

Task

• Light Mode Video


• Create a program that
– when the user enters ‘1’ all lights turn on
– when the user enters ‘2’ all lights flash
– when the user enters ‘3’ lights cycle repeatedly
– when the user enters ‘q’ or ‘e’ the lights turn off
– when + or - is pressed the speed of the LED
increase or decrease

37

37

TASK

• Write a program that asks the user for a number and outputs
the number that is entered. Once the number has been output
the program finishes.
– EXAMPLE:

Please enter a number: 1 <enter>


The number you entered was: 1

38
38

38

Truong Minh Thai 19


05/02/2023

TASK

• Write a program that asks the user for a


number and outputs the number squared that
is entered. Once the number has been output
the program finishes.

Please enter a number: 4 <enter>


Your number squared is: 16

39

39

Outline

• Arduino Sketch
• Input and Output
• Example of light controlling application

40

40

Truong Minh Thai 20


05/02/2023

Christmas Light Assignment

• Using at least 5 LEDs create a program that


has at least 4 different modes
• Must use if statements and user input to switch
modes
• Marks:
– Creativity [4 Marks]
– Working user input [2 Marks]
– Four Working modes [2 Marks]
– Instructions [1 Mark]
41
41

41

Q&A

42

Truong Minh Thai 21

You might also like