Hi all (beginner here), I have a problem with creating key combinations using a simple ShiftIn circuit with Arduino Micro and CD4021BE - arranged like in the official tutorial
Now (please see attached code) is set up to simply write a lower case letter with a press of a button:
#include <Keyboard.h>
int dataPin = 9;
int clockPin = 7;
int latchPin = 8;
unsigned long last_press = 0;
unsigned long last_key_press = 0;
char* letters[] ={"abcdefgh"};
int num_chars = 8;
int num_registers = 1;
byte button_states[] = {0x00,0x00,0x00,0x00}; //01001000
int cursor_index = 0;
int char_index = 0;
char cur_char = 'X';
void setup() {
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
Keyboard.begin();
}
void loop() {
if (millis() - last_key_press > 350) read_keyboard();
delay(50);
}
void read_keyboard(){
//set it to 0 to transmit data serially
digitalWrite(latchPin,LOW);
//while the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
for (int i = 0; i < num_registers; i++){
button_states[i] = shiftIn(dataPin, clockPin);
}
boolean something_in = false;
for (int i = 0; i < num_registers; i++){
something_in = translate_buttons_2_keys(button_states[i], letters[i]);
if (something_in) last_key_press = millis();
}
digitalWrite(latchPin,HIGH);
}
boolean translate_buttons_2_keys(byte buttons, char* keys){
for(int i = 0 ; i < 8; i++){
if ( (1 << i) & buttons ){
cur_char = keys[i];
Keyboard.print(keys[i]);
char_index++;
return true;
}
}
return false;
}
byte shiftIn(int myDataPin, int myClockPin) {
int i;
int temp = 0;
int pinState;
byte myDataIn = 0;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(20);
temp = digitalRead(myDataPin);
if (temp) {
pinState = 1;
myDataIn = myDataIn | (1 << i);
}
else {
pinState = 0;
}
digitalWrite(myClockPin, 1);
}
return myDataIn;
}
but I want to emulate the SHIFT key (or other non-character keys) - so while this key is pressed, in combination with these lower case letters, the corresponding upper case (or a punctuation) character to appear.
I tried to use byte instead of char (so I can put the hexa code for the Left Shift key) but it returned me random characters...
I managed to do this using individual digital pins:
#include <Keyboard.h>
#define KEY_LEFT_SHIFT 0x81
int buttonPin = 2;
int buttonPin2 = 3;
void setup()
{
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode(buttonPin2, INPUT);
digitalWrite(buttonPin2, HIGH);
}
void loop()
{
if (digitalRead(buttonPin) == 0)
{
Keyboard.press(KEY_LEFT_SHIFT);
delay(250);
}
if (digitalRead(buttonPin2) == 0)
{
Keyboard.press('n');
Keyboard.release('n');
delay(250);
}
if(digitalRead(buttonPin)&&digitalRead(buttonPin2)==true)
{
Keyboard.releaseAll();
}
}
but can't figure out how to do it with the shift register. Please help me to find this solution, it really bugs me, couldn't find nothing on internet on this specific matter.
Is even possible to have combination of keys (multi-press) by using the CD4021BE shift register?