接线图(Arduino UNO控制板、Uno R3 v5扩展版和5050 RGB 全彩LED流水灯模块)
代码
int Pin[] = {4, 5, 6, 7, 8, 9, 10, 11};
int RGB[] = {A0, A1, A2};
//效果一:八个灯依次点亮并熄灭,走完一遍颜色变换,三种颜色依次更替
void led1(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], LOW); // 低电平灯亮
delay(T); // wait for a second
digitalWrite(Pin[i], HIGH); // 高电平灯灭
delay(T);
}
}
void sy1(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW); // 低电平灯亮
led1(200); // wait for a second
digitalWrite(RGB[i], HIGH); // 高电平灯灭
}
}
//效果二:八只灯依次亮起再依次熄灭,三种颜色依次更替
void led2(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], LOW); // 低电平灯亮
delay(T); // wait for a second
}
}
void led3(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], HIGH); // 低电平灯亮
delay(T); // wait for a second
}
}
void sy2(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW); // 低电平灯亮
led2(200);
led3(200);
digitalWrite(RGB[i], HIGH); // 高电平灯灭
}
}
//效果三:初始八个灯依次亮起不灭,接着八个灯一起变更颜色
void led4(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], LOW);
delay(T);
}
}
void sy3(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW);
led4(200);
digitalWrite(RGB[i], HIGH);
}
}
//效果四:八个灯依次点亮,三种颜色依次更替
void led5(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], LOW);
delay(T);
}
}
void led6(int T) {
for (int i = 8; i > 0; i--) {
digitalWrite(Pin[i], HIGH);
}
}
void sy4(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW);
led5(200);
led6(200);
digitalWrite(RGB[i], HIGH);
}
}
//效果五:前四个灯一起亮起再熄灭,接着后四个灯亮起熄灭,以此往复三种颜色依次更替
void led7(int T) {
for (int i = 0; i < 4; i++) {
digitalWrite(Pin[i], LOW);
}
}
void led8(int T) {
for (int i = 0; i < 4; i++) {
digitalWrite(Pin[i], HIGH);
}
}
void led9(int T) {
for (int i = 4; i < 8; i++) {
digitalWrite(Pin[i], LOW);
}
}
void led10(int T) {
for (int i = 4; i < 8; i++) {
digitalWrite(Pin[i], HIGH);
}
}
void sy5(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW);
led7(200);
delay(200);
led8(200);
delay(200);
led9(200);
delay(200);
led10(200);
delay(200);
digitalWrite(RGB[i], HIGH);
}
}
//效果六:逐个灯更换颜色
void led11(int T) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 3; j++) {
digitalWrite(RGB[j], LOW);
digitalWrite(Pin[i], LOW);
delay(T);
digitalWrite(Pin[i], HIGH);
delay(T);
digitalWrite(RGB[j], HIGH);
}
}
}
void sy6(int T) {
for (int i = 0; i < 3; i++) {
digitalWrite(RGB[i], LOW);
led11(T);
digitalWrite(RGB[i], HIGH);
}
}
//效果七:八个灯在更替颜色的同时依次逐个点亮并逐个熄灭
void led12(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], LOW);
sy7(200);
}
}
void led13(int T) {
for (int i = 0; i < 8; i++) {
digitalWrite(Pin[i], HIGH);
sy7(200);
}
}
void sy7(int T){
for (int j = 0; j < 3; j++) {
digitalWrite(RGB[j], LOW);
delay(T);
digitalWrite(RGB[j], HIGH);
}
}
//效果八:第1、3、5、7号灯每一种颜色依次亮,第2、4、6、8号灯每一种颜色依次亮
void led14(int T)
{
for (int j = 0; j < 8; j++)
{
if (j % 2 == 0) {
digitalWrite( Pin[j], LOW);
delay(T);
digitalWrite( Pin[j], HIGH);
delay(T);
}
else {
digitalWrite( Pin[j], HIGH);
}
}
}
void sy8(int T) {
for (int i = 0; i < 3; i++)
{
digitalWrite(RGB[i], LOW);
led14(T);
digitalWrite(RGB[i], HIGH);
}
}
void led15(int T)
{
for (int j = 0; j < 8; j++)
{
if (j % 2 == 1) {
digitalWrite( Pin[j], LOW);
delay(T);
digitalWrite( Pin[j], HIGH);
delay(T);
}
else {
digitalWrite( Pin[j], HIGH);
}
}
}
void sy9(int T) {
for (int i = 0; i < 3; i++)
{
digitalWrite(RGB[i], LOW);
led15(T);
digitalWrite(RGB[i], HIGH);
}
}
//效果九:第1、3、5、7号灯一起亮起,三种颜色更替变换
void led16(int T)
{
for (int j = 0; j < 8; j++)
{
if (j % 2 == 0) {
digitalWrite( Pin[j], LOW);
}
}
}
void sy10(int T) {
for (int i = 0; i < 3; i++)
{
digitalWrite(RGB[i], LOW);
led16(T);
delay(T);
digitalWrite(RGB[i], HIGH);
}
}
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(Pin[i], OUTPUT);
digitalWrite(Pin[i], HIGH);//初始化,灯全灭
}
for (int i = 0; i < 3; i++) {
pinMode(RGB[i], OUTPUT);
digitalWrite(RGB[i], HIGH);//初始化,灯全灭
}
}
void loop() {
sy1(200);
delay(400);
sy2(200);
delay(400);
sy3(200);
delay(400);
sy4(200);
delay(400);
sy5(200);
delay(400);
led11(200);
delay(400);
led12(200);
led13(200);
delay(400);
sy8(200);
sy9(200);
delay(400);
sy10(200);
delay(400);
}