Okay folks, I'm throwing myself on the mercy of the community. I am an UTTER newb when it comes to programming, and am doing my best to learn as I go.
I'm building a control panel for a simulator. Simple stuff, just need to hook up a small pile of buttons to a computer, where each one sends a keyboard press when used. Here's the basic code I've been working with:
const int potPin0 = 0;
const int potPin1 = 1;
const int momentaryPin1 = 2;
const int momentaryPin2 = 3;
const int momentaryPin3 = 4;
const int momentaryPin4 = 5;
const int momentaryPin5 = 6;
const int momentaryPin6 = 7;
const int momentaryPin7 = 8;
const int momentaryPin8 = 9;
const int momentaryPin9 = 10;
const int momentaryPin10 = 11;
const int momentaryPin11 = 12;
const int momentaryPin12 = 13;
int val = 0;
void setup() {
pinMode(momentaryPin1, INPUT_PULLUP);
pinMode(momentaryPin2, INPUT_PULLUP);
pinMode(momentaryPin3, INPUT_PULLUP);
pinMode(momentaryPin4, INPUT_PULLUP);
pinMode(momentaryPin5, INPUT_PULLUP);
pinMode(momentaryPin6, INPUT_PULLUP);
pinMode(momentaryPin7, INPUT_PULLUP);
pinMode(momentaryPin8, INPUT_PULLUP);
pinMode(momentaryPin9, INPUT_PULLUP);
pinMode(momentaryPin10, INPUT_PULLUP);
pinMode(momentaryPin11, INPUT_PULLUP);
pinMode(momentaryPin12, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//momentary switch reading
int buttonState1 = digitalRead(momentaryPin1);
if (buttonState1 == LOW) {
Keyboard.print("button 1");
delay(500);
}
//momentary switch reading
int buttonState2 = digitalRead(momentaryPin2);
if (buttonState2 == LOW) {
Keyboard.print("button 2");
delay(500);
}
//momentary switch reading
int buttonState3 = digitalRead(momentaryPin3);
if (buttonState3 == LOW) {
Keyboard.print("button 3");
delay(500);
}
//momentary switch reading
int buttonState4 = digitalRead(momentaryPin4);
if (buttonState4 == LOW) {
Keyboard.print("button 4");
delay(500);
}
//momentary switch reading
int buttonState5 = digitalRead(momentaryPin5);
if (buttonState5 == LOW) {
Keyboard.print("button 5");
delay(500);
}
//momentary switch reading
int buttonState6 = digitalRead(momentaryPin6);
if (buttonState6 == LOW) {
Keyboard.print("button 6");
delay(500);
}
//momentary switch reading
int buttonState7 = digitalRead(momentaryPin7);
if (buttonState7 == LOW) {
Keyboard.print("button 7");
delay(500);
}
//momentary switch reading
int buttonState8 = digitalRead(momentaryPin8);
if (buttonState8 == LOW) {
Keyboard.print("button 8");
delay(500);
}
//momentary switch reading
int buttonState9 = digitalRead(momentaryPin9);
if (buttonState9 == LOW) {
Keyboard.print("button 9");
delay(500);
}
//momentary switch reading
int buttonState10 = digitalRead(momentaryPin10);
if (buttonState10 == LOW) {
Keyboard.print("button 10");
delay(500);
}
//momentary switch reading
int buttonState11 = digitalRead(momentaryPin11);
if (buttonState11 == LOW) {
Keyboard.print("button 12");
delay(500);
}
//momentary switch reading
int buttonState12 = digitalRead(momentaryPin12);
if (buttonState12 == LOW) {
Keyboard.print("button 13");
delay(500);
}
//pot reading
static int oldPotState0 = analogRead(potPin0);
val = analogRead(potPin0);
if (val > (oldPotState0 + 50) || val < (oldPotState0 - 50)) {
if (val < (oldPotState0 - 50)) {
Keyboard.println("pot1 -");
} else if (val > (oldPotState0 + 50)) {
Keyboard.println("pot1 +");
}
oldPotState0 = val;
}
//pot reading 2
static int oldPotState1 = analogRead(potPin1);
val = analogRead(potPin1);
if (val > (oldPotState1 + 50) || val < (oldPotState1 - 50)) {
if (val < (oldPotState1 - 50)) {
Keyboard.println("pot2 -");
} else if (val > (oldPotState1 + 50)) {
Keyboard.println("pot2 +");
}
oldPotState1 = val;
}
}
Initially, the plan was to have about a dozen buttons and a few potentiometers or so, but it's grown. To the point where I've now exceeded my pins. So I assumed using a shift register would do the job. So ordered up a couple of 74HC165 breakout boards from Sparkfun… and have hit an absolute brick wall in modifying the code. I've looked at the Arduino playground example until my eyes glazed over, but just don't seem to be getting it. I realize that most of you are reading this and thinking that I'm an absolute moron, that there's nothing easier, and the examples are crystal - and am sure they are. But… my head's just not wrapping around it.
So if anyone with a minute to spare could help me out, I'd massively appreciate it! ![]()