Skip to content

Instantly share code, notes, and snippets.

@geckotang
Created August 9, 2013 10:02
Show Gist options
  • Save geckotang/6192565 to your computer and use it in GitHub Desktop.
Save geckotang/6192565 to your computer and use it in GitHub Desktop.
ボタンを長押しすると赤LEDが光る。再度長押しすると赤LEDが消えて緑LEDが光る。
/*
* ActionButton example
* Jeff Hoefs
* 11/3/11
*
* This example demonstrates a few diffrent ways to use the ActionButton
* library. You can attach event listeners, or poll for the changes in
* button state.
*
* Note that you must specify the button mode as the 2nd paramter of the
* ActionButton constructor (PULL_UP, PULL_UP_INTERNAL, PULL_DOWN)
* according to how you have wired the button to your Arduino.
*/
#include <ActionButton.h>
// the callback function provides a referene to the
// button that fired the event
void onPressed(ActionButton & btn) {
Serial.print("button ");
Serial.print(btn.getPin());
Serial.println(" pressed");
}
void onReleased(ActionButton & btn) {
Serial.print("button ");
Serial.print(btn.getPin());
Serial.println(" released");
}
void onSustainedPress(ActionButton & btn) {
Serial.print("button ");
Serial.print(btn.getPin());
Serial.println(" sustained");
}
// a change event is fired on each transition between pressed and
// released states
void onChange(ActionButton & btn) {
String state = "";
switch (btn.getState()) {
case ActionButton::PRESSED:
state = " pressed";
break;
case ActionButton::RELEASED:
state = " released";
break;
}
Serial.print("button ");
Serial.print(btn.getPin());
Serial.println(state);
}
/*
init
*/
ActionButton btn1 = ActionButton(2, ActionButton::PULL_DOWN);
boolean isTeamA = true;
boolean isTeamB = true;
const int LED_PIN_A = 12;
const int LED_PIN_B = 13;
void setup() {
Serial.begin(57600);
// LED
pinMode(LED_PIN_A, OUTPUT);
pinMode(LED_PIN_B, OUTPUT);
// button
btn1.attach(ActionButton::PRESSED, onPressed);
btn1.attach(ActionButton::RELEASED, onReleased);
btn1.attach(ActionButton::SUSTAINED, onSustainedPress);
btn1.setSustainedInterval(2000);
}
void loop() {
// must call update for each button
btn1.update();
/*
if (btn1.isPressed()) Serial.println("button 7 pressed");
if (btn1.isReleased()) Serial.println("button 7 released");
*/
if (btn1.isSustained()){
Serial.println("Team A got flag!!");
isTeamA = !isTeamA;
isTeamB = !isTeamA;
}
digitalWrite( LED_PIN_A, isTeamA );
digitalWrite( LED_PIN_B, isTeamB );
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment