Vous êtes sur la page 1sur 8

MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

Background:
Today’s tutorial will cover two parts, first we will construct an array of LEDs and learn to use if statements to
control them. In the second part we will introduce a physical button to control the state of the Arduino and circuit
system.

In the first part of the tutorial we will be introducing arrays and “for” loops as ways of controlling large data sets
and conditional programming respectively. In the second part we will introduce a physical control to the circuit
and control it using “while” loops.

Hardware:
1 × Arduino Eleven

3 × LED, red or green

3 × 470 Ohm Resistor (yellow-violet-black-black-gold)

1 × 10k Ohm Resistor (brown-black-black-brown-gold)

1 × Pushbutton

1 × breadboard

6 × jumper wires

Page | 1 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

Part a)

Setup the hardware as shown below:

Page | 2 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

The circuit diagram:


An array is a string of anything that you need in your program. In this case we have eight LEDs so instead of
assigning each LED a variable name we will do it all at once with an array.

First we need to declare the variable names. We need variables for the number of LEDs we have connected, the
pins the LEDs are connected to and the delay we want the LEDs to have. Of course you don’t need to put in the
“ledCount” and “ledDelay”, but you will end up having to change multiple values in the code every time you wish
to change either the number of LEDs or the delay between them.

int ledCount = 3;

int ledPins[] = { 11, 12, 13};

int ledDelay = 300;

Page | 3 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

Next we need to initialise the setup of the code. Remember this code only runs once at the start of the program.
We are going to take a shortcut and use a “for” loop to make it easier on ourselves to set all the LEDs as outputs.
What this code will do is create a local variable (is only defined in the area it is called, in this case the “setup()”)
called “thisLed” and give it an initial value of 0. Then for every integer where “thisLed” is less than “ledCount” (3)
it will increase the count by 1 and set the corresponding value in the “ledPins” array to be a digital pin and an
output. This means we don’t have to type out the same line 8 times. It then goes back to the top of the “for” loop
and repeats until “thisLed” = “ledCount”.

void setup(){

for (int thisLed = 0; thisLed < ledCount; thisLed++) {

pinMode(ledPins[thisLed], OUTPUT);

Now we get to the “loop()” and the main part of the program that will tell the LEDs to flash. For this to work we
need to “for” loops, one that goes from left to right (13-6) and one that goes form right to left (6-13). “++” and “--
“ add and subtract 1 to the integer.

void loop(){

for(int thisLed = 0; thisLed < ledCount; thisLed++) {

digitalWrite(ledPins[thisLed], HIGH);

delay(ledDelay);

digitalWrite(ledPins[thisLed], LOW);

for(int thisLed = ledCount - 1; thisLed > 0; thisLed--) {

digitalWrite(ledPins[thisLed], HIGH);

delay(ledDelay);

digitalWrite(ledPins[thisLed], LOW);

Page | 4 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

Part b)
Now we will include a button that has two states (ON, OFF) to control the scanning of the LEDs. We are going to
write the code so that the LEDs only scan backwards and forwards when the button is pressed, otherwise you are
stationary.

Page | 5 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

Setup the hardware as shown below:

Page | 6 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

The circuit diagram:


We are going to work off the previous code, but with a few modifications so it can recognise the button being
pressed and respond accordingly.

First you can see in the diagram that the button has to be wired up to the 5V power terminal. This is so the input
we plug it into can see when it is on (5V) and when it is off (0V).

We need to introduce an new integer to control which pin connects to the button in this case we have it
connected to pin 3, therefore the following needs to be added to the start of the code.

int buttonPin = 3;

Now we need to tell the system what to do while the button is being pressed, for this we use a “while” loop. A
“while” loop tells a program what to do while a condition is being met by the code. So long as the condition is true

Page | 7 -1
MONASH UNIVERSITY MEC2402 DESIGN 1 Name:

Tutorial 2 Scanning lights or ALL the LEDs

the loop will continue to repeat itself. While the button is being held down we want the LEDs to scan backward
and forward, and for them to stop when the button is released. For this we need to add the following code twice,
once in each “for” loop.

while(digitalRead(buttonPin)== LOW) {

delay(10);

Work out for yourselves where the “while” loops should go in the previous code.

How could you make the code from both examples with only two “for” loops?

What is the voltage drop across the button when it is pressed and when it is released?

Page | 8 -1

Vous aimerez peut-être aussi