int led_pin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int j = -2;
int direct = 0;
int btn_pin = 10;
int val = 1;
int r=9;
void setup()
{
Serial.begin(9600);
pinMode(btn_pin, INPUT);
for (int i = 0; i < 8; i++)
{
pinMode(led_pin[i], OUTPUT);
}
}
void loop()
{
for (int k = 0; k < 8; k++)
{
digitalWrite(led_pin[k], LOW);
}
val = digitalRead(btn_pin);//按下去沒反應可能電位有問題,用邏輯探測棒debug
if (val == 0)
{
val = digitalRead(btn_pin);
delay(1000);
while (digitalRead(btn_pin) == LOW);
direct = !direct;
}
if (direct == 0)//由左往右跑每次+2
{
if (j == 6)
j = 0;
else
j = j + 2;
// 0 1 2 3 4 5 6 7
if(j==1) j=2;//如果奇數切過來是1就變偶數2(index)
if(j==3) j=4;//如果奇數切過來是3就變偶數4(index)
if(j==5) j=6;//如果奇數切過來是5就變偶數6(index)
if(j==7) j=0;//如果奇數切過來是7就變偶數0(index)
digitalWrite(led_pin[j], HIGH);
delay(1000);
}
else
{
if (j == 1)//由右往左跑,每次-2
j = 7;
else
j = j - 2;
// 0 1 2 3 4 5 6 7
if(j==0) j=7;//如果偶數切過來是0就變奇數7(index)
if(j==2) j=1;//如果偶數切過來是2就變奇數1(index)
if(j==4) j=3;//如果偶數切過來是4就變奇數3(index)
if(j==6) j=5;//如果偶數切過來是6就變奇數5(index)
digitalWrite(led_pin[j], HIGH);
delay(1000);
}
}
沒有留言:
張貼留言