int unsigned long time1 = 0;
int unsigned long time2 = 0;
int sw = 10;
int led_pin = 13;
int led[] = {2, 3, 4, 5, 6, 7, 8, 9};
int debounce_delay = 100;
int val;
int i;
int j = 7;//因為您設最左邊為開始
int direct = 0;
void setup()
{
pinMode(led_pin, OUTPUT);
pinMode(sw, INPUT);
digitalWrite(sw, HIGH);
for (int i = 0; i < 8; i++)
{
pinMode(led[i], OUTPUT);
}
}
void loop()
{
for (i = 0; i < 8; i++)//此行一定要加,每loop一次就全部led關閉
{//如果不加會出現76543210燈依續亮,加此for loop才會一次亮一個燈
digitalWrite(led[i], LOW);
}
if (millis() - time1 > 500)//大於0.5秒才進來一次
{
time1 - millis();
digitalWrite(led_pin, HIGH); delay(500);//亮0.5秒
digitalWrite(led_pin, LOW); delay(500);//不亮0.5秒
}
val = digitalRead(sw);
if (val == LOW)//按下去LOW才進來執行
{
if (millis() - time2 > 300)//大於300ms=0.3秒才進來執行
{
digitalWrite(led[j], HIGH);
delay(300);
if (j == 7)//由右向左快跑
j = 0;
else
j++;
}
}
else
{
digitalWrite(led[j], HIGH);
delay(1000);
if (j == 0)//到最右邊0時就又歸7最左邊
j = 7;
else//即為一開機為由左向右
j--;//在全域變數下, 您j=7, 每次-1即7654321
}
}
沒有留言:
張貼留言