Just got a keypad of 16 capacitive buttons and finally got it working over a 2-wire system. I don't think the way I did it is at all close to the best possible way to do it, the array bit-at-which-position referencing seems quite roundabout. It spits out a bit per button, active low, so a string (using the term colloquially) of 16 1's is nothing pressed, each button pressed changes its position to 0.
I've only written enough to make some button affect one LED just as a marker. As shown, keypad button 10 toggles LED on and off.
I see that it is possible to move all of this business to an interrupt so that it doesn't have to constantly poll the keypad for changes. I tried that one code by tomschlitz or something like that for interrupt but couldn't get it working since it as written for a Leonardo but switching around pins to the Uno's 2/3 got no response.
So, here's the working code I made, I've never used an interrupt other than for a single button that keeps an LED on as an exercise. I need help understanding what parts become a function, what parts move to setup, how I can still have the Serial.print since it shouldn't be in the interrupt, and what my trigger would be since there's 16 buttons instead of 1. Need help learning how to make this suck less, basically.
/*
16-key Capacitive Keypad (type TTP229-B)
Jumpered "3", "4", and "5" on keypad (TP2, TP3, and TP4). Jumpers ground the pins through on-board 1MOhm resistor.
"3" for 16-key function
"4" and "5" for 'All multikeys: one group (16 keys)'
VCC - 5V
GND - GND
SCL - D2
SDO - D3
*/
#define clockPin 2
#define dataPin 3
#define ledPin 11
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, HIGH);
pinMode(dataPin, INPUT);
}
void loop() {
byte inputArray[16];
for (byte i = 0; i < 16; i++){
digitalWrite(clockPin, LOW);
byte databit = digitalRead(dataPin);
digitalWrite(clockPin, HIGH);
inputArray[i] = databit;
Serial.print(inputArray[i]);
}
Serial.println();
/*
//Begin Momentary
if (inputArray[2] == 0)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
//End Momentary
*/
//Begin Toggle
byte static prevState = 1;
byte static state = 1;
if (inputArray[9] == 0 && inputArray[9] != prevState) {
digitalWrite(ledPin, state);
state = !state;
}
prevState = inputArray[9];
//End Toggle
} //End loop