Created
August 15, 2013 06:09
-
-
Save geckotang/6238629 to your computer and use it in GitHub Desktop.
5秒押したら点滅したままになる。
点滅したままに成った状態で、再度長押しすると変なことになる。
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
// Requrements | |
// ActionButton library by Jeff Hoefs | |
// https://github.com/soundanalogous/ActionButton/ | |
#include <ActionButton.h> | |
const int ledPin_1 = 13; // RED | |
const int ledPin_2 = 12; // BLUE | |
const int btnPin_1 = 8; // RED | |
const int btnPin_2 = 7; // BLUE | |
const int pressedTime = 1000; | |
const int maxCount = 5; | |
int currentCount = 0; | |
int currentBtnPin = 0; | |
int currentLedPin = 0; | |
ActionButton btn_1 = ActionButton(btnPin_1, ActionButton::PULL_DOWN); | |
ActionButton btn_2 = ActionButton(btnPin_2, ActionButton::PULL_DOWN); | |
// ボタンが押されたら | |
void onPressed(ActionButton & btn) { | |
// シリアルに状態をプリントし | |
Serial.print("pressed "); | |
Serial.print("button "); | |
Serial.println(btn.getPin()); | |
// LEDを点灯 | |
if (btn.getPin() == btnPin_1) { | |
digitalWrite(ledPin_1, HIGH); | |
} else { | |
digitalWrite(ledPin_2, HIGH); | |
} | |
} | |
// ボタンが離されたら | |
void onReleased(ActionButton & btn) { | |
// シリアルに状態をプリントし | |
Serial.print("released "); | |
Serial.print("button "); | |
Serial.println(btn.getPin()); | |
// LEDを消灯 | |
if (btn.getPin() == btnPin_1 && currentBtnPin != btnPin_1) { | |
digitalWrite(ledPin_1, LOW); | |
} else if (currentBtnPin != btnPin_2 && currentBtnPin != btnPin_2) { | |
digitalWrite(ledPin_2, LOW); | |
} else { | |
} | |
} | |
// ボタンが長押しされたら | |
void onSustainedPress(ActionButton & btn) { | |
// シリアルに状態をプリントし | |
Serial.print("sustained "); | |
Serial.print("button "); | |
Serial.println(btn.getPin()); | |
// 現在長押しした方のボタン | |
currentBtnPin = btn.getPin(); | |
currentCount++; | |
if (currentCount < maxCount) { | |
if (btn.getPin() == btnPin_1) { | |
digitalWrite(ledPin_1, HIGH); | |
delay(250); | |
digitalWrite(ledPin_1, LOW); | |
delay(250); | |
}else{ | |
digitalWrite(ledPin_2, HIGH); | |
delay(250); | |
digitalWrite(ledPin_2, LOW); | |
delay(250); | |
} | |
} else { | |
currentCount = 0; | |
// 点灯させたままにするLED | |
if (currentBtnPin == btnPin_1) { | |
currentLedPin = ledPin_1; | |
// 逆側のLEDを消灯 | |
digitalWrite(ledPin_2, LOW); | |
} else { | |
currentLedPin = ledPin_2; | |
// 逆側のLEDを消灯 | |
digitalWrite(ledPin_1, LOW); | |
} | |
// 3回点滅 | |
for (int i = 0; i < 3; i++) { | |
digitalWrite(currentLedPin, HIGH); | |
delay(250); | |
digitalWrite(currentLedPin, LOW); | |
delay(250); | |
} | |
digitalWrite(currentLedPin, HIGH); | |
} | |
} | |
void setup() { | |
pinMode(ledPin_1, OUTPUT); | |
pinMode(ledPin_2, OUTPUT); | |
Serial.begin(9600); | |
// ボタンにイベントをセット | |
btn_1.attach(ActionButton::PRESSED, onPressed); | |
btn_1.attach(ActionButton::RELEASED, onReleased); | |
btn_1.attach(ActionButton::SUSTAINED, onSustainedPress); | |
btn_1.setSustainedInterval(pressedTime); | |
// ボタンにイベントをセット | |
btn_2.attach(ActionButton::PRESSED, onPressed); | |
btn_2.attach(ActionButton::RELEASED, onReleased); | |
btn_2.attach(ActionButton::SUSTAINED, onSustainedPress); | |
btn_2.setSustainedInterval(pressedTime); | |
} | |
void loop() { | |
// ループ中で毎回updateメソッドを呼んで更新 | |
btn_1.update(); | |
btn_2.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment