Hi! I am quite new in electronics and I have issue.
I am doing custom keyboard using arduino micro and potentiometer with switches.
I was following guides and all was good when I was tested separately. When I make it working together, turning pot also triggers buttons clicks (sometimes keyboard prints pressed). And other way around, button click also triggers pot value changes (I get down, pressed, up).
I don't use any additional resistors (should I?). Connection is very simple. Button connected to ground and pin 42. Pot connected to 5v, ground and pin 41. I will attach schematics below.
I assume this happens because voltage drops down or something. I am using type-c connector, it should be enough power for arduino, I guess.
Maybe something wrong with my logic. I added delay to button to avoid fluctuations. Will add a capacitor and remove this delay. Maybe this is the problem?
Posting code here.
Thank you in advance for help!
#include <math.h>
#include <Keyboard.h>
#define PORT_POTENTIOMETER 41
#define PORT_BUTTON 42
#define LIMIT 50
// Keyboard
void mute();
void quit();
void raise();
// Volume
void volume_up();
void volume_down();
// Controllers
void read_button();
void read_rotary();
// Main
void setup() {
pinMode(PORT_BUTTON, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
read_button();
read_rotary();
}
// Implementations
int is_low = 1;
void read_button() {
int state = digitalRead(PORT_BUTTON);
if (is_low && state == HIGH) {
Keyboard.print("pressed\n");
}
is_low = state == LOW;
delay(100);
}
char current = 0;
void read_rotary() {
char value = analogRead(PORT_POTENTIOMETER);
char diff = value - current;
if (abs(diff) <= LIMIT) {
return;
}
if (diff > 0) {
volume_up();
} else {
volume_down();
}
current = value;
}
void mute() {
Keyboard.print("mute\n");
// Keyboard.press('D');
// Keyboard.press(KEY_LEFT_CTRL);
// Keyboard.releaseAll();
}
void raise() {
Keyboard.print("raise\n");
// Keyboard.press(KEY_LEFT_CTRL);
// Keyboard.press(KEY_LEFT_GUI);
// Keyboard.press('H');
// Keyboard.releaseAll();
}
void quit() {
Keyboard.print("quit\n");
// Keyboard.press(KEY_LEFT_CTRL);
// Keyboard.press('W');
// Keyboard.releaseAll();
}
void volume_up() {
Keyboard.print("up\n");
// Keyboard.press(KEY_RIGHT_GUI);
// Keyboard.press(KEY_F10);
// Keyboard.releaseAll();
}
void volume_down() {
Keyboard.print("down\n");
// Keyboard.press(KEY_RIGHT_GUI);
// Keyboard.press(KEY_F9);
// Keyboard.releaseAll();
}
