Code Help, 4 inputs per hit

Hello! I'm new to Arduino and I am creating a Taiko controller as a passion project, so please take it easy on me, thanks!

I've been trying to get this code working on the pro micro for the past day or s0.

The code is taking an analog input from 4 piezo sensor, A0, A1, A2, A3, and converting it to a keyboard input. So for Example, if i were to tap a piezo sensor, based on which of the four, it's supposed to input either an 'a' 'b' 'c' or 'd', not all for at the same time.

For some reason, every time I hit a piezo, it type all 4 letters (abcd), instead of just one or just the other.
Do I have to add other conditions as to make this type just one letter per piezo?
Could anyone tell me if I've done something wrong, it' would be greatly appreciated.
Thanks you all in advance

include <Keyboard.h>
const int Sensor0 = A0;
const int Sensor1 = A1;
const int Sensor2 = A2;
const int Sensor3 = A3;
const int threshold0 = 100;
const int threshold1 = 100;
const int threshold2 = 100;
const int threshold3 = 100;
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;

void setup()
{
Keyboard.begin();
}
void loop()
{
val0 = analogRead(Sensor0);
val1 = analogRead(Sensor1);
val2 = analogRead(Sensor2);
val3 = analogRead(Sensor3);
if (val0 > threshold0)
{
Keyboard.press('a');
Keyboard.releaseAll();
}
if (val1 > threshold1)
{
Keyboard.press('b');
Keyboard.releaseAll();
}
if (val2 > threshold2)
{
Keyboard.press('c');
Keyboard.releaseAll();
}
if (val3 > threshold3)
{
Keyboard.press('d');
Keyboard.releaseAll();
}
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

It appears that all 4 inputs are going over the threshold.

Have you tried displaying the values you are getting to see if you have set the threshold way too low?

An alternative is to show only the first one triggered:

  if (val0 > threshold0)
  {
    Keyboard.press('a');
    Keyboard.releaseAll();
  }
  else  if (val1 > threshold1)
  {
    Keyboard.press('b');
    Keyboard.releaseAll();
  }
  else if (val2 > threshold2)
  {
    Keyboard.press('c');
    Keyboard.releaseAll();
  }
  else if (val3 > threshold3)
  {
    Keyboard.press('d');
    Keyboard.releaseAll();
  }

Actually now none are responsive at all?
Idk

You are there, we are not.

What is the exact piezo sensor you have four of?

How are they wired to the Arduino?

Do they need power of their own? If so, how are you providing this power?

How are they arranged mechanically? Any chance that tapping one can be "heard" by tapping another?

Take a few pictures. Draw a schematic and post both. Bury us with details.

Sounds like you may have damaged them or have dodgy wiring, power and/or mechanical circumstnces.

Did you say what Arduino board you are using, and how it is powered?

a7

Hi,
Can you please post a circuit diagram, showing component names, pin labels and power supplies?

A couple of images of your project would also help. so we can see your hardware layout.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thank you for your help!
This is the piezo sensor I have used, though i must say i used these on the same project a few year ago, worked well, all that went wrong was isolating the vibrations.

The piezos are wired to the analog ports A0 A1 A2 & A3 of an arduino pro micro using the breadboard 4 1M Ohm resistors (one per piezo)

Sorry for the jumbled mess, but basically it's just 4 separate analog inputs, and 4 to 1 to the ground port

Power to the piezo is being provided by the arduino pro micro which is powered only by a USB connection using a micro USB cable.
Don't know if I can change this as it will be used on PC with an emulator of the rhythm game "Taiko no Tatsujin".
As for the arrangement issue, there is none, as we have also tried testing them one by one, keeping only one connected to the arduino, even so when trying them together we keep them on different surfaces as to avoid one "hearing" the vibration of another.

This is how one of the piezos are connected, with the resistor being 1M Ohm.

Unfortunately, we don't know why we are having so much trouble, and we have encountered several issues such as: random non-stop inputs (even after having messed around with the thresholds and/or without touching them); inputs that don't match the analog in they corrispond to (for example A0 is supposed to input 'a', however it doesn't, it just spam a few letters, and if you press it often enough, it locks up (?), as if you were holding down several keys at once); even just trying to use one (with all the other ones disconnected) ends up typing all 4 inputs, a, b, c, & d, and doesn't stop spamming them.
Sorry for the mess, hopefully I haven't been too annoying, I've tried all i can before asking for help.
Thank you in advance to all who'll respond.

Hi,
Thanks for the info.

You might be worth putting Serial print statements in your code to see what signal you are getting on the analog inputs.
Remove the keyboard references from your code, for the moment, and just have pure analog input reading and display on your IDE monitor screen.

Tom... :smiley: :+1: :coffee: :australia:

Hi,

I notice you are reading each analog input in sequence, you may not be allowing the single ADC unit to read each input properly.
Try this;

val0 = analogRead(Sensor0);
val0 = analogRead(Sensor0);
val1 = analogRead(Sensor1);
val1 = analogRead(Sensor1);
val2 = analogRead(Sensor2);
val2 = analogRead(Sensor2);
val3 = analogRead(Sensor3);
val3 = analogRead(Sensor3);

It might look strange, but the ADC has a capacitor on its input and when the input multiplexer changes input, the must be time for the charge on the capacitor to equal the newer analog input.
Reading each input twice gives the capacitor a chance to keep up.

Tom... :smiley: :+1: :coffee: :australia:

Thank you, and sorry for the late answer, it was 2 am in italy when i last replied.
I'll try it right away and inform you of the result.

Hey, it worked for 20 seconds or so, though only the 'a' & 'c' inputs, then it went back to writing random inputs.
But it did work, even if just for a little bit.
Any tips on how to keep the code from having a stroke?

Hi,
You need to write some code that JUST reads the inputs and displays them on the IDE serial monitor, establish that you are receiving valid values.
Forget about all the if statements and keyboard statements.

Lets get down to basics to see if you have input.

Tom.. :smiley: :+1: :coffee: :australia:

Thank you!
Hopefully i wrote it right but the value i recieve from the arduino seem random.

#include <Keyboard.h>
const int Sensor0 = A0;
int val0 = 0;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  val0 = analogRead(Sensor0);
  Serial.println(val0);
  delay(100); 
}

Here are a few values while touching and applying pressure to the piezo.
As you can see the values stay around 300 even when not touching it, and don't change even while touching it.
New Text Document (3).txt (653 Bytes)

UPDATE: apparently, when adding the serial print in, in the code with the if statements, the piezos stop working, without it, it sometimes work other times it inputs random gibberish.

UPDATE#2: nevermind, stopped working and just spamming 'a's now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.