Is there any way to get the AnalogButtons library to repeat a key when held down? I tried a few different ways to no avail.
void handleButtons(int id, boolean held)
{
if (held) {
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed and held");
int num = 0;
do
{
num = num++;
Serial.println(num);
delay(50);
analogButtons.checkButtons();
} while (held);
} else{
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed only");
}
}
don't work...
Still learning. :) I want this to work like a PC keyboard when the button is held down. Thanks for any help in advance!
Stop using the baby AnalogButtons library and just write the functions yourself. Then you have control over what is happening and you can make it do what you want.
Hmm... if you're very new to programming it might be difficult to explain... Do words like class, object, method, base and derived class make sense to you ?
I'm trying held() right now, think I'll post an example soon.
No repeat support, it seems, at least out-of-the-box.
Instead of writing to serial, you could toggle a boolean flag and have a function in loop that just checks for the flag and use the blink without delay trick ( http://arduino.cc/en/Tutorial/BlinkWithoutDelay ) to write the key to serial every X seconds as long as the flag is up. (or whatever else the OP had in mind)
Tuxduino, yes I have a basic understanding of most of it. this biggest issue I am having is trying to get it to know when you let go of the button. Everything works just I would like to increment a value when the button is held until the button is released just like a PC keyboard would. Maybe someone could help me on this?
I figured it out but thinking for a few min… simple as reading the analog pin (don’t even worry about the library) to see if is pressed out not. Man kinda a duh moment huh? Thanks guys for your help. Here is my code in case someone else runs into this.
do
{
num = num++;
Serial.println(num);
delay(50);
analogButtons.checkButtons();
} while (analogRead(A0) < 900);
Here is the working example…
/*
AnalogButtons,
created 02 Jan 2009 V 0.1
Connect more than one button to a single analog pin,
register a call-back function.which gets called when a button
is pressed or held down for the defined number of seconds. Includes
software key debouncing which may need to be adjusted, the the second
argument to AnalogButtons class. Define the ANALOG_PIN in the constructor
of AnalogButtons.
The circuit:
* 5 buttons, 1 side of all buttons connected together to +5V.
The other side of each button is connected via a different value
resister (tested with) 1k, 2k5, 5k8, 10k, 18k to one side of a
100k resister which is in turn connected to GND. At the point
where all the different resisters are joined you make a connection
to your analog input. Basicly a different voltage divider is setup
depending upon which button is pressed. You have to configure the
Buttons Hi/Low values, see the comments in example code below and the
AnalogButtons::configure(ANALOG_PIN) function.
More or less than 5 buttons could be added, just pick different values
of the resister sot hat all buttons have different values which arn't too
close in size.
I'm not sure what happens when Arduino is powered from batteries and Power V
drops below V5.
by Neil DUdman and everyone who's ever used Arduino
*/
#include "AnalogButtons.h"
#define ANALOG_PIN 0
// A call back function that you pass into the constructor of AnalogButtons, see example
// below. Alternitivly you could extend the Button class and re-define the methods pressed()
// or held() which are called
AnalogButtons analogButtons(ANALOG_PIN, 30, &handleButtons);
Button b1 = Button(1, 802,812);
Button b2 = Button(2, 635, 645);
Button b3 = Button(3, 412, 422);
Button b4 = Button(4, 242, 252);
Button b5 = Button(5, 93, 103);
Button b6 = Button(6, 0, 10);
void setup()
{
Serial.begin(9600);
Serial.println("Testing your Analog buttons");
analogButtons.addButton(b1);
analogButtons.addButton(b2);
analogButtons.addButton(b3);
analogButtons.addButton(b4);
analogButtons.addButton(b5);
analogButtons.addButton(b6);
}
void loop()
{
// To check values when button are pressed
analogButtons.checkButtons();
// To configure the MAX/Min values for each
// Button, uncomment this line, make sure you've called Serial.begin(9600);
// Then in turn hold town each botton, noting the max/min values
//AnalogButtons::configure(ANALOG_PIN); //delay(1000);
}
void handleButtons(int id, boolean held)
{
if (held) {
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed and held");
int num = 0;
//Repeat code start
do
{
num = num++;
Serial.println(num);
delay(50);
analogButtons.checkButtons();
} while (analogRead(A0) < 900);
//Repeat code end
} else{
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed only");
}
}