Analog Write with 12 LEDs on an Arduino
This example fades 12 LEDs up and the down, one by one, on an Arduino board.Hardware Required
- Arduino Mega Board
- (12) LEDs
- (12) 220 ohm resistors
- hook up wire
- breadboard
Circuit
Connect the longer, positive legs of (anodes) 12 LEDs to digital pins 2-13 through 220 ohm current limiting resistors. Connect the shorter, negative legs (cathodes) to ground.
Schematic
Code
In thesetup()
function of the code below, a for()
loop is used to assign digital pins 2-13 of the Mega as outputs.
Next, in the
loop()
function of the program below, a trio of nested for()
loops are used.
The first of these loops,
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
analogWrite(thisPin, brightness);
delay(2);
}
for()
loop kicks in, and the program moves on to the next LED pin, repeating all the steps mentioned above.
/*
Mega analogWrite() test
This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
This sketch was written for the Arduino Mega, and will not work on previous boards.
The circuit:
* LEDs attached from pins 2 through 13 to ground.
created 8 Feb 2009
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int lowestPin = 2;
const int highestPin = 13;
void setup() {
// set pins 2 through 13 as outputs:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// iterate over the pins:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
// fade the LED on thisPin from off to brightest:
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
// fade the LED on thisPin from brithstest to off:
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
// pause between LEDs:
delay(100);
}
}
Mega analogWrite() test
This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
This sketch was written for the Arduino Mega, and will not work on previous boards.
The circuit:
* LEDs attached from pins 2 through 13 to ground.
created 8 Feb 2009
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int lowestPin = 2;
const int highestPin = 13;
void setup() {
// set pins 2 through 13 as outputs:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// iterate over the pins:
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) {
// fade the LED on thisPin from off to brightest:
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
// fade the LED on thisPin from brithstest to off:
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
// pause between LEDs:
delay(100);
}
}
ini hasil percobaan yang telah dilakukan
2 komentar::
artikel bagus gan, kunjungan baliknya di MEMBUAT LAMPU LED BERKEDIP DENGAN ARDUINO ARGA YUDHA
atau langsung ke website
http://www.argayudhaadhiprama.com/2016/03/24/membuat-lampu-led-berkedip-arduino-uno-r3/
artikelnya bagus gan, kunjungi juga website saya di " MEMBUAT LAMPU LED BERKEDIP DENGAN ARDUINO UNO R3 "
atau link.nya di http://www.argayudhaadhiprama.com/membuat-lampu-led-berkedip-arduino-uno-r3/
trimakasih dari argayudhaadhiprama.com
Posting Komentar
silakan komentar: