Greetings
I've been working on a project for awhile to have 32 analog inputs into my pc. At first I was going to do this with the keyboard.h library and just have it type a letter. I later found the joystick.h library and I am trying that route. I have four shift registers each with 8 inputs. the first shift register is for inputs 1-8, second register is 9-16, etc. When I was using the keyboard library, input 1 worked as designed. Now that I moved it to joystick.h the first input isn't working. I went back to keyboard.h and it isn't work now either. So I moved on. Physical input 2 is now showing up in the serial monitor as input 1. Physical input 3 is showing as 2 and so on. So that means when I get to input 25 it shows as input 24. When I test input 26, nothing happens. I then created a fifth variable because I wasn't sure if there was room in the byte because everything seem to have shifted. That hasn't seemed to make a difference either.
Here are my questions. What have I don't incorrectly that the fourth shift register isn't reading any inputs? Why does physical input 1 no longer work? What can I do to make this code better?
//*************************************** Libraries ***************************************************
#include <Keyboard.h>
//define pins for shift registers
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;
byte switchVar1 = 0;
byte switchVar2 = 0;
byte switchVar3 = 0;
byte switchVar4 = 0;
byte switchVar5 = 0;
byte variable = 0;
int duration = 150;
void setup() {
Serial.begin(9600);
//define pin modes
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
Keyboard.begin();
}
void loop() {
//Pulse the latch pin:
//set it to 1 to collect parallel data
digitalWrite(latchPin, 1);
//set it to 1 to collect parallel data, wait
delayMicroseconds(20);
//set it to 0 to transmit data serially
digitalWrite(latchPin, 0);
//while the shift register is in serial mode collect each shift register into a byte
//the register attached to the chip comes in first
switchVar1 = shiftIn(dataPin, clockPin,LSBFIRST);
switchVar2 = shiftIn(dataPin, clockPin,MSBFIRST);
switchVar3 = shiftIn(dataPin, clockPin,MSBFIRST);
switchVar4 = shiftIn(dataPin, clockPin,MSBFIRST);
switchVar5 = shiftIn(dataPin, clockPin,MSBFIRST);
/*
if (switchVar1 == B10000000) {
Serial.print("Button 1 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B1000000) {
Serial.print("Button 2 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B100000) {
Serial.print("Button 3 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B10000) {
Serial.print("Button 4 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B1000) {
Serial.print("Button 5 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B100) {
Serial.print("Button 6 pressed --> ");
Serial.println(switchVar1, BIN);
}
if (switchVar1 == B10) {
Serial.print("Button 7 pressed --> ");
Serial.println(switchVar1, BIN);
//Keyboard.write('R');
}
*/
//if (switchVar1 == B1) {
Serial.print("Button 8 pressed --> ");
Serial.println(switchVar1, BIN);
//Keyboard.write('D');
//}
//if (switchVar2 == B10000000) {
Serial.print("Button 9 pressed --> ");
Serial.println(switchVar2, BIN);
//}
//if (switchVar2 == B1000000) {
Serial.print("Button 10 pressed --> ");
Serial.println(switchVar2, BIN);
//}
//if (switchVar2 == B100000) {
Serial.print("Button 11 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('k');
//}
//if (switchVar2 == B10000) {
Serial.print("Button 12 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('l');
//}
//if (switchVar2 == B1000) {
Serial.print("Button 13 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('m');
//}
//if (switchVar2 == B100) {
Serial.print("Button 14 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('n');
//}
//if (switchVar2 == B10) {
Serial.print("Button 15 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('o');
//}
//if (switchVar2 == B1) {
Serial.print("Button 16 pressed --> ");
Serial.println(switchVar2, BIN);
//Keyboard.write('p');
//}
//if (switchVar3 == B10000000) {
Serial.print("Button 17 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('q');
//}
//if (switchVar3 == B1000000) {
Serial.print("Button 18 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('r');
//}
//if (switchVar3 == B100000) {
Serial.print("Button 19 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('s');
//}
//if (switchVar3 == B10000) {
Serial.print("Button 20 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('t');
//}
//if (switchVar3 == B1000) {
Serial.print("Button 21 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('u');
//}
//if (switchVar3 == B100) {
Serial.print("Button 22 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('v');
//}
//if (switchVar3 == B10) {
Serial.print("Button 23 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('w');
//}
//if (switchVar3 == B1) {
Serial.print("Button 24 pressed --> ");
Serial.println(switchVar3, BIN);
//Keyboard.write('x');
//}
//if (switchVar4 == B10000000) {
Serial.print("Button 25 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('y');
//}
//if (switchVar4 == B1000000) {
Serial.print("Button 26 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('z');
//}
//if (switchVar4 == B100000) {
Serial.print("Button 27 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('1');
//}
//if (switchVar4 == B10000) {
Serial.print("Button 28 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('2');
//}
//if (switchVar4 == B1000) {
Serial.print("Button 29 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('3');
//Joystick.setButton (31,1);
//}
//if (switchVar4 == B100) {
Serial.print("Button 30 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('4');
//}
//if (switchVar4 == B10) {
Serial.print("Button 31 pressed --> ");
Serial.println(switchVar4, BIN);
//Keyboard.write('5');
//}
//if (switchVar4 == B1) {
Serial.print("Button 32 pressed --> ");
Serial.println(switchVar4, BIN); //used for debugging
//Keyboard.write('6');
//}
Serial.print("Button 33 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 34 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 35 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 36 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 37 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 38 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 39 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.print("Button 40 pressed --> ");
Serial.println(switchVar5, BIN); //used for debugging
Serial.println();
Serial.println();
delay(2500);
}
Thank you for your time and help.