Sometimes when we need to handle a set of buttons it may take most part of digital ports and limit the device capabilities. In these cases that we don't have enough available digital ports, a good solution is to use an analog port to handle a series of buttons and resistors.
Versão em português desse artigo: Controlando mútiplos botões com somente uma porta analógica do Arduino.
The ebl-arduino's ButtonEvent class is now capable to handle series of analog buttons. Consider the following breadboard project:
Note that it's a 10k resistors series. Click here to download the Fritzing source file for this project.
In order to handle these buttons you should proceed as the source code bellow:
#include <ButtonEvent.h> short Button1 = 230; short Button2 = 365; short Button3 = 460; void setup() { //initial buffer for 3 buttons ButtonEvent.initialCapacity = sizeof(ButtonInformation)*3; ButtonEvent.addButton(0, //analog button pin Button1, //analog value 20, //deviation onDown, //onDown event function onUp, //onUp event function onHold, //onHold event function 1000, //hold time in milliseconds onDouble, //onDouble event function 200); //double time interval ButtonEvent.addButton(0, //analog button pin Button2, //analog value 20, //deviation onDown, //onDown event function onUp, //onUp event function onHold, //onHold event function 1000, //hold time in milliseconds onDouble, //onDouble event function 200); //double time interval ButtonEvent.addButton(0, //analog button pin Button3, //analog value 20, //deviation onDown, //onDown event function onUp, //onUp event function onHold, //onHold event function 1000, //hold time in milliseconds onDouble, //onDouble event function 200); //double time interval Serial.begin(9600); } void loop() { ButtonEvent.loop(); } void onDown(ButtonInformation* Sender) { Serial.print("Button "); if (Sender->analogValue == Button1) Serial.print(1); else if (Sender->analogValue == Button2) Serial.print(2); else if (Sender->analogValue == Button3) Serial.print(3); Serial.println(" down!"); } void onUp(ButtonInformation* Sender) { Serial.print("Button "); if (Sender->analogValue == Button1) Serial.print(1); else if (Sender->analogValue == Button2) Serial.print(2); else if (Sender->analogValue == Button3) Serial.print(3); Serial.println(" up!"); } void onHold(ButtonInformation* Sender) { Serial.print("Button "); if (Sender->analogValue == Button1) Serial.print(1); else if (Sender->analogValue == Button2) Serial.print(2); else if (Sender->analogValue == Button3) Serial.print(3); Serial.print(" hold for "); Serial.print(Sender->holdMillis); Serial.println("ms!"); } void onDouble(ButtonInformation* Sender) { Serial.print("Button "); if (Sender->analogValue == Button1) Serial.print(1); else if (Sender->analogValue == Button2) Serial.print(2); else if (Sender->analogValue == Button3) Serial.print(3); Serial.print(" double click in "); Serial.print(Sender->doubleMillis); Serial.println("ms!"); }
This project has their own repository with more information about the classes, usage instructions and download options. It could be accessed through the URL http://code.google.com/p/ebl-arduino/.
not sure to get it ? no 5v input????
ReplyDeleteThannks for a great read
ReplyDelete