|
各位前輩好!小弟之前有寫個程式是用每個 BIT 去判斷要讓 LED 亮或滅!是否可以改成一次就檢查每個bit不需要一個一個檢查!以下是我原始碼
int i;
int led[]={2,3,4,5,6,7,8,9};
void setup()
{
Serial.begin(9600);
for(i=0;i<8;i++)
pinMode(led[i],OUTPUT);
for(i=0;i<8;i++)
digitalWrite(led[i],HIGH);
}
void loop()
{
if(Serial.available()>0)
{
unsigned char data = Serial.read();
if(data&0x01)
digitalWrite(2,LOW);
else
digitalWrite(2,HIGH);
if(data&0x02)
digitalWrite(3,LOW);
else
digitalWrite(3,HIGH);
if(data&0x04)
digitalWrite(4,LOW);
else
digitalWrite(4,HIGH);
if(data&0x08)
digitalWrite(5,LOW);
else
digitalWrite(5,HIGH);
if(data&0x10)
digitalWrite(6,LOW);
else
digitalWrite(6,HIGH);
if(data&0x20)
digitalWrite(7,LOW);
else
digitalWrite(7,HIGH);
if(data&0x40)
digitalWrite(8,LOW);
else
digitalWrite(8,HIGH);
if(data&0x80)
digitalWrite(9,LOW);
else
digitalWrite(9,HIGH);
}
} |
|