极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9144|回复: 3

求助--互动跑马灯程序(谢谢各位大侠!)

[复制链接]
发表于 2014-6-20 10:58:37 | 显示全部楼层 |阅读模式
项目背景:使LED灯带(5个)两端变亮,并向对方LED运动,直到互相碰到,之后返回两端。
参考:《Arduino 从基础到实践》运行问题:只有两端的灯一直亮,其他灯不亮。
自编程序如下:
byte ledPin[]={9,10,11,12,13};
int ledDelay(1000);
int direction=1;
int currentLED1=0;
int currentLED2=4;
unsigned long changeTime;
void setup() {
  for(int x=0;x<5;x++)
    {pinMode(ledPin[x],OUTPUT);}
changeTime=millis();
}

void loop() {
  if ((millis()-changeTime)>ledDelay)
      { changeLED();
        changeTime=millis();
      }
}
void changeLED()
{
    for(int x=0;x<5;x++)
       { digitalWrite(ledPin[x],LOW);//5只LED灯低平信号,熄灭
       }
       digitalWrite(ledPin[currentLED1],HIGH);
       digitalWrite(ledPin[currentLED2],HIGH);//设当前两端LED高平信号,点亮
    currentLED1+=direction;
    currentLED2-=direction; //当前值增加为direction表示的值
    if(currentLED1==currentLED2 )
  {while(currentLED1>0))//设第三个LED为基准点,往两边亮
    { digitalWrite(ledPin[currentLED1],HIGH);
      digitalWrite(ledPin[currentLED2],HIGH);
      for(int x=0;x<5;x++)
       { digitalWrite(ledPin[x],LOW);//5只LED灯低平信号,熄灭
       }
    currentLED1-=direction;
    currentLED2+=direction;
  digitalWrite(ledPin[currentLED1],HIGH);
  digitalWrite(ledPin[currentLED2],HIGH);
    }
  }
  
}
回复

使用道具 举报

发表于 2014-6-20 11:13:59 | 显示全部楼层
看到最后一点,有点看不下去了。呵呵 脑袋已经退化了。。诶

把 digitalwrite改成 print,看看数据变化的情况。。。

好像是你闪烁的太快了,眼镜看不到变化,所以感觉只有两头的灯常亮。
回复 支持 反对

使用道具 举报

发表于 2014-6-20 11:32:26 | 显示全部楼层
{while(currentLED1>0))//设第三个LED为基准点,往两边亮
这里多了个')'   ,编译通不过

int ledDelay(1000);     
这里定义了一个空函数么? 有什么意义么?应该是赋值吧? 改成int ledDelay = 1000; 试试看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-24 09:28:45 | 显示全部楼层
本帖最后由 slightwind 于 2014-6-24 09:32 编辑

谢谢各位好心人帮忙,我的最终程序如下(共同探讨下):
byte ledPin[]={9,10,11,12,13};
int direction=1;
int currentLEDone=0;
int currentLEDtwo=4;
void setup() {
  for(int x=0;x<5;x++)
    {pinMode(ledPin[x],OUTPUT);}
}

void loop() {
     while(currentLEDone<4)
    { for(int x=0;x<5;x++)
       { digitalWrite(ledPin[x],LOW);//5只LED灯低平信号,熄灭
       }
       delay(1000);
       digitalWrite(ledPin[currentLEDone],HIGH);
       digitalWrite(ledPin[currentLEDtwo],HIGH);
       delay(1000);
    currentLEDone+=direction;
    currentLEDtwo-=direction;}
        while(currentLEDone>0)
    {for(int x=0;x<5;x++)
       { digitalWrite(ledPin[x],LOW);//5只LED灯低平信号,熄灭
       }
       delay(1000);
       digitalWrite(ledPin[currentLEDone],HIGH);
       digitalWrite(ledPin[currentLEDtwo],HIGH);
       delay(1000);
       currentLEDone-=direction;
    currentLEDtwo+=direction;
  }     
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 18:37 , Processed in 0.091806 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表