Created
October 15, 2013 18:45
-
-
Save geckotang/6996632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @link http://s-azcat.com/archives/389 | |
int buttonA = 0; | |
int lastButtonA = 0; | |
int buttonB = 0; | |
int lastButtonB = 0; | |
unsigned long time1 = 0; | |
unsigned long time2 = 0; | |
int PIN_FLAG_A = 2; | |
int PIN_FLAG_B = 3; | |
int PIN_BTN_A = 8; | |
int PIN_BTN_B = 9; | |
int HOLDTIME = 5000; | |
bool flagA = false; | |
bool flagB = false; | |
void toggleLed() { | |
if (flagA && !flagB) { | |
digitalWrite(PIN_FLAG_A, flagA); | |
digitalWrite(PIN_FLAG_B, flagB); | |
} | |
if (flagB && !flagA) { | |
digitalWrite(PIN_FLAG_A, LOW); | |
digitalWrite(PIN_FLAG_B, HIGH); | |
} | |
if (!flagA && !flagB) { | |
digitalWrite(PIN_FLAG_A, LOW); | |
digitalWrite(PIN_FLAG_B, LOW); | |
} | |
} | |
void setup() { | |
pinMode(PIN_BTN_A,INPUT); | |
pinMode(PIN_BTN_B,INPUT); | |
pinMode(PIN_FLAG_A, OUTPUT); | |
pinMode(PIN_FLAG_B, OUTPUT); | |
} | |
void loop() { | |
toggleLed(); | |
buttonA = digitalRead(PIN_BTN_A); | |
buttonB = digitalRead(PIN_BTN_B); | |
// | |
// buttnA (RED) | |
// | |
//ボタンの状態が変化 | |
if(buttonA != lastButtonA) { | |
if(buttonA == HIGH) { | |
//ボタンが押された時間を保存 | |
time1 = millis(); | |
} | |
//ボタンの状態を保存 | |
lastButtonA = buttonA; | |
} else { | |
//ボタンが押されてから1秒経過し、かつボタンの状態がHIGH | |
if((millis() - time1 > HOLDTIME) && (buttonA == HIGH)){ | |
flagA = true; | |
flagB = false; | |
time1 = 0; | |
} | |
} | |
// | |
// buttnB (BLUE) | |
// | |
if(buttonB != lastButtonB) { | |
if(buttonB == HIGH) { | |
time2 = millis(); | |
} | |
lastButtonB = buttonB; | |
} else | |
if((millis() - time2 > HOLDTIME) && (buttonB == HIGH)){ | |
flagA = false; | |
flagB = true; | |
time2 = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment