int led_pin[]={2,3,4,5,6,7,8,9};
int led_table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int btn1=10;
int num=-1;
int direct=0;
int val;
unsigned long time1=0;
unsigned long time2=0;
void setup()
{
int x;
pinMode(btn1,INPUT);
pinMode(13,OUTPUT);
for(x=0;x<8;x++)
{
pinMode(led_pin[x],OUTPUT);
}
}
void outport(unsigned char data)
{
int x;
for(int x=0;x<8;x++)
{
if(data %2==1)
digitalWrite(led_pin[x],HIGH);
else
digitalWrite(led_pin[x],LOW);
data=data/2;
}
}
void loop()
{
if(millis()-time1>=500)
{
time1=millis();
digitalWrite(13,HIGH);delay(1000);
digitalWrite(13,LOW);delay(1000);
}
if(direct==0)
{
if(num==7)//小括號要寫兩個等於等於,否則只會停在第1個LED
{
direct=1;
}
else
{
num++;
}
outport(led_table[num]);
delay(1000);
}
else if(direct==1)
{
if(num==0)//小括號要寫兩個等於等於,否則只會停在第1個LED
{
direct=0;
}
else
num--;
outport(led_table[num]);
delay(300);
}
}
沒有留言:
張貼留言