Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatsuro-ueda/6c5d082badcd6af8e1c0 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/6c5d082badcd6af8e1c0 to your computer and use it in GitHub Desktop.
ArduinoでLEDを点灯させるトグルスイッチのコード
int lightIsOn = LOW;
int switchIsWaiting = HIGH;
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
if(digitalRead(12) == HIGH){ // スイッチが押されているなら
if(switchIsWaiting){
if(lightIsOn){
// 消灯
lightIsOn = LOW;
digitalWrite(13, LOW);
} else { // 消灯しているなら
// 点灯
lightIsOn = HIGH;
digitalWrite(13, HIGH);
}
switchIsWaiting = LOW; // スイッチは、もはや待機しない
}
} else { // スイッチが押されていない
switchIsWaiting = HIGH;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment