Uploaded by Shereen El-Saghir

BE1200 Lesson 3 W23 - array and for loop

advertisement
BE1200
Lesson 3:Programming With Arduino
Dr. Lubna Alazzawi
1
Review
2
Review: Arduino Board
3
Review: Programming
Structure of an Arduino Program
• Functions
• Digital I/O
• Constants
• Variables
4
Review: Data Types
5
Cont. …….with variables
6
Array
• An array is a list of values that can be accessed
via an index number
• They are used to build a table of values that can easily
be accessed
• For example:
If you want to store different levels of brightness you could create
six variables called light00, light01, light02, and so on
7
Array
Creating (Declaring) an Array
All the methods below are valid ways to create (declare) an array.
Example:
More examples:
int LEDs [ ] = {2, 3, 4, 5, 6};
int LEDs [5] = {2, 3, 4, 5, 6};
char Str [ ] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
We can declare an array without choosing a size.
• The compiler counts the elements and creates an array of the appropriate size.
8
Accessing an Array
• The array is a zero indexed
• That is, the first element of the array is at index 0
int
•
a
Example 1:
X= myArray[0];
// X=3
9
Accessing an Array
• Example 2:
int myArray [ ]= { 1, 5, 8, 6 };
Y = myArray[3];
// Y=6
• Example 3:
int Array [10]={9,3,2,4,5,2,7,8,9,11};
Z = Array[4];
// Z= 5
10
Accessing an Array
Example 4:
This example demonstrates the use of an array to hold LED numbers
int ledLEDs[ ] = { 2, 3, 4, 5, 6,7};
// an array of LED numbers to which LEDs are attached
11
Example 5
12
Example 5: Code cont.…..
13
Example 5: Using Array
14
Example 5: with and without
Array
15
Example 5: Schematic Diagram
for 8 LEDs
16
Exercise 1**
• Draw the schematic diagram for the traffic lights circuit
?
17
Main Loops in Arduino
Programming
18
General Structure
19
Control Structure
• Control structures allow you to:
➢ Execute the code multiple times using For loops
➢ Perform actions only under certain conditions
using:
if, if else, while, Do while, case statements…
20
For loop
•
The for statement is used to repeat a block of statements enclosed in
curly braces a specified number of times
•
An increment or decrement counter is usually used to terminate the
loop
•
Syntax of the for loop:
for (initialization; condition; increment or decrement)
{
//statement(s);
}
Example 1:
for (int i=0; i<= 50; i++)
21
For Loop Example 2
22
Example 3
How to use a for loop to blink a single LED four
times
23
Exercise 2**:
Dim the LED using a PWM pin and for
loop - TinkerCad
24
Array and for Loop Examples
25
Exercise 3**
LED flicker using (for loop and array) - Arduino
Board
26
Exercise 4**:
Knight Rider using
(Array and for loop) with TinkerCad
27
Exercise 5**
( Using Arduino board)
SOS Morse Code Signaler
28
What is Morse code
• Morse code is a method of sending text messages by
keying in a series of electronic pulses
• Usually represented as a short pulse (called a "dot")
and a long pulse (a "dash")
• The purpose of this project is to introduce the concept
of For loop
29
Morse Code
30
Connect it up
31
Exercise 5 Code: Task 1**
• Task1:SOS Message
32
Exercise 5 Code cont……
• Task1:SOS Code
33
Exercise 5- Task 2**
• Modify the code to display:
A. The first letter of your name using Morse code
B. Number 20 using Morse code
34
What you need to submit for
Lesson 3?
Please see next slide
35
Lesson 3:
Submission
•
•
•
•
•
Exercise 1- slide 17
Exercise 2- slide 24
Exercise 3 - slide 26
Exercise 4 - slide 27
Exercise 5- slide 28, Task 1 and Task 2
Download