Hello!
I recently encountered a problem with the Keyboard.h library. My previous versions of the code work, as do the examples (tested just now, after my new code), so I know the problem isn't my arduino, library compatability or connections. I also know that it can send the characters, as my previous code worked. I also know the code reaches the point attached as it prints to the serial monitor. My board of choice (seeeduino XIAO) also has a transmitting light or something, which lights up when it is sending data to the serial monitor or the computer. This light turns on but nothing appears on field i'm typing to. I have also tried multiple computers so i don't think that's the problem. The code snippet: (full code below as i can't attach it as an .ino file)
for (int i = 0; i < 15; i++) {
if (pressed[i] == true) {
//Keyboard.press(128);
//Keyboard.press(129);
if (lastPressed[i] == false) {
Keyboard.press(i + 33 + mode * 30);
Serial.println(i + 33 + mode * 30);
}
lastPressed[i] = true;
}
else if (lastPressed[i] == true) {
Keyboard.release(i + 33 + mode * 30);
lastPressed[i] = false;
}
}
FULL CODE:
#include <Mouse.h>
#include <Keyboard.h>
#define CLK 8
#define DT 9
#define ModeSwitchPin A10
int DBTime = 50; //SWITCH DEBOUNCE DELAY
unsigned long LPT[15]; //THIS IS AN ARRAY OF PRESSED TIMES
bool lastPressed[15]; //ARRAY OF SWITCHES LAST LOOP PRESSED (THERE ARE 15 TOTAL)
int BNR = 0; //THIS IS THE CURRENT BUTTON NUMBER IN THE LOOP
bool pressed[15]; //CURRENT PRESSED SWITCHES
int mode = 0; //MACROPAD WORKING MODE
int currentSwitch; //MODE SWITCH STATE
int counter = 0; //ROTARY ENCODER STUFF
int currentStateCLK;
int lastStateCLK;
int loopStart;
int loopEnd;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
Serial.begin(9600);
Keyboard.begin();
Mouse.begin();
lastStateCLK = digitalRead(CLK);
}
void loop() {
loopStart = millis();
//-----Mode determining-----
currentSwitch = analogRead(ModeSwitchPin);
if (currentSwitch > 500 && currentSwitch < 600) mode = 2; //normal
else if (currentSwitch > 300 && currentSwitch < 400) mode = 1; //obs
else if (currentSwitch < 100) mode = 0; //koolitöö
//-----Reading rotary encoder-----
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
counter --;
if (mode == 0) {
Keyboard.write(0xD7);
}
else if (mode == 1) {
Mouse.move(0, 0, -1);
}
else if (mode == 2) {
Mouse.click(MOUSE_RIGHT);
}
} else {
// Encoder is rotating CW so increment
counter ++;
if (mode == 0) {
Keyboard.write(0xD8);
}
else if (mode == 1) {
Mouse.move(0, 0, 1);
}
else if (mode == 2) {
Mouse.click(MOUSE_LEFT);
}
}
}
lastStateCLK = currentStateCLK;
//-----Reading buttons-----
for (int column = 5; column < 8; column++) {
digitalWrite(column, 1);
for (int row = 0; row < 5; row++) {
BNR = (column - 5) * 5 + row;
if (analogRead(row) >= 900) {
LPT[BNR] = millis();
pressed[BNR] = true;
}
else if (millis() - LPT[BNR] < DBTime && lastPressed[BNR] == true) {
pressed[BNR] = true;
}
}
digitalWrite(column, 0);
}
for (int i = 0; i < 15; i++) {
if (pressed[i] == true) {
//Keyboard.press(128);
//Keyboard.press(129);
if (lastPressed[i] == false) {
Keyboard.press(i + 33 + mode * 30);
Serial.println(i + 33 + mode * 30);
}
lastPressed[i] = true;
}
else if (lastPressed[i] == true) {
Keyboard.release(i + 33 + mode * 30);
lastPressed[i] = false;
}
}
for (int i = 0; i < 33; i++) {
pressed[i] = 0;
}
loopEnd = millis();
// Serial.println(loopEnd - loopStart);
}
Thanks in advance!