本實驗就是將按鍵控制LED的實驗擴展成3 個按鍵對應3 個LED,佔用6 個數位I/O 端口。
int redled=10; //LED
int yellowled=9;
int greenled=8;
int redpin=7; //KEY
int yellowpin=6;
int greenpin=5;
int red;
int yellow;
int green;
void setup()
{
pinMode(redled,OUTPUT); //output
pinMode(yellowled,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(redpin,INPUT); //input
pinMode(yellowpin,INPUT);
pinMode(greenpin,INPUT);
}
void loop()
{
red=digitalRead(redpin);
if(red==LOW)
{ digitalWrite(redled,LOW);}
else
{ digitalWrite(redled,HIGH);}
yellow=digitalRead(yellowpin);
if(yellow==LOW)
{ digitalWrite(yellowled,LOW);}
else
{ digitalWrite(yellowled,HIGH);}
green=digitalRead(greenpin);
if(green==LOW)
{ digitalWrite(greenled,LOW);}
else
{ digitalWrite(greenled,HIGH);}
}
沒有留言:
張貼留言