Skip to content

Instantly share code, notes, and snippets.

@ikks
Created July 25, 2014 02:52
Show Gist options
  • Save ikks/db272f2b30ebad7cc94e to your computer and use it in GitHub Desktop.
Save ikks/db272f2b30ebad7cc94e to your computer and use it in GitHub Desktop.
Counter for Arduino when pressing a Button
#include <MicroView.h> // include MicroView library
int buttonPin = A0; // push button pin
int buttonState = 0; // variable to store the pushbutton status
int counter = 0; // counts the number of clicks
int current = 0; // holds the current state
int ticks = 0; // cycle counter
int current_ticks = 0; // Last ticks when the button was released
int doubleclick_max = 50; // topmost double click, if you can't do better call someone younger :P
int difference = 0; // Your record in double click
void setup() {
uView.begin(); // start MicroView
uView.clear(PAGE); // clear page
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
digitalWrite(buttonPin,HIGH); // set Internal pull-up
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
// check if the pushbutton is pressed.
// if it is not pressed, the buttonState is HIGH:
if (buttonState == HIGH) {
if (current != buttonState) {
//Identifies when you released the button
if (ticks - current_ticks < doubleclick_max) {
// You will be marked as fast click
counter = 0;
difference = ticks - current_ticks;
if (difference != 0 && difference < doubleclick_max) {
//With this fast multiprocessor you should have played so much if you manage to reach 0 ;)
doubleclick_max = difference;
}
}
else {
counter = counter + 1;
}
current_ticks = ticks;
}
uView.circle(31, 23, 10);
uView.circleFill(31, 23, 9, BLACK, PAGE);
uView.circle(31, 23, 5);
uView.circleFill(31, 23, 4, BLACK, PAGE);
uView.display();
}
else {
// Let's show some reaction, why don't you play when you feel bored?
uView.circleFill(31, 23, 10, WHITE, PAGE);
uView.circleFill(31, 23, 4, BLACK, PAGE);
uView.display();
}
current = buttonState;
uView.setCursor(0, 0);
uView.print(" ");
uView.setCursor(0, 0);
uView.print(counter);
ticks = ticks + 1;
if (difference != 0) {
// Show off you can bang the most of all.
uView.setCursor(50, 23);
uView.print(" ");
uView.setCursor(50, 23);
uView.print(doubleclick_max);
}
uView.setCursor(0, 40);
uView.print("FstClickr");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment