第5步 编程

我给LED灯从外向内进行编号,其中1,3,5,7,9,11 有公共的红、绿、蓝总线(即所有的红线接在一起形成A总线),2,4,6,8,10,12有共同的绿、红、蓝总线(B总线)。

然后把LED的共阳极线和总线按照下面的方式连接到 arduino

LED1阳极= 0;
LED2阳极= 1;
LED3阳极= 2;
LED4阳极= 4;
LED5阳极= 7;
LED6阳极= 8;
LED7阳极= 12;
LED8阳极= 13;
LED9阳极= 14;
LED10阳极= 15;
LED11阳极= 16;
LED12阳极= 17;
红色总线 A = 3;
绿色总线 A = 5;
蓝色总线 A = 6;
红色总线 B = 9;
绿色总线B = 10;
蓝色总线B = 11;

int value = 0;  
  int LED1 = 0; 
  int LED2 = 1; 
  int LED3 = 2; 
  int LED4 = 4; 
  int LED5 = 7; 
  int LED6 = 8; 
  int LED7 = 12; 
  int LED8 = 13; 
  int LED9 = 14; 
  int LED10 = 15; 
  int LED11 = 16; 
  int LED12 = 17; 
  int RED1 = 3; 
  int GREEN1 = 5; 
  int BLUE1 = 6; 
  int RED2 = 9; 
  int GREEN2 = 10; 
  int BLUE2 = 11; 
          
void setup()  
{  
  pinMode(LED1,OUTPUT); 
  pinMode(LED2,OUTPUT); 
pinMode(LED3,OUTPUT); 
pinMode(LED4,OUTPUT); 
pinMode(LED5,OUTPUT); 
pinMode(LED6,OUTPUT); 
pinMode(LED7,OUTPUT); 
pinMode(LED8,OUTPUT); 
pinMode(LED9,OUTPUT); 
pinMode(LED10,OUTPUT); 
pinMode(LED11,OUTPUT); 
pinMode(LED12,OUTPUT); 
          
digitalWrite(LED1,LOW); 
digitalWrite(LED2,LOW); 
digitalWrite(LED3,LOW); 
digitalWrite(LED4,LOW); 
digitalWrite(LED5,LOW); 
digitalWrite(LED6,LOW); 
digitalWrite(LED7,LOW); 
digitalWrite(LED8,LOW); 
digitalWrite(LED9,LOW); 
digitalWrite(LED10,LOW); 
digitalWrite(LED11,LOW); 
digitalWrite(LED12,LOW); 
          
Red1Off(); 
Blue1Off(); 
Green1Off(); 
Red2Off(); 
Blue2Off(); 
Green2Off(); 
} 
          
void loop() 
{ 
          
LEDOn(LED1); 
LEDOn(LED2); 
LEDOn(LED3); 
LEDOn(LED4); 
LEDOn(LED5); 
LEDOn(LED6); 
LEDOn(LED7); 
LEDOn(LED8); 
LEDOn(LED9); 
LEDOn(LED10); 
LEDOn(LED11); 
LEDOn(LED12); 
          
FadeUp(RED1,RED2,2); 
delay(2000); 
FadeDown(RED1,RED2,2); 
FadeUp(GREEN1,GREEN2,2); 
delay(2000); 
FadeDown(GREEN1,GREEN2,2); 
FadeUp(BLUE1,BLUE2,2); 
delay(2000); 
FadeDown(BLUE1,BLUE2,2); 
          
Magenta1On (); 
Magenta2On (); 
delay (2000); 
Magenta1Off(); 
Magenta2Off(); 
Cyan1On (); 
Cyan2On(); 
delay (2000); 
Cyan1Off (); 
Cyan2Off(); 
Yellow1On (); 
Yellow2On(); 
delay (2000); 
Yellow1Off; 
Yellow2Off(); 
          
          
Blue1On(); 
Cyan2On(); 
delay (2000); 
Blue1Off(); 
Cyan2Off(); 
Green2On(); 
Yellow1On(); 
delay (2000); 
Green2Off(); 
Yellow1Off(); 
Red1On(); 
Magenta2On(); 
delay (2000); 
Red1Off(); 
Magenta2Off(); 
White1On(); 
White2On(); 
delay(2000); 
White1Off(); 
White2Off(); 
          
          
Blue1On(); 
Cyan2On(); 
delay (2000); 
Blue1Off(); 
Cyan2Off(); 
Green2On(); 
Yellow1On(); 
delay (2000); 
Green2Off(); 
Yellow1Off(); 
Red1On(); 
Magenta2On(); 
delay (2000); 
Red1Off(); 
Magenta2Off(); 
Red1On(); 
White2On(); 
delay(2000); 
Red1Off(); 
White2Off(); 
          
          
          
LEDOff(LED1); 
LEDOff(LED2); 
LEDOff(LED3); 
LEDOff(LED4); 
LEDOff(LED5); 
LEDOff(LED6); 
LEDOff(LED7); 
LEDOff(LED8); 
LEDOff(LED9); 
LEDOff(LED10); 
LEDOff(LED11); 
LEDOff(LED12); 
Red1Off(); 
Blue1Off(); 
Green1Off(); 
Red2Off(); 
Blue2Off(); 
Green2Off(); 
White1Off(); 
White2Off(); 
Red1Off(); 
Green1Off(); 
Blue1Off(); 
Red2Off(); 
Green2Off(); 
Blue2Off(); 
          
          
Red1On(); 
Red2On(); 
LEDSp(125); 
Red1Off(); 
Red2Off(); 
Blue1On(); 
Blue2On(); 
LEDSp(125); 
Blue1Off(); 
Blue2Off(); 
Green1On(); 
Green2On(); 
LEDSp(125); 
Green1Off(); 
Green2Off(); 
Magenta1On (); 
Magenta2On (); 
LEDSp(125); 
Magenta1Off(); 
Magenta2Off(); 
Cyan1On (); 
Cyan2On(); 
LEDSp(125); 
Cyan1Off (); 
Cyan2Off(); 
Yellow1On (); 
Yellow2On(); 
LEDSp(125); 
Yellow1Off; 
Yellow2Off(); 
White1On(); 
White2On(); 
LEDSp(125); 
White1Off(); 
White2Off(); 
Blue1On(); 
Cyan2On(); 
LEDSp(125); 
Blue1Off(); 
Cyan2Off(); 
Green2On(); 
Yellow1On(); 
LEDSp(125); 
Green2Off(); 
Yellow1Off(); 
Red1On(); 
Magenta2On(); 
LEDSp(125); 
Red1Off(); 
Magenta2Off(); 
Red1On(); 
White2On(); 
LEDSp(125); 
Red1Off(); 
White2Off(); 
Red1Off(); 
Green1Off(); 
Blue1Off(); 
Red2Off(); 
Green2Off(); 
Blue2Off(); 
Green1On(); 
Blue2On(); 
LEDSp2 (200); 
Green1Off(); 
Blue2Off(); 
Green1On(); 
Blue2On(); 
LEDSp2 (200); 
Green1Off(); 
Blue2Off(); 
          
          
          
          
          
          
          
} 
void LEDSp (int x) { // spirals on from 1-n 
LEDOn(LED1); 
delay(x); 
LEDOn(LED2); 
delay(x); 
LEDOn(LED3); 
delay(x); 
LEDOn(LED4); 
delay(x); 
LEDOn(LED5); 
delay(x); 
LEDOn(LED6);\ 
delay(x); 
LEDOn(LED7); 
delay(x); 
LEDOn(LED8); 
delay(x); 
LEDOn(LED9); 
delay(x); 
LEDOn(LED10); 
delay(x); 
LEDOn(LED11); 
delay(x); 
LEDOn(LED12); 
          
delay(x); 
LEDOff(LED1); 
delay(x); 
LEDOff(LED2); 
delay(x); 
LEDOff(LED3); 
delay(x); 
LEDOff(LED4); 
delay(x); 
LEDOff(LED5); 
delay(x); 
LEDOff(LED6); 
delay(x); 
LEDOff(LED7); 
delay(x); 
LEDOff(LED8); 
delay(x); 
LEDOff(LED9); 
delay(x); 
LEDOff(LED10); 
delay(x); 
LEDOff(LED11); 
delay(x); 
LEDOff(LED12); 
} 
          
void LEDSp2 (int x) { // spirals on from 1-n 
LEDOn(LED1); 
delay(x); 
LEDOff(LED1); 
LEDOn(LED2); 
delay(x); 
LEDOff(LED2); 
LEDOn(LED3); 
delay(x); 
LEDOff(LED3); 
          
LEDOn(LED4); 
delay(x); 
LEDOff(LED4); 
LEDOn(LED5); 
delay(x); 
LEDOff(LED5); 
LEDOn(LED6); 
delay(x); 
LEDOff(LED6); 
LEDOn(LED7); 
delay(x); 
LEDOff(LED7); 
LEDOn(LED8); 
delay(x); 
LEDOff(LED8); 
LEDOn(LED9); 
delay(x); 
LEDOff(LED9); 
LEDOn(LED10); 
delay(x); 
LEDOff(LED10); 
LEDOn(LED11); 
delay(x); 
LEDOff(LED11); 
LEDOn(LED12); 
delay(x); 
LEDOff(LED12); 
          
} 
          
void LEDOn(int x) { 
digitalWrite(x,HIGH); 
} 
void LEDOff(int x) { 
digitalWrite(x,LOW); 
} 
void Red1On() { 
analogWrite(RED1,100); 
} 
void Red1Off() { 
analogWrite(RED1,255); 
} 
void Green1On() { 
analogWrite(GREEN1,100); 
} 
void Green1Off() { 
analogWrite(GREEN1,255); 
} 
void Blue1On() { 
analogWrite(BLUE1,100); 
} 
void Blue1Off() { 
analogWrite(BLUE1,255); 
} 
void Red2On() { 
analogWrite(RED2,100); 
} 
void Red2Off() { 
analogWrite(RED2,255); 
} 
void Green2On() { 
analogWrite(GREEN2,100); 
} 
void Green2Off() { 
analogWrite(GREEN2,255); 
} 
void Blue2On() { 
analogWrite(BLUE2,100); 
} 
void Blue2Off() { 
analogWrite(BLUE2,255); 
} 
void Yellow1On() { 
analogWrite (RED1,100) ; 
analogWrite (GREEN1,100); 
} 
void Yellow1Off() { 
analogWrite (RED1,255); 
analogWrite (GREEN1,255); 
} 
void Cyan1On() { 
analogWrite (GREEN1,100); 
analogWrite (BLUE1,100); 
} 
void Cyan1Off () { 
analogWrite (GREEN1,255); 
analogWrite (BLUE1,255); 
} 
void Magenta1On () { 
analogWrite (RED1,100); 
analogWrite (BLUE1,100); 
} 
void Magenta1Off () { 
analogWrite (RED1,255); 
analogWrite (BLUE1,255); 
} 
void White1On () { 
analogWrite (RED1,100); 
analogWrite (BLUE1,100); 
analogWrite (GREEN1,100); 
} 
void White1Off () { 
analogWrite (RED1,225); 
analogWrite (BLUE1,225); 
analogWrite (GREEN1,225); 
} 
          
void Yellow2On() { 
analogWrite (RED2,100) ; 
analogWrite (GREEN2,100); 
} 
void Yellow2Off() { 
analogWrite (RED2,255); 
analogWrite (GREEN2,255); 
} 
void Cyan2On() { 
analogWrite (GREEN2,100); 
analogWrite (BLUE2,100); 
} 
void Cyan2Off () { 
analogWrite (GREEN2,255); 
analogWrite (BLUE2,255); 
} 
void Magenta2On () { 
analogWrite (RED2,100); 
analogWrite (BLUE2,100); 
} 
void Magenta2Off () { 
analogWrite (RED2,255); 
analogWrite (BLUE2,255); 
} 
void White2On () { 
analogWrite (RED2,100); 
analogWrite (BLUE2,100); 
analogWrite (GREEN2,100); 
} 
void White2Off () { 
analogWrite (RED2,225); 
analogWrite (BLUE2,225); 
analogWrite (GREEN2,225); 
} 
          
void FadeUp(int colour,int colour2 ,int speed) { 
for(value = 255 ; value >= 80;) // fade in (from min to max) 
{ 
          
analogWrite(colour, value); // sets the value (range from 0 to 255) 
analogWrite(colour2, value); 
delay(20); // waits for 30 milli seconds to see the dimming effect 
value = value - speed; 
} 
          
} 
          
void FadeDown(int colour,int colour2, int speed) { 
for(value = 80; value <= 255;) // fade out (from max to min) 
{ 
          
analogWrite(colour, value); 
analogWrite(colour2, value); 
delay(20); 
value = value + speed; 
} 
}

下一步上一步

评 论

e

选择昵称后请轻按空格键

提 交

请勿进行人身攻击,谩骂以及任何违法国家相关法律法规的言论。

信息

49844

浏览
5
oydean

作者:oydean

分享:42

高骚不止. >>
112

关键词: arduino led

猜你会喜欢

简单的光线探测器

光线探测器是最普遍的传感器之一,并广泛应...

无线供电的LED旋转时钟

这次的制作结合了无线供电的原理和时钟功能...

发光圣诞树

圣诞节就要来了,你还没有圣诞树么?现在就...

高手打造的智能无线供电台灯

无线供电是一个很吸引人的制作课题,许多电...

iPhone控制的简易物联网

这次我尝试制作一个简单的物联网系统,可以...