I decide to do this project to try algorithms with leds.
Required Components: 1. Breadboard; 2. Five Green LEDs and five yellow LEDs; 3. Arduino UNO; 4. Wires; 5. Five resistors of 1K ohm; Laptop; 6. Patient (a lot).
Setting Up Arduino IDE
Nothing special in this step.
Wiring
The idea is use first short leg’s led to join ground wiring, and put each other leg’s led with a resistor an Arduino pin.
Arduino sketch:
This code was used to turn on and off the leds.
/* Santa Claus Leds Tree
by
Lucio Agostinho Rocha
Last update: 21/01/2017
*/
//Com PWM: 3, 5, 6, 9, 10, 11
//Sem PWM: 2, 4, 7, 8, 12 e 13
int numLeds=10;
int led1=13;
int led2=12;
int led3=11;
int led4=10;
int led5=2;
int led6=3;
int led7=4;
int led8=5;
int led9=6;
int led10=7;
int pino[10] = {led1,led2,led3,led4,led5,led6,led7,led8,led9,led10};
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
// put your setup code here, to run once:
for(int i=0; i<numLeds; i++)
pinMode(pino[i],OUTPUT);
}
void loop() {
int tempo=100;
/*
//1 de cada vez e volta para 1 (fila circular)
for(int i=0;i<3;i++){
int tempo=100;
for(int j=0; j<numLeds;j++)
funcao1(pino[j], tempo);
}
*/
//1 de cada vez e volta do último
for(int i=0;i<3;i++){
for(int j=0; j<numLeds;j++)
funcao1(pino[j], tempo);
for (int j=numLeds-2;j>0;j--)
funcao1(pino[j], tempo);
}
//apenas verdes
for(int i=0;i<3;i++){
for(int j=0; j<numLeds;j++){
j++;
funcao1(pino[j], tempo);
}
}
//apenas amarelos
for(int i=0;i<3;i++){
for(int j=0; j<numLeds;j++){
funcao1(pino[j], tempo);
j++;
}
}
//Vai e volta fileira 1 (horizontal)
for(int i=0;i<3;i++){
for(int j=0; j<numLeds/2;j++)
funcao1(pino[j], tempo);
for(int j=numLeds/2-2; j>0;j--)
funcao1(pino[j], tempo);
}
//Vai e volta fileira 2 (horizontal)
for(int i=0;i<3;i++){
for(int j=numLeds/2; j<numLeds;j++)
funcao1(pino[j], tempo);
for(int j=numLeds-2; j>numLeds/2;j--)
funcao1(pino[j], tempo);
}
//Vai e volta fileira 1 (vertical)
for(int i=0;i<3;i++){
//Para frente
for(int j=0; j<numLeds/2;j++){
funcao1(pino[j], tempo);
funcao1(pino[j+(numLeds/2)], tempo);
}
//Para trás
for(int j=numLeds-1; j>=numLeds/2;j--){
funcao1(pino[j], tempo);
funcao1(pino[j-(numLeds/2)], tempo);
}
}
//Pisca apenas nos pinos com PWM
/* funcao2(pino[2]);
funcao2(pino[3]);
funcao2(pino[5]);
funcao2(pino[7]);
funcao2(pino[8]);
*/
//Fade apenas nos pinos com PWM
/*funcao3(pino[2],50);
funcao3(pino[3],50);
funcao3(pino[5],50);
funcao3(pino[7],50);
funcao3(pino[8],50);
*/
/*
vagaLume(pino[2]);
vagaLume(pino[3]);
vagaLume(pino[5]);
vagaLume(pino[7]);
vagaLume(pino[8]);
*/
}
void funcao1(int pino, int tempo){
digitalWrite(pino,HIGH);
delay(tempo);
digitalWrite(pino,LOW);
delay(tempo);
}
//Pisca
void funcao2(int pino){
for(int i=0;i<20;i++){
analogWrite(pino, 100);
delay(50);
analogWrite(pino, 0);
delay(50);
}
}
void funcao3(int pino, int tempo){
// set the brightness of pin 9:
analogWrite(pino, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(tempo);
}
void vagaLume(int pino){
if (brightness==0){
analogWrite(pino, brightness);
delay(3000);
for(int i=0;i<20;i++){
analogWrite(pino, 100);
delay(50);
analogWrite(pino, 0);
delay(50);
}//fim for
delay(3000);
}
// set the brightness of pin 9:
analogWrite(pino, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
void funcao0(int pino){
digitalWrite(pino,LOW);
}
Final remarks
This sketch was very funny for me because I can set many parameters like time, sequencing, speed, and fading. However, PWM for fading not worked well with many LEDs, and you can try yourself in commented ‘vagalume’ part. Thanks. Please, give me suggestions about this text.
Comments