http://www.powenko.com/wordpress/digital-%E6%95%B8%E4%BD%8D%E8%BC%B8%E5%85%A5%E8%88%87%E8%BC%B8%E5%87%BA/
byte led1Pin = 8;
byte led2Pin = 9;
byte bnt1Pin = 2;
byte bnt2Pin = 3;
boolean state1;
boolean state2;
boolean buttonUp1;
boolean buttonUp2;
void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
pinMode(bnt1Pin, INPUT);
pinMode(bnt2Pin, INPUT);
digitalWrite(bnt1Pin, HIGH);
digitalWrite(bnt2Pin, HIGH);
}
void loop()
{
if(digitalRead(bnt1Pin) == LOW && buttonUp1 == true)
{
state1 = !state1;
digitalWrite(led1Pin, state1);
tone(10,500,500);
buttonUp1 = false;
}
else if(digitalRead(bnt1Pin) == HIGH && buttonUp1 == false)
{
buttonUp1 = true;
}
if(digitalRead(bnt2Pin) == LOW && buttonUp2 == true)
{
state2 = !state2;
digitalWrite(led2Pin, state2);
tone(10,500,500);
buttonUp2 = false;
}
else if(digitalRead(bnt2Pin) == HIGH && buttonUp2 == false)
{
buttonUp2 = true;
}
delay(10); // Delay 時間可視情況調整
}
沒有留言:
張貼留言