Introduction: One Button Multiple Functions !!#Arduino

Picture of One Button Multiple Functions !!#Arduino

Instead of using multiple buttons to select different function , here is a simple technique which uses only one button to select different function !!

Step 1: Components Needed

Picture of Components Needed

Arduino uno ,
Push button,
led !!

Step 2: Connection!!

Picture of Connection!!

Led to pin 13

Button to Analog 1

Step 3: Code !!


#include "OneButton.h"

// The actions I ca do...
typedef enum {
ACTION_NONE, // do nothing.
ACTION_ON, // set LED "ON"
ACTION_SLOW, // blink LED "SLOW"
ACTION_FAST // blink LED "FAST"
}
MyActions;

// Setup a new OneButton on pin A1.
OneButton button(A1, true);

MyActions nextAction = ACTION_NONE; // no action when starting

// setup code here, to run once:
void setup() {
// enable the standard led on pin 13.
pinMode(13, OUTPUT); // sets the digital pin as output

// link the myClickFunction function to be called on a click event.
button.attachClick(myClickFunction);

// link the doubleclick function to be called on a doubleclick event.
button.attachDoubleClick(myDoubleClickFunction);
} // setup


// main code here, to run repeatedly:
void loop() {
unsigned long now = millis();

// keep watching the push button:
button.tick();

// You can implement other code in here or just wait a while

if (nextAction == ACTION_NONE) {
// do nothing.
digitalWrite(13, LOW);

} else if (nextAction == ACTION_ON) {
// turn LED on
digitalWrite(13, HIGH);

} else if (nextAction == ACTION_SLOW) {
// do a slow blinking
if (now % 1000 < 500) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
} // if

} else if (nextAction == ACTION_FAST) {
// do a fast blinking
if (now % 200 < 100) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
} // if

} // if

} // loop


// this function will be called when the button was pressed 1 time and them some time has passed.
void myClickFunction() {
if (nextAction == ACTION_NONE)
nextAction = ACTION_ON;
else
nextAction = ACTION_NONE;
} // myClickFunction


// this function will be called when the button was pressed 2 times in a short timeframe.
void myDoubleClickFunction() {
if (nextAction == ACTION_ON) {
nextAction = ACTION_SLOW;

} else if (nextAction == ACTION_SLOW) {
nextAction = ACTION_FAST;

} else if (nextAction == ACTION_FAST) {
nextAction = ACTION_ON;
} // if
} // myDoubleClickFunction

// End

Step 4: Library !! Link

http://www.mathertel.de/Arduino/OneButtonLibrary.aspx&sa=U&ved=0ahUKEwitn4z7_cnLAhXC0iYKHWgqDecQFggfMAU&sig2=5UzHZ_hnprc_HBL7pg_4HQ&usg=AFQjCNGhorWrXney3BBtKcyids3KiI_pow

Comments

About This Instructable

3,323views

13favorites

License:

Bio: Electronics and Automation Engineer, Game designer, Artist! EMAIL: amaraxr@gmail.com
More by AXR AMAR:#DIY# How to grow Bonsai (or any plant) using Internet of things (IOT)#DIY# A Complete Guide to Build Your own ATtiny85 Project PCB with Relay and Interfacing HC05 Bluetooth Module to it Pinguino Project (a PIC microcontroller based Arduino #No Programmer Required)
Add instructable to: