Docsity

Arduino e LEDs: Um Guia Prático, Notas de aula de Linguagem de Programação

Pré-visualização parcial do texto

Baixe Arduino e LEDs: Um Guia Prático e outras Notas de aula em PDF para Linguagem de Programação, somente na Docsity!

INTRODUCTION TO

ARDUINO AND LEDS

Activity one: Make sure everybody is up and running

Arduino

Arduino

USB Interface

External Power

ATmega

Analog Inputs

Digital I/O pins

tx/rx LEDs

Test LED on pin 13

power LED

Reset

Physical Computing Essentials

 pinMode(pinNumber, mode); // declare a pin INPUT or OUTPUT

 digitalRead(pinNumber); // read the HIGH/LOW status of pin

 digitalWrite(pinNumber, value); // force HIGH/LOW voltage

 analogWrite(pinNumber, value); // PWM for intermediate vals

 analogRead(pinNumber); // read analog pin through ADC

Arduino Programming

Load Examples - Basics - Blink

Arduino SW system

 Two required functions

 void setup(){…} // runs once at init time

 void loop(){…} // loops forever after init

Arduino Programming

Verify, Upload, New, Open, Save

Programming area

Notification area

Blink Example

int led = 13;

void setup() {

pinMode(led, OUTPUT);

void loop() {

digitalWrite(led, HIGH);

delay(1000);

digitalWrite(led, LOW);

delay(1000);

LEDs and Resistors

Anode + Cathode -

Current flows from Anode to Cathode

Lights up when current flows

long lead short lead

www.todbot.com

LEDs and Resistors

Anode + Cathode -

Current flows from Anode to Cathode Lights up when current flows

long lead short lead

Arduino

Ground

www.todbot.com

LEDs and Resistors

Anode + (^) Cathode -

Current flows from Anode to Cathode Lights up when current flows

long lead short lead

Arduino

Ground

www.todbot.com

Current Limiting Resistor

 Diodes have a “forward voltage” or “diode drop”

 Typically V f is around 0.7v for a diode,

and 1.5v to 3.0v for an LED

 Diodes also have a current

limit

 Typically 20mA for an LED

 If you don’t limit the current,

they’ll burn out

Solderless Breadboad

Duemilanove

Solderless Breadboard

Duemilanove

Wiring up an External LED

Wiring up an External LED

Multiple LED Example

Multiple LED Example

We just made LEDs blink…Big Deal?

 Most actuators are switched on and off with a

digital output

 The digitalWrite(pin,value); function is the software

command that lets you control almost anything

 LEDs are easy!

 Motors, servos, etc. are a little trickier, but not much

 More on that later…

 Arduino has 14 digital pins (inputs or outputs)

 can easily add more with external helper chips

 More on that later…

Moving on…

www.todbot.com

Fading Program

int ledPin = 9; // LED connected to digital pin 9

void setup() {

// nothing happens in setup (Why not?)

// Answer: Pins default to being OUTPUT. You must define any that you want to use

// as INPUT, but if you are using them as OUTPUT you can just assume the default.

// Typically, though, it’s good practice to define the OUTPUT pins anyway…

void loop() {

// fade in from min to max in increments of 5 points:

for (int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {

analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255):

delay(30); // wait for 30 milliseconds between brightness steps

// fade out from max to min in increments of 5 points:

for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {

analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255):

delay(30); // wait for 30 milliseconds between dimming steps

load Examples – Analog - Fading

Fading Example

End of Activity One

 There are some additional slides that you can look

at later

 There’s a summary at the end of the handout

Add a diffuser

Source vs. Sink for current

 Arduino pins: source/sink a max of 40mA/pin

 Also 250mA total for ALL pins

Sink means pull to ground LED is on when Pin 10 is LOW

Source means pull to Vdd LED is on when Pin 10 is HIGH

Random Brightness

 Write a program to make the LED flicker like a flame

 Choose a random intensity

 For a random amount of time

 Use analogWrite(ledPin, val) to change brightness

 Main loop repeats itself forever…

 Set the value of the brightness to a random value

 Wait for a random amount of time

 repeat

 The effect looks like flickering…

Flickering Pseudocode

1. Set the LED to a random brightness

2. Wait for a random amount of time

3. repeat

Flickering Pseudocode

1. Pick a random number between 100-

2. Set LED to that brightness (use analogWrite)

3. Pick another random number between 10-

4. Wait for that amount of time (in ms)

5. Repeat

int brightness;

brightness = random(100, 256);