Debounce Analog Pins

Hi, i am trying to make a drumkit for clone hero,

for the pedal of the drumkit i used “bounce2.h” is it possible to make it work with analog ?
because when i tap on the drum sometime it detects multiple input of the same drum can i
debounce analog ? i use piezos for the drums.

for any other needed informations contact me !

-Bunchy

const int sens = 200;
const int del = 0;
int PinState = 0;
int onoff = 0;
const int Pinled = 6;
const int PinButton = 9;
#include <Keyboard.h>
#include <Bounce2.h>

Bounce debouncer  = Bounce();
Bounce cym1  = Bounce();
Bounce cym2  = Bounce();
Bounce cym3  = Bounce();
Bounce cym4  = Bounce();

void setup() {   

debouncer.attach(PinButton,INPUT_PULLUP);
debouncer.interval(25);

cym1.attach(A0);
cym1.interval(25);

cym2.attach(A1);
cym2.interval(25);

cym3.attach(A1);
cym3.interval(25);

cym4.attach(A3);
cym4.interval(25);



pinMode(Pinled, OUTPUT);
pinMode(PinButton, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, INPUT);
}
void loop() {

debouncer.update();
cym1.update();
cym2.update();
cym3.update();
cym4.update();

/*Pédale*/
onoff = digitalRead(10);
PinState = digitalRead(PinButton);
if(debouncer.rose()){
  digitalWrite(Pinled, HIGH);
  Keyboard.press('t');
}
/*Piezo 1*/
if(cym1.rose()){
  digitalWrite(2, HIGH);
  Keyboard.press('q');
}
/*Piezo 2*/
if(cym2.rose()){
  digitalWrite(3, HIGH);
  Keyboard.press('w');
}
/*Piezo 3*/
if(cym3.rose()){
  digitalWrite(4, HIGH);
  Keyboard.press('e');
}
/*Piezo 4*/
if(cym4.rose()){
  digitalWrite(5, HIGH);
  Keyboard.press('r');
}
Keyboard.releaseAll();
delay(del);
digitalWrite(Pinled, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}

Did you try increasing the existing debounce interval of 25ms?

I forgot to mention that it's not working cym1 to cym4 does not respond

bunchyearth23:
I forgot to mention that it's not working cym1 to cym4 does not respond

Then we will need a lot more information, mainly a wiring diagram or schematic.

Also you have now posted conflicting information, so which is true:

sometime it detects multiple input of the same drum

and

it's not working cym1 to cym4 does not respond

and
You didn't answer the question in reply #1.

Try setting the pinMode() of the A* pins to INPUT or INPUT_PULLUP to force them into digital mode before attaching them to a Bounce object

Or use the second parameter of attach() as you have in some instances in your sketch

aarg sorry for the confusion it's because another version of the code created this multiple input but it was before i use bounce2.h

and because of bounce on analog didn't seem to work adding more ms does nothing

UKHeliBob it is the first thing that i tried but it's not working

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi Tom,

There the schematic i’m not good at this i’m sorry.

P.S: the piezos is the drums

Maybe you falsely believe that the analog pins act differently somehow, when they are used for digital input? If so you are wrong. When A0-A5 are configured for digital I/O (which you did), there is absolutely no analog activity at all, and the pins function exactly the same as the digital pins 0-13, when performing digital I/O (which you are).

aarg, there my ancient code that work it’s this code that create the multiple input compared to the actual where the drums don’t even work.

const int sens = 80;
const int del = 45;
int PinState = 0;
int onoff = 0;
const int Pinled = 6;
const int PinButton = 9;
#include <Keyboard.h>
#include <Bounce2.h>

Bounce debouncer  = Bounce();
Bounce cym1  = Bounce();
Bounce cym2  = Bounce();
Bounce cym3  = Bounce();
Bounce cym4  = Bounce();

void setup() {   

debouncer.attach(PinButton,INPUT_PULLUP); 
debouncer.interval(25); 

cym1.attach(A0); 
cym1.interval(25); 

cym2.attach(A1); 
cym2.interval(25);

cym3.attach(A1); 
cym3.interval(25); 

cym4.attach(A3); 
cym4.interval(25); 

pinMode(Pinled, OUTPUT);
pinMode(PinButton, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, INPUT);
}
void loop() {

debouncer.update();
cym1.update();
cym2.update();
cym3.update();
cym4.update();

/*Pedal*/
onoff = digitalRead(10);
PinState = digitalRead(PinButton);
if(debouncer.rose()){
  digitalWrite(Pinled, HIGH);
  Keyboard.press('t');
}
/*Piezo 1*/
if(analogRead(A0) >= sens){
  digitalWrite(2, HIGH);
  Keyboard.press('q');
}
/*Piezo 2*/
if(analogRead(A1) >= sens){
  digitalWrite(3, HIGH);
  Keyboard.press('w');
}
/*Piezo 3*/
if(analogRead(A2) >= sens){
  digitalWrite(4, HIGH);
  Keyboard.press('e');
}
/*Piezo 4*/
if(analogRead(A3) >= sens){
  digitalWrite(5, HIGH);
  Keyboard.press('r');
}
delay(del);
Keyboard.releaseAll();
digitalWrite(Pinled, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}

Hi,
Thanks for the clear schematic;

Tom.. :slight_smile:

bunchyearth23:
aarg, there my ancient code that work it's this code that create the multiple input compared to the actual where the drums don't even work.

I'm confused. You say you have code that works. Just to be clear, is that the code in reply #10? Why not just use that code?

What problem are you actually experiencing with the original sketch? I assume that when you say it works, you mean that impulses at the piezo do light up the LEDs. But, you haven't explained what is wrong with it, and how you do want it to behave.

Do you have interfering impulses that you want to remove? Because they are too short? Too small? Do you want to trigger a pulse on the LEDs? You need to tell us the answer to these questions, so we don't have to wildly guess from a few sentences and your code.

I did observe, you are treating the inputs as switches... which could actually work. That is why I asked about the debounce constant 25ms. Actually, you haven't explained what happened when you tried increasing it other than "nothing". Unless the library has some hidden limit on the debounce interval, that is highly unlikely. Instead, you just said, "because it's analog" which doesn't really answer. If nothing else, it would have been helpful to say what value you changed it to.

Alternatively, it is definitely possible to condition the analog signal programatically. I have some recent code that does that, it runs an elliptical digital low pass filter on the analog values, then applies a hysteresis function. If you answer some fundamental questions about your needs, I could present more detail about that.

aarg, the first code that i have posted is the one where i tried to debounce it myself but don’t work

the second that i posted in reply 10 is the older version that work but have multiple keyboard output not only one the goal is to i tap on the drums but output only one key and not multiple ones.

to respond to the questions:

“What problem are you actually experiencing with the original sketch?”

with the first that i posted it just don’t work the one in reply 10 have multiple keyboard output the LED is mandatory only to know if there is a signal.

“Do you have interfering impulses that you want to remove? Because they are too short? Too small? Do you want to trigger a pulse on the LEDs?”

i do not comprehend this.

“Actually, you haven’t explained what happened when you tried increasing it.”

in the first code i tried but change nothing, in the one in reply 10 debounce is not used

“Alternatively, it is definitely possible to condition the analog signal programatically. I have some recent code that does that, it runs an elliptical digital low pass filter on the analog values, then applies a hysteresis function. If you answer some fundamental questions about your needs, I could present more detail about that.”

i’m not good with analog so i do not know what you are talking in this.

if there more things that i need to respond don’t hesitate.

-Bunchy

bunchyearth23:
in the first code i tried but change nothing, in the one in reply 10 debounce is not used

It most definitely is used:

debouncer.interval(25);

cym1.attach(A0);
cym1.interval(25);

What I want from you is a functional description, like this (but this one is imaginary, just an example):

“Any piezo signal exceeding approximately 2V should register as a key press, and should not register any more key presses until a time out period of 100ms has elapsed”.

Or at least a good problem report like,
“When a drum is hit, multiple key presses are registered”

In the last example, if that is indeed the case, you might have to reduce the input amplitude by changing your hardware, because it is possible that the input is too sensitive. Also much finer troubleshooting activity would be welcome. For example, in the same case, what happens when the drum is tapped lightly vs. hard? These small details that we can’t see, are important.

If your input sensitivity is too high, you will not be able to use debouncing routines because they will have to be made so long, that the response to inputs will be longer than the interval between drum beats, and the system will not be usable.

You might also write a sketch that just records and displays the analog values on serial - if you follow the correct format, you can even plot it like an oscilloscope in the serial plotter feature of the IDE. This would give you a better idea of what kind of signal you are dealing with. Hint, hint - you could share it with us! :slight_smile:

Also, I suggest inserting 1.0k ohm resistors in series with the analog inputs (no change to the 1M pull downs). The piezos can easily generate currents in excess of what the clamping diodes on the IC inputs should really be exposed to.

aarg so what i need,

is when i tap one of the piezo it register one key of the keyboard and light up one led but only one key,

example

when i tap it register a “a” with my actual code when i tap it register “aaaaa” like if i tapped it 5 times
but i only want one letter per tap on the piezo.

i cannot give you a voltage because i need to be able to modify it easily and i need the least delay possible because this project is for a rythm game.

if another information needed don’t hesitate.

-Bunchy

Okay, please also address the other issues that I mentioned in reply #14. I told you there are problems with achieving "the least delay possible". I clearly explained what they are. Until you come back with some response to those problems, I can't help you.

Also you can give me a voltage, I told you how to measure it. The fact that you can modify it has no bearing on whether you know it or not.

aarg,

so i tried to mesure but a obtain values like 0.200V to 12V.

when i hit lightly or hard it does not change much at all.

for creating a sketch that record and display analog values how i do that ?

and i’m gonna add 1kohm resistor to the piezo.

“Any piezo signal exceeding approximately 2V should register as a key press, and should not register any more key presses until a time out period of 100ms has elapsed”.

this can be a solution but how i do that ?

-Bunchy

Did you try changing the 25ms debounce interval to something longer?

so i tried to mesure but a obtain values like 0.200V to 12V.

I assume that is a static measurement, with a DMM? That is not what I meant. We need to know how those values are changing in time. I told you about streaming the values to the serial output to see what numbers are actually appearing there. I believe there is actually an example sketch that ships with the IDE that will stream the values. That is what I want you to do. If you can’t figure out how to use the plotter, just copy the output from the serial monitor and paste it in to a message here.

You can modify “AnalogOutSerial” to do that. If you can’t make a sketch just print out the values, honestly the chances of you getting the rest of this done are faint. I’m perplexed that you don’t know how to print analog values, because your second program reads values using analogRead(). It seems a rather trivial extension to just print the values…

There the values

1023
548
1023
280
347
312
59
162
144
68
97
59
22
25
6
6
6
6
6
6
6
7
6
6
6
6
6
6
711
28
411
622
274
221
230
95
104
45
36
25
6
9
7
7
7
7
6
7
6
6
6
6
6
349
1023
623
710
590
285
241
219
132
83
92
46
27
20
7
6
6
7
6
6
6
6
6
6
6
6
851
325
334
481
228
189
189
109
93
44
41
24
6
7
6
7
8
7
6
7
7
6
6
6
6
136
888
582
711
464
269
215
153
124
61
75
35
15
14
6
6
7
6
6
6
6
6
7
6
6

and the code that i used

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int Value = analogRead(A0);
if(Value > 5){
Serial.println(Value);
delay(10);
}}