char piliTable1[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
char piliTable2[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
char piliTable3[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};
char piliTable4[] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
char piliPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
char sw1 = 10;
char sw2 = 11;
char sw3 = 12;
char sw4 = 13;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int number1;
int number2;
int number3;
int number4;
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
void setup()
{
int i;
for (i = 0; i < 8; i++)
{
pinMode(piliPin[i], OUTPUT);
digitalWrite(piliPin[i], LOW);
}
digitalWrite(sw1, INPUT);
digitalWrite(sw2, INPUT);
digitalWrite(sw3, INPUT);
digitalWrite(sw4, INPUT);
}
void loop()
{
int val1;
int val2;
int val3;
int val4;
int i;
val1 = digitalRead(sw1);
{
if (val1 == 0)
{
delay(20);
while (digitalRead(sw1) == LOW);
state1 = 1;
state2 = 0;
state3 = 0;
state4 = 0;
}
}
//=========================================
val2 = digitalRead(sw2);
{
if (val2 == 0)
{
delay(20);
while (digitalRead(sw2) == LOW);
state1 = 0;
state2 = 1;
state3 = 0;
state4 = 0;
}
}
//============================================
val3 = digitalRead(sw3);
{
if (val3 == 0)
{
delay(20);
while (digitalRead(sw3) == LOW);
state1 = 0;
state2 = 0;
state3 = 1;
state4 = 0;
}
}
//============================================
val4 = digitalRead(sw4);
{
if (val4 == 0)
{
delay(20);
while (digitalRead(sw4) == LOW);
state1 = 0;
state2 = 0;
state3 = 0;
state4 = 1;
}
}
//===============================================
if (state1 == 1 && state2 == 0 && state3 == 0 && state4 == 0)
{
number1 = counter1;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable1[number1 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time1 >= 500)
{
time1 = millis();
counter1++;
if (counter1 > 7)counter1 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 1 && state3 == 0 && state4 == 0)
{
number2 = counter2;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable2[number2 % 8], i))
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time2 >= 500)
{
time2 = millis();
counter2++;
if (counter2 > 7)counter2 = 0;
}
}
//============================================
if (state1 == 0 && state2 == 0 && state3 == 1 && state4 == 0)
{
number3 = counter3;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable3[number3 % 13], i))//有13個字型,所以要取餘數13,否則會停在第一格
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time3 >= 500)
{
time3 = millis();
counter3++;
if (counter3 > 13)counter3 = 0;
}
}
//================================================
if (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 1)
{
number4 = counter4;
for (i = 0; i < 8; i++)
{
if (bitRead(piliTable4[number4 % 8], i))//pili燈表4
digitalWrite(piliPin[i], HIGH);
else
digitalWrite(piliPin[i], LOW);
}
if (millis() - time4 >= 500)
{
time4 = millis();
counter4++;
if (counter4 > 7)counter4 = 0;
}
}
}
沒有留言:
張貼留言