You are on page 1of 205

ARDUINO

PROGRAMMERING
DIGITALE/ANALOGE INNDATA/UTDATA
LYSDIODER FRITZING
Roger Antonsen
INF1510
30. januar 2012
FOR EKSPERIMENTELL INFORMATIKK FOR EKSPERIMENTELL INFORMATIKK
PEN SONE PEN SONE
STUDIELABEN
Arduino Cookbook, Michael Margolis
Making Things Talk, Tom Igoe
Flere av illustrasjonene og eksemplene i denne forelesningen er tatt derfra.
En mer avansert, men veldig bra, bok!
Denne boken inneholder det meste man trenger vite.
PROGRAMMERING I
ARDUINO
pinMode(pin, Mode) pin angir pinnen og Mode er INPUT eller OUTPUT
pinMode(pin, Mode) pin angir pinnen og Mode er INPUT eller OUTPUT
digitalWrite(pin, verdi) verdi er HIGH eller LOW
pinMode(pin, Mode) pin angir pinnen og Mode er INPUT eller OUTPUT
digitalWrite(pin, verdi) verdi er HIGH eller LOW
digitalRead(pin) returnerer HIGH eller LOW
pinMode(pin, Mode) pin angir pinnen og Mode er INPUT eller OUTPUT
digitalWrite(pin, verdi) verdi er HIGH eller LOW
digitalRead(pin) returnerer HIGH eller LOW
analogWrite(pin, verdi) verdi er tall mellom 0 og 255
pinMode(pin, Mode) pin angir pinnen og Mode er INPUT eller OUTPUT
digitalWrite(pin, verdi) verdi er HIGH eller LOW
digitalRead(pin) returnerer HIGH eller LOW
analogWrite(pin, verdi) verdi er tall mellom 0 og 255
analogRead(pin) returnerer et tall mellom 0 og 1023

Variable brukes til lagre data.

Variable brukes til lagre data.

Noen viktige begreper er flgende.

Variable brukes til lagre data.

Noen viktige begreper er flgende.

deklarasjon: sette av plass i minnet


int x;
boolean y;

Variable brukes til lagre data.

Noen viktige begreper er flgende.

deklarasjon: sette av plass i minnet


int x;
boolean y;

tilordning: sette en verdi


x = 20;
y = true;

Variable brukes til lagre data.

Noen viktige begreper er flgende.

deklarasjon: sette av plass i minnet


int x;
boolean y;

tilordning: sette en verdi


x = 20;
y = true;

skop: der hvor variablene er synlige


int: 16-bits verdi mellom -32 768 og 32 767
int x = 32767;
x++; // gjr at x blir -32768
int: 16-bits verdi mellom -32 768 og 32 767
int x = 32767;
x++; // gjr at x blir -32768
unsigned int: 16-bits verdi mellom 0 og 65535
kun positive verdier
int: 16-bits verdi mellom -32 768 og 32 767
int x = 32767;
x++; // gjr at x blir -32768
unsigned int: 16-bits verdi mellom 0 og 65535
kun positive verdier
long: 32-bits verdi mellom -2147483648 og 2147483647
int: 16-bits verdi mellom -32 768 og 32 767
int x = 32767;
x++; // gjr at x blir -32768
unsigned int: 16-bits verdi mellom 0 og 65535
kun positive verdier
long: 32-bits verdi mellom -2147483648 og 2147483647
usigned long: 32-bits verdi mellom 0 og 4294967295
int: 16-bits verdi mellom -32 768 og 32 767
int x = 32767;
x++; // gjr at x blir -32768
unsigned int: 16-bits verdi mellom 0 og 65535
kun positive verdier
long: 32-bits verdi mellom -2147483648 og 2147483647
usigned long: 32-bits verdi mellom 0 og 4294967295
oat/double: 32-bits yttall
avrundede verdier; husk at dette kan medfre nyaktigheter
byte: 8-bits verdi mellom 0 og 255
byte x = 130;
byte x = B10000010; // samme resultat
byte: 8-bits verdi mellom 0 og 255
byte x = 130;
byte x = B10000010; // samme resultat
char: en byte som lagrer et tegn
(egentlig bare en verdi mellom -128 og 127)
char x = G;
char x = 71; // samme resultat
byte: 8-bits verdi mellom 0 og 255
byte x = 130;
byte x = B10000010; // samme resultat
char: en byte som lagrer et tegn
(egentlig bare en verdi mellom -128 og 127)
char x = G;
char x = 71; // samme resultat
boolean: true eller false
byte: 8-bits verdi mellom 0 og 255
byte x = 130;
byte x = B10000010; // samme resultat
char: en byte som lagrer et tegn
(egentlig bare en verdi mellom -128 og 127)
char x = G;
char x = 71; // samme resultat
boolean: true eller false
array: en samling av indekserte variable
array: en samling av indekserte variable
deklarasjon (og tilordning):
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
// siste tegn er et nulltegn som avslutter
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
// siste tegn er et nulltegn som avslutter
hente og sette inn verdier:
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
// siste tegn er et nulltegn som avslutter
hente og sette inn verdier:
mineVerdier[0] returnerer 32.
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
// siste tegn er et nulltegn som avslutter
hente og sette inn verdier:
mineVerdier[0] returnerer 32.
mineVerdier[5] gir feilmelding.
array: en samling av indekserte variable
deklarasjon (og tilordning):
int mittArray[6]; // kun deklarasjon
int mineTall[] = {3,7,1,2,8}; // automatisk
int mineVerdier[3] = {32,64,-8}; // bde-og
char beskjed[6] = "hello"; // strenger er arrayer av tegn (char)
// siste tegn er et nulltegn som avslutter
hente og sette inn verdier:
mineVerdier[0] returnerer 32.
mineVerdier[5] gir feilmelding.
mineVerdier[0] = 100; // erstatter frste tall
int multipliser(int x, int y) {
int multipliser(int x, int y) {
int resultat;
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
Viktig huske p:
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
Viktig huske p:

skop
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
Viktig huske p:

skop

semikolon
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
Viktig huske p:

skop

semikolon

klammeparenteser
int multipliser(int x, int y) {
int resultat;
resultat = x * y;
return resultat;
}
type til returverdi
(eller void hvis ingen type)
navn p funksjonen
type og navn p
argumentene til funksjonen
deklarasjon
av variabel
tilordning av
variabel
returverdi;
m vre av riktig type
Viktig huske p:

skop

semikolon

klammeparenteser

kommentarer
Aritmetikk:
Aritmetikk:
int y;
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
x <= y // x mindre enn eller lik y
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
x <= y // x mindre enn eller lik y
Logiske operatorer:
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
x <= y // x mindre enn eller lik y
Logiske operatorer:
x > 0 && x < 5 // OG - sann hvis begge er sanne
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
x <= y // x mindre enn eller lik y
Logiske operatorer:
x > 0 && x < 5 // OG - sann hvis begge er sanne
x > 0 || x < 5 // ELLER - sann hvis en er sann
Aritmetikk:
int y;
y = 9 / 4; // y blir satt til 2
y = y + 3; // ker verdien til y med 3
y++; // samme som y = y + 1;
y /= 2; // samme som y = y / 2;
Sammenlikninger:
x == y // x lik y
x != y // x ulik y
x <= y // x mindre enn eller lik y
Logiske operatorer:
x > 0 && x < 5 // OG - sann hvis begge er sanne
x > 0 || x < 5 // ELLER - sann hvis en er sann
!x < 0 // IKKE - sann hvis uttrykket er usant
if (x < 100)
{
// gjr A
}
if (x < 100)
{
// gjr A
}
if (x < 100)
{
// gjr A
}
else
{
// gjr B
}
switch (x) {
switch (x) {
case 1:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
// default er valgfritt
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
// default er valgfritt
}
switch (x) {
switch (x) {
case 1:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
// default er valgfritt
switch (x) {
case 1:
// hvis x er lik 1, gjr dette
break;
case 2:
// hvis x er lik 2, gjr dette
break;
default:
// hvis ingen matcher, gjr flgende
// default er valgfritt
}
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
deklarere
variabel
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
deklarere
variabel
sett
startverdi
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
deklarere
variabel
sett
startverdi
test
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
deklarere
variabel
sett
startverdi
test
endre
verdi
for (int x = 0; x < 100; x++) {
Serial.println(x);
}
// skriver tallene fra 0 til 99
deklarere
variabel
sett
startverdi
test
endre
verdi
kropp
DIGITALE INNDATA
Arduinofunksjonen for lese digital input heter digitalRead
og forteller om spenningen p en pinne er HIGH (5 volt) eller
LOW (0 volt).
Arduinofunksjonen for lese digital input heter digitalRead
og forteller om spenningen p en pinne er HIGH (5 volt) eller
LOW (0 volt).
Arduinofunksjonen for fortelle at en pinne skal brukes til
dette er pinMode(pin,INPUT).
Arduinofunksjonen for lese digital input heter digitalRead
og forteller om spenningen p en pinne er HIGH (5 volt) eller
LOW (0 volt).
Arduinofunksjonen for fortelle at en pinne skal brukes til
dette er pinMode(pin,INPUT).
I utgangspunktet er alle kongurert som inputpinner, men det
er god praksis sette dem allikevel.
Iigurc 5-1. Digita| and ana|og pins on a standard Arduino board
This chaptei coveis the Aiuuino pins that can sense digita| anu ana|og inputs. Digital
input pins sense the piesence anu aLsence ol voltage on a pin. Analog input pins meas-
uie a iange ol voltages on a pin.
The Aiuuino lunction to uetect uigital input is digitalRead anu it tells youi sketch il a
voltage on a pin is HIGH (5 volts) oi LOW (0 volts). The Aiuuino lunction to conliguie a
pin loi ieauing input is pinMode(pin, INPUT).
On a typical Loaiu, theie aie 1+ uigital pins (numLeieu 0 to 13) as shown at the top ol
Figuie 5-1. Pins 0 anu 1 (maikeu RX anu TX) aie useu loi the USB seiial connection
anu shoulu Le avoiueu loi othei uses. Il you neeu moie uigital pins on a stanuaiu Loaiu,
you can use the analog pins as uigital pins (analog pins 0 thiough 5 can Le useu as
uigital pins 1+ thiough 19).
Aiuuino 1.0 intiouuceu logical names loi many ol the pins. The constants in Ta-
Lle 5-1 can Le useu in all lunctions that expect a pin numLei.
Tab|c 5-1. Pin constants introduccd in Arduino 1.0
Constant Pin Number Constant Pin Number
A0 Analog input 0 (Digital 14) LED_BUILTIN On-board LED (Digital 13)
A1 Analog input 1 (Digital 15) SDA I2C Data (Digital 18)
A2 Analog input (Digital 16) SCL I2C Clock (Digital 19)
A3 Analog input (Digital 17) SS SPI Select (Digital 10)
A4 Analog input (Digital 18) MOSI SPI Input (Digital 11)
A5 Analog input (Digital 19) MISO SPI Output (Digital 12)
SCL SPI Clock (Digital 13)
148 | Chapter 5:Simple Digital and Analog Input
}
}
Iigurc 5-3. Switch conncctcd using pu||-down rcsistor
Stanuaiu Aiuuino Loaius have a Luilt-in LED connecteu to pin 13. Il
youi Loaiu uoes not, see Recipe 7.1 loi inloimation on connecting an
LED to an Aiuuino pin.
Discussion
The setup lunction conliguies the LED pin as OUTPUT anu the switch pin as INPUT.
A pin must Le set to OUTPUT moue loi digitalWrite to contiol the pin`s
output voltage. It must Le in INPUT moue to ieau the uigital input.
The digitalRead lunction monitois the voltage on the input pin (inputPin), anu it ie-
tuins a value ol HIGH il the voltage is high (5 volts) anu LOW il the voltage is low (0 volts).
Actually, any voltage that is gieatei than 2.5 volts (hall ol the voltage poweiing the
chip) is consiueieu HIGH anu less than this is tieateu as LOW. Il the pin is lelt unconnecteu
5.1 Using a Switch | 151
(known as j|oating), the value ietuineu liom digitalRead is inueteiminate (it may Le
HIGH oi LOW, anu it cannot Le ieliaLly useu). The iesistoi shown in Figuie 5-3 ensuies
that the voltage on the pin will Le low when the switch is not piesseu, Lecause the
iesistoi pulls uown the voltage to giounu. Vhen the switch is pusheu, a connection
is maue Letween the pin anu -5 volts, so the value on the pin inteipieteu Ly digital
Read changes liom LOW to HIGH.
Do not connect a uigital oi analog pin to a voltage highei than 5 volts
(oi 3.3 volts on a 3.3V Loaiu). This can uamage the pin anu possiLly
uestioy the entiie chip. Also, make suie you uon`t wiie the switch so
that it shoits the 5 volts to giounu (without a iesistoi). Although this
may not uamage the Aiuuino chip, it is not goou loi the powei supply.
In this example, the value liom digitalRead is stoieu in the vaiiaLle val. This will Le
HIGH il the Lutton is piesseu, LOW otheiwise.
The switch useu in this example (anu almost eveiywheie else in this
Look) makes electiical contact when piesseu anu Lieaks contact when
not piesseu. These switches aie calleu Noimally Open (NO); see this
Look`s weLsite loi pait numLeis. The othei kinu ol momentaiy switch
is calleu Noimally Closeu (NC).
The output pin connecteu to the LED is tuineu on when you set val to HIGH, illuminating
the LED.
Although Aiuuino sets all uigital pins as inputs Ly uelault, it is a goou piactice to set
this explicitly in youi sketch to ieminu youisell aLout the pins you aie using.
You may see similai coue that uses true insteau ol HIGH; these can Le useu inteichange-
aLly (they aie also sometimes iepiesenteu as 1). Likewise, false is the same as LOW anu
0. Use the loim that Lest expiesses the meaning ol the logic in youi application.
Almost any switch can Le useu, although the ones calleu noncntary tacti|c switchcs aie
populai Lecause they aie inexpensive anu can plug uiiectly into a LieauLoaiu. See the
weLsite loi this Look loi some suppliei pait numLeis.
Heie is anothei way to implement the logic in the pieceuing sketch:
void loop()
{
digitalWrite(ledPin, digitalRead(inputPin)); // turn LED ON if input pin is
// HIGH, else turn OFF
}
This uoesn`t stoie the Lutton state into a vaiiaLle. Insteau, it sets the LED on oi oll
uiiectly liom the value oLtaineu liom digitalRead. It is a hanuy shoitcut, Lut il you
linu it oveily teise, theie is no piactical uilleience in peiloimance, so pick whichevei
loim you linu easiei to unueistanu.
152 | Chapter 5:Simple Digital and Analog Input
The pull-up coue is similai to the pull-uown veision, Lut the logic is ieveiseu: the value
on the pin goes LOW when the Lutton is piesseu (see Figuie 5-+ loi a schematic uiagiam
ol this). It may help to think ol this as piessing the switch DOWN, causing the output to
go LOW:
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, LOW); // turn LED OFF
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
Iigurc 5-1. Switch conncctcd using pu||-up rcsistor
See Also
The Aiuuino ieleience loi digitalRead: http://arduino.cc/cn/Rcjcrcncc/Digita|Rcad
The Aiuuino ieleience loi digitalWrite: http://arduino.cc/cn/Rcjcrcncc/Digita|Writc
The Aiuuino ieleience loi pinMode: http://arduino.cc/cn/Rcjcrcncc/PinModc
The Aiuuino ieleiences loi constants (HIGH, LOW, etc.): http://arduino.cc/cn/Rcjcrcncc/
Constants
Aiuuino tutoiial on uigital pins: http://arduino.cc/cn/Tutoria|/Digita|Pins
5.1 Using a Switch | 153
The pull-up coue is similai to the pull-uown veision, Lut the logic is ieveiseu: the value
on the pin goes LOW when the Lutton is piesseu (see Figuie 5-+ loi a schematic uiagiam
ol this). It may help to think ol this as piessing the switch DOWN, causing the output to
go LOW:
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, LOW); // turn LED OFF
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
Iigurc 5-1. Switch conncctcd using pu||-up rcsistor
See Also
The Aiuuino ieleience loi digitalRead: http://arduino.cc/cn/Rcjcrcncc/Digita|Rcad
The Aiuuino ieleience loi digitalWrite: http://arduino.cc/cn/Rcjcrcncc/Digita|Writc
The Aiuuino ieleience loi pinMode: http://arduino.cc/cn/Rcjcrcncc/PinModc
The Aiuuino ieleiences loi constants (HIGH, LOW, etc.): http://arduino.cc/cn/Rcjcrcncc/
Constants
Aiuuino tutoiial on uigital pins: http://arduino.cc/cn/Tutoria|/Digita|Pins
5.1 Using a Switch | 153
PLITELIG SJEKK AV BRYTERE
PLITELIG SJEKK AV BRYTERE
Nr en bryter sls p hender det som regel at verdiene gr
mellom hy og lav mange ganger veldig fort.
PLITELIG SJEKK AV BRYTERE
Nr en bryter sls p hender det som regel at verdiene gr
mellom hy og lav mange ganger veldig fort.
Vi kan kompensere for dette i koden vr.
PLITELIG SJEKK AV BRYTERE
Nr en bryter sls p hender det som regel at verdiene gr
mellom hy og lav mange ganger veldig fort.
Vi kan kompensere for dette i koden vr.
Dette kalles p engelsk for debouncing.
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}

digitalWrite(ledPin, buttonState);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}

digitalWrite(ledPin, buttonState);
lastButtonState = reading;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}

digitalWrite(ledPin, buttonState);
lastButtonState = reading;
}
ANALOG INNDATA
ANALOG INPUT
ANALOG INPUT
Arduinofunksjonen for lese analog input heter analogRead
og forteller hvor hy spenningen er, fra 0 til 5 volt, ved gi et
tall mellom 0 og 1023.
ANALOG INPUT
Arduinofunksjonen for lese analog input heter analogRead
og forteller hvor hy spenningen er, fra 0 til 5 volt, ved gi et
tall mellom 0 og 1023.
(Det er alts 10-bits analog-til-digital konvertering som skjer.)
int sensorPin = A0; // select the input pin for the potentiometer
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
ENDRE REKKEVIDDEN
ENDRE REKKEVIDDEN
Hvis vi trenger verdier mellom 0 og 100 i stedet for mellom 0
og 1023 kan vi bruke funksjonen map.
ENDRE REKKEVIDDEN
Hvis vi trenger verdier mellom 0 og 100 i stedet for mellom 0
og 1023 kan vi bruke funksjonen map.
map(verdi, fraLav, fraHy, tilLav, tilHy)
ENDRE REKKEVIDDEN
Hvis vi trenger verdier mellom 0 og 100 i stedet for mellom 0
og 1023 kan vi bruke funksjonen map.
map(verdi, fraLav, fraHy, tilLav, tilHy)
F.eks. vil map(verdi, 0, 1023, 0, 255) gjre jobben.
DIGITALE UTDATA
DIGITAL UTDATA
DIGITAL UTDATA
Alle pinner kan ogs brukes som digitale ut-pinner.
DIGITAL UTDATA
Alle pinner kan ogs brukes som digitale ut-pinner.
Da m de settes med pinMode(pin,OUTPUT).
DIGITAL UTDATA
Alle pinner kan ogs brukes som digitale ut-pinner.
Da m de settes med pinMode(pin,OUTPUT).
Deretter kan vi bruke digitalWrite(pin,OUTPUT), hvor
OUTPUT er enten HIGH eller LOW.
ANALOGE UTDATA
ANALOGE UTSIGNALER
ANALOGE UTSIGNALER
Analog betyr her at vi skal kunne variere spenningen fra 0 til 5
volt.
ANALOGE UTSIGNALER
Analog betyr her at vi skal kunne variere spenningen fra 0 til 5
volt.
Her kan vi bruke funksjonen analogWrite(pin,OUTPUT),
hvor OUTPUT er et tall fra 0 til 255.
ANALOGE UTSIGNALER
Analog betyr her at vi skal kunne variere spenningen fra 0 til 5
volt.
Her kan vi bruke funksjonen analogWrite(pin,OUTPUT),
hvor OUTPUT er et tall fra 0 til 255.
Dette kan kun gjres p pinner 3, 5, 6, 9, 10 og 11.
ANALOGE UTSIGNALER
Analog betyr her at vi skal kunne variere spenningen fra 0 til 5
volt.
Her kan vi bruke funksjonen analogWrite(pin,OUTPUT),
hvor OUTPUT er et tall fra 0 til 255.
Dette kan kun gjres p pinner 3, 5, 6, 9, 10 og 11.
Metoden som ligger under kalles pulsbreddemodulasjon.
PULSBREDDEMODULASJON
PULSBREDDEMODULASJON
Pulse Width Modulation (PWM)
PULSBREDDEMODULASJON
Pulse Width Modulation (PWM)
Ved sl av og p strm veldig raskt, klarer Arduinokortet
simulere en spenning mellom 0 og 5 volt.
PULSBREDDEMODULASJON
Pulse Width Modulation (PWM)
Ved sl av og p strm veldig raskt, klarer Arduinokortet
simulere en spenning mellom 0 og 5 volt.
Frekvensen er ca. 500 Hz.
PVM woiks Ly vaiying the piopoition ol the pulses` on time to oll time, as shown in
Figuie 7-1. Low-level output is emulateu Ly piouucing pulses that aie on loi only a
shoit peiiou ol time. Highei level output is emulateu with pulses that aie on moie than
they aie oll. Vhen the pulses aie iepeateu guickly enough (almost 500 times pei seconu
on Aiuuino), the pulsing cannot Le uetecteu Ly human senses, anu the output liom
things such as LEDs looks like it is Leing smoothly vaiieu as the pulse iate is changeu.
Iigurc 7-1. PWM output jor various ana|ogWritc va|ucs
Aiuuino has a limiteu numLei ol pins that can Le useu loi analog output. On a stanuaiu
Loaiu, you can use pins 3, 5, 6, 9, 10, anu 11. On the Aiuuino Mega Loaiu, you can
use pins 2 thiough 13 loi analog output. Many ol the iecipes that lollow use pins that
can Le useu loi Loth uigital anu analog to minimize iewiiing il you want to tiy out
uilleient iecipes. Il you want to select uilleient pins loi analog output, iememLei to
choose one ol the suppoiteu analogWrite pins (othei pins will not give any output).
Controlling Light
Contiolling light using uigital oi analog output is a veisatile, ellective, anu wiuely useu
methou loi pioviuing usei inteiaction. Single LEDs, aiiays, anu numeiic uisplays aie
242 | Chapter 7:Visual Output
LITT OM LYSDIODER
LYSDIODER
LYSDIODER
En LED er en diode som kun slipper strm gjennom i n
retning.
LYSDIODER
En LED er en diode som kun slipper strm gjennom i n
retning.
Den har to ben, anode (vanligvis litt lenger) og katode, og nr
spenningen fra anode til katode er hy nok, s slippes strm
gjennom og den lyser.
LYSDIODER
En LED er en diode som kun slipper strm gjennom i n
retning.
Den har to ben, anode (vanligvis litt lenger) og katode, og nr
spenningen fra anode til katode er hy nok, s slippes strm
gjennom og den lyser.
Spenningen som skal til for at den lyser (forward voltage) er
vanligvis p 1,8 volt.
LYSDIODER + MOTSTANDER
LYSDIODER + MOTSTANDER
Strmmen gjennom en lysdiode m begrenses med en
motstand.
LYSDIODER + MOTSTANDER
Strmmen gjennom en lysdiode m begrenses med en
motstand.
Hver Arduinopinne kan gi opp til 40 mA.
LYSDIODER + MOTSTANDER
Strmmen gjennom en lysdiode m begrenses med en
motstand.
Hver Arduinopinne kan gi opp til 40 mA.
En lysdiode trenger typisk 20 mA for lyse.
The uata sheet ieleis to +0 mA as the aLsolute maximum iating anu
some engineeis may Le hesitant to opeiate anywheie neai this value.
Howevei, the +0 mA liguie is alieauy ue-iateu Ly Atmel anu they say
the pins can salely hanule this cuiient. Recipes that lollow ielei to the
+0 mA maximum iating; howevei, il you aie Luiluing anything wheie
ieliaLility is impoitant, ue-iating this to 30 mA to pioviue an auueu
comloit maigin is piuuent.
7.1 Connecting and Using LEDs
Problem
You want to contiol one oi moie LEDs anu select the coiiect cuiient-limiting iesistoi
so that you uo not uamage the LEDs.
Solution
Tuining an LED on anu oll is easy to uo with Aiuuino, anu some ol the iecipes in
pievious chapteis have incluueu this capaLility (see Recipe 5.1 loi an example that
contiols the Luilt-in LED on pin 13). The iecipe heie pioviues guiuance on choosing
anu using exteinal LEDs. Figuie 7-2 shows the wiiing loi thiee LEDs, Lut you can iun
this sketch with just one oi two.
Iigurc 7-2. Connccting cxtcrna| LEDs
The schematic symLol loi the cathoue (the negative pin) is |, not c. The
schematic symLol c is useu loi a capacitoi.
7.1 Connecting and Using LEDs | 245
FRITZING
FRITZING
FRITZING
Dette er programvare for dokumentere prosjekter og lage
design av kretskort.
FRITZING
Dette er programvare for dokumentere prosjekter og lage
design av kretskort.
Det forenkler prosessen fra prototype til endelig design.
FRITZING
Dette er programvare for dokumentere prosjekter og lage
design av kretskort.
Det forenkler prosessen fra prototype til endelig design.
Ogs pen kildekode og i samme familie som Processing og
Arduino.
FRITZING
Dette er programvare for dokumentere prosjekter og lage
design av kretskort.
Det forenkler prosessen fra prototype til endelig design.
Ogs pen kildekode og i samme familie som Processing og
Arduino.
Se http://fritzing.org/ for mer informasjon!
A
R
E
F
G
N
D
R
E
S
E
T
3
V
3
P
W
M
P
W
M
P
W
M
L
TX
RX
USB
EXT
P
W
R
S
E
L
PWR
ICSP
P
W
M
P
W
M
P
W
M
T
X
R
X
3
1
2
1
1
1
0
1
9 8
DIGITAL
7 6 5 4 3 2 1 0
1
5V Gnd
POWER
www.arduino.cc
ANALOG IN
9V 0 1 2 3 4 5
Arduino

You might also like