TKU016CT-A100 Arduino Sample Code Guide
4×4 Keypad Arduino Example Programs
These example programs use the libraries included with the Arduino 1.6.7 IDE to send commands to the keypad module.
The demo performed by these sketches emulate interactive keys that activate the embedded buzzer when pressed.
Wiring Example
Using either an Arduino UNO or Arduino PRO:
- The only difference between the Arduino PRO and Arduino UNO wiring is the need for level shifting.
- The Arduino UNO uses 5V communication signals when connected via USB, so a level shifter is needed. (Example Sparkfun BOB-12009)
- The Arduino PRO is programmed with a TTL-232R-3V3 with the power (RED) wire cut. An external 3.3V power supply is required as well.
- A level shifter is not required for the Arduino PRO.
Sample Code (Arduino Sketches) for Arduino 1.6.7 IDE
- Copy the following sample code and upload it to your Arduino
Right-click in the code area to copy all of the source code to your clipboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
<span style="color: auto;"> /* * Establish a UART connection between an Arduino UNO/PRO and a Noritake 4x4 Keypad. * This example has example code for UART and key matrix scanning. * These difference code blocks are labeled in the loop() function. * This example uses the SoftwareSerial library from Arduino. * * Wiring Reference: * RX: pin 10 * TX: pin 11 * * KS0: pin 7 * KS1: pin 6 * KS2: pin 5 * KS3: pin 4 * * KD0: pin 9 * KD1: pin 8 * KD2: pin 3 * KD3: pin 2 * * Switching between UART and keyscan mode: * This example code starts in UART mode. * To use keyscan mode: * - Comment out the section labeled "UART Touch Algorithm" * - Comment out "touchSwitchReadSetting(0x02);" * - Uncomment "keyScan();" * - Make sure the keyscan select and data signals are connected as seen in the wiring reference file "UART_KeyScan - Schematic.pdf" * * The only difference between the Arduino PRO and Arduino UNO wiring is the need for level shifting and power requirements. * The Arduino UNO uses 5V communication signals when connected via USB, so a level shifter is needed (https://www.sparkfun.com/products/12009) * The Arduino PRO is programmed with a TTL-232R-3V3 with the power (RED) wire cut. An external 3.3V power supply is required as well. * A level shifter is not required for the Arduino PRO. * * ======== DISCLAIMER ======== * YOU MUST AGREE TO THIS TERMS AND CONDITIONS. THIS SOFTWARE IS * PROVIDED BY NORITAKE CO., INC "AS IS" AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR SORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================= */ </span><span style="color: auto;"> #include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); // RX, TX int valid = 0; int keyValid = 0; int touchValid = 0; byte switchVal; byte touchVal = 0; int countData = 0; int count = 0; byte prevSW = 0; //Keyscan ports int KS0 = 7; int KS1 = 6; int KS2 = 5; int KS3 = 4; int KD0 = 9; int KD1 = 8; int KD2 = 3; int KD3 = 2; void setup() { //Set up keyscan select pins pinMode(KS0, OUTPUT); pinMode(KS1, OUTPUT); pinMode(KS2, OUTPUT); pinMode(KS3, OUTPUT); //Set up keyscan data pins pinMode(KD0, INPUT); pinMode(KD1, INPUT); pinMode(KD2, INPUT); pinMode(KD3, INPUT); mySerial.begin(38400); // set the data rate for the SoftwareSerial port allLEDsON(); basicBuzzer(); touchSwitchReadSetting(0x02); //Set the read setting to automatic transmit mode 2 } void loop() { //Start UART Touch Algorithm if(mySerial.available()){ byte data = mySerial.read(); delayMicroseconds(300); //Check for a valid touch value and store it if(valid == 1 && keyValid == 1 && touchValid == 0){ touchValid = 1; touchVal = data; } //Check for a valid key value and store it else if(valid == 1 && keyValid == 0 && data <= 16 && data >= 0){ keyValid = 1; switchVal = data; } //Check for the touch header byte else if(data == 0x11 && valid == 0){ valid = 1; } //Turn LED OFF if a switch is being pressed if(touchVal == 0x01 && touchValid == 0x01){ LED_control_OFF(switchVal); basicBuzzer(); } //Turn LED ON if a switch is released else if(touchVal == 0 && touchValid == 0x01){ LED_control_ON(switchVal); } //Reset flags when a touch packet has been read if(touchValid == 1){ valid = 0; keyValid = 0; touchValid = 0; } } //End UART Touch Algorithm //Function used for keyscan mode //keyScan(); } /* * Turns all LEDs ON. * * @return none */ void allLEDsON(){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x20); mySerial.write(0xf0); mySerial.write(0x02); mySerial.write(0xff); mySerial.write(0xff); } /* * Turns a specific LED ON. * * @param LED The desired LED to turn ON. * @return none */ void LED_control_ON(byte LED){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x21); mySerial.write(LED); mySerial.write(0xf0); } /* * Turns a specific LED OFF. * * @param LED The desired LED to turn OFF. * @return none */ void LED_control_OFF(byte LED){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x21); mySerial.write(LED); mySerial.write((byte)0x00); } /* * Makes the default buzzer noise. * * @return none */ void basicBuzzer(){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x30); mySerial.write(0x03); } /* * Makes a buzzer noise based on input parameter. * * Pitch values can be found in the keypad's datasheet. * * @return none */ void buzzerPitch(byte pitch){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x31); mySerial.write(0x02); mySerial.write(pitch); mySerial.write(0x07); mySerial.write(0x40); mySerial.write(0x07); } /* * Set the desired touch switch setting. * * byte = 0x00: Manual transmit mode (Send only in response to read command) * byte = 0x01: Automatic transmit mode 1 (All touch switch status) * byte = 0x02: Automatic transmit mode 2 (Individual touch switch status) * default: byte = 0x00 or memory SW setting * * @return none */ void touchSwitchReadSetting(byte mode){ mySerial.write(0x1f); mySerial.write(0x4b); mySerial.write(0x18); mySerial.write(mode); } /* * Produce a keyscan signal and change the display based on key scan results. * * @return none */ void keyScan(){ byte column1 = columnScan(KS0); byte column2 = columnScan(KS1); byte column3 = columnScan(KS2); byte column4 = columnScan(KS3); //Check if a key in column 1 is pressed if(~column1 < 0xff){ if(~column1 & 0x01){ //SW1 is pressed Serial.println("SW1"); LED_control_OFF((byte)0x00); } else if((~column1 & 0x01) == 0x00){ LED_control_ON((byte)0x00); } if(~column1 & 0x02){ //SW2 is pressed Serial.println("SW2"); LED_control_OFF(0x01); } else if((~column1 & 0x02) == 0x00){ LED_control_ON(0x01); } if(~column1 & 0x04){ //SW3 is pressed Serial.println("SW3"); LED_control_OFF(0x02); } else if((~column1 & 0x04) == 0x00){ LED_control_ON(0x02); } if(~column1 & 0x08){ //SW4 is pressed Serial.println("SW4"); LED_control_OFF(0x03); } else if((~column1 & 0x08) == 0x00){ LED_control_ON(0x03); } } //Check if a key in column 2 is pressed if(~column2 < 0xff){ if(~column2 & 0x01){ //SW5 is pressed Serial.println("SW5"); LED_control_OFF(0x04); } else if((~column2 & 0x01) == 0x00){ LED_control_ON(0x04); } if(~column2 & 0x02){ //SW6 is pressed Serial.println("SW6"); LED_control_OFF(0x05); } else if((~column2 & 0x02) == 0x00){ LED_control_ON(0x05); } if(~column2 & 0x04){ //SW7 is pressed Serial.println("SW7"); LED_control_OFF(0x06); } else if((~column2 & 0x04) == 0x00){ LED_control_ON(0x06); } if(~column2 & 0x08){ //SW8 is pressed Serial.println("SW8"); LED_control_OFF(0x07); } else if((~column2 & 0x08) == 0x00){ LED_control_ON(0x07); } } //Check if a key in column 3 is pressed if(~column3 < 0xff){ if(~column3 & 0x01){ //SW9 is pressed Serial.println("SW9"); LED_control_OFF(0x08); } else if((~column3 & 0x01) == 0x00){ LED_control_ON(0x08); } if(~column3 & 0x02){ //SW10 is pressed Serial.println("SW10"); LED_control_OFF(0x09); } else if((~column3 & 0x02) == 0x00){ LED_control_ON(0x09); } if(~column3 & 0x04){ //SW11 is pressed Serial.println("SW11"); LED_control_OFF(0x0a); } else if((~column3 & 0x04) == 0x00){ LED_control_ON(0x0a); } if(~column3 & 0x08){ //SW12 is pressed Serial.println("SW12"); LED_control_OFF(0x0b); } else if((~column3 & 0x08) == 0x00){ LED_control_ON(0x0b); } } //Check if a key in column 4 is pressed if(~column4 < 0xff){ if(~column4 & 0x01){ //SW13 is pressed Serial.println("SW13"); LED_control_OFF(0x0c); } else if((~column4 & 0x01) == 0x00){ LED_control_ON(0x0c); } if(~column4 & 0x02){ //SW14 is pressed Serial.println("SW14"); LED_control_OFF(0x0d); } else if((~column4 & 0x02) == 0x00){ LED_control_ON(0x0d); } if(~column4 & 0x04){ //SW15 is pressed Serial.println("SW15"); LED_control_OFF(0x0e); } else if((~column4 & 0x04) == 0x00){ LED_control_ON(0x0e); } if(~column4 & 0x08){ //SW16 is pressed Serial.println("SW16"); LED_control_OFF(0x0f); } else if((~column4 & 0x08) == 0x00){ LED_control_ON(0x0f); } } } /* * Helper function for keyscan(). * Sends a keyscan signal to the module. * * @return scanData The byte representing the keyscan result for a column. */ byte columnScan(int column){ digitalWrite(column, LOW); byte scanData = readKD(); digitalWrite(column, HIGH); return scanData; } /* * Helper function for columnScan. * Reads in the four output values from the module and masks them into one byte. * * @return result The combined value for the given column scan. */ byte readKD(){ byte KD0_in = digitalRead(KD0); byte KD1_in = digitalRead(KD1); byte KD2_in = digitalRead(KD2); byte KD3_in = digitalRead(KD3); byte result = ((KD3_in << 3) | (KD2_in << 2) | (KD1_in << 1) | KD0_in); return result; } </span> |
Right-click in the code area to copy all of the source code to your clipboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
<span style="color: auto;"> /* * Establish an SPI connection between an Arduino UNO/PRO and a Noritake 4x4 Keypad. * This example uses the included SPI library from Arduino. * * Connection reference (relative to 4x4 keypad) * RESET: pin 5 * CSB: pin 7 * MOSI: pin 11 * MISO: pin 12 * SCK: pin 13 * * ======== DISCLAIMER ======== * YOU MUST AGREE TO THIS TERMS AND CONDITIONS. THIS SOFTWARE IS * PROVIDED BY NORITAKE CO., INC "AS IS" AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR SORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================= */ </span><span style="color: auto;"> #include <SPI.h> const int RESET = 5; const int CS = 7; const int BUFFERMAX = 63; byte touchResults[BUFFERMAX]; byte data; byte pos = 0; byte empty; void setup() { </span><span style="color: auto;">//Join SPI bus </span><span style="color: auto;"> SPI.begin(); SPI.setDataMode(SPI_MODE3); pinMode(RESET, OUTPUT); digitalWrite(RESET, HIGH); pinMode(CS, OUTPUT); </span><span style="color: auto;">//Reset keypad </span><span style="color: auto;"> digitalWrite(RESET, LOW); delay(1); digitalWrite(RESET, HIGH); delay(100); defaultBuzzer(); </span><span style="color: auto;">//Play buzzer after reset </span><span style="color: auto;"> allLEDsON(); touchSwitchReadSetting(0x02); </span><span style="color: auto;">//Set the read setting to automatic transmit mode 2 </span><span style="color: auto;"> } void loop() { int valid = 0; digitalWrite(CS, LOW); </span><span style="color: auto;">//Get status byte </span><span style="color: auto;"> SPI.transfer(0x58); delayMicroseconds(20); </span><span style="color: auto;">// Force 20us delay </span><span style="color: auto;"> byte maskedResult; byte statusResult = SPI.transfer(0x00); delayMicroseconds(20); </span><span style="color: auto;">// Force 20us delay </span> <span style="color: auto;">//Verify that the incoming data is valid </span><span style="color: auto;"> if((statusResult & 0x40) == 0){ maskedResult = statusResult & 0x3f; } digitalWrite(CS, HIGH); </span><span style="color: auto;">//End status byte read </span> <span style="color: auto;">//Get touch data </span><span style="color: auto;"> digitalWrite(CS, LOW); SPI.transfer(0x54); delayMicroseconds(20); </span><span style="color: auto;">// Force 20us delay </span><span style="color: auto;"> for(int i = 0; i <= maskedResult + 1; i++){ </span><span style="color: auto;">//Read data </span><span style="color: auto;"> data = SPI.transfer(0x00); delayMicroseconds(20); </span><span style="color: auto;">// Force 20us delay </span><span style="color: auto;"> if(valid){ touchResults[i] = data; } if(data == 0x11 && valid == 0){ i = 0; touchResults[i] = data; valid = 1; } empty = 0; } valid = 0; digitalWrite(CS, HIGH); </span><span style="color: auto;"> //End touch data read </span> <span style="color: auto;">//Parse touch data and manipulate buzzer and LEDs </span><span style="color: auto;"> while(!empty){ if(touchResults[pos] == 0x11 && touchResults[pos + 2] == 0x01){ defaultBuzzer(); LED_control_OFF(touchResults[pos + 1]); } if(touchResults[pos] == 0x11 && touchResults[pos + 2] == 0x00){ LED_control_ON(touchResults[pos + 1]); } if(touchResults[pos + 3] == 0x11){ pos += 3; empty = 0; } else { empty = 1; } } </span><span style="color: auto;">//Reset variables </span><span style="color: auto;"> pos = 0; touchResults[0] = 0; touchResults[1] = 0; touchResults[2] = 0; statusResult = 0; maskedResult = 0; } </span><span style="color: auto;"> /* * Turn all of the LEDs ON on the keypad. * * @return none */ </span><span style="color: auto;"> void allLEDsON(){ digitalWrite(CS, LOW); SPI.transfer(0x44); SPI.transfer(0x1f); SPI.transfer(0x4b); SPI.transfer(0x20); SPI.transfer(0xf0); SPI.transfer(0x02); SPI.transfer(0xff); SPI.transfer(0xff); digitalWrite(CS, HIGH); } </span><span style="color: auto;"> /* * Plays the default buzzer sound on the keypad. * * @return none */ </span><span style="color: auto;"> void defaultBuzzer(){ digitalWrite(CS, LOW); SPI.transfer(0x44); SPI.transfer(0x1f); SPI.transfer(0x4b); SPI.transfer(0x30); SPI.transfer(0x03); digitalWrite(CS, HIGH); } </span><span style="color: auto;"> /* * Turn an individal LED ON. * * @param LED The LED to turn ON. * @return none */ </span><span style="color: auto;"> void LED_control_ON(byte LED){ digitalWrite(CS, LOW); SPI.transfer(0x44); SPI.transfer(0x1f); SPI.transfer(0x4b); SPI.transfer(0x21); SPI.transfer(LED); SPI.transfer(0xf0); digitalWrite(CS, HIGH); } </span><span style="color: auto;"> /* * Turn an individual LED OFF. * * @param LED The LED to turn OFF. */ </span><span style="color: auto;"> void LED_control_OFF(byte LED){ digitalWrite(CS, LOW); SPI.transfer(0x44); SPI.transfer(0x1f); SPI.transfer(0x4b); SPI.transfer(0x21); SPI.transfer(LED); SPI.transfer(0x00); digitalWrite(CS, HIGH); } </span><span style="color: auto;"> /* * Set the touch switch read mode. * * mode = 0x00: Manual transmit mode (Send only in response to a read command) * mode = 0x01: Automatic transmit mode 1 (All touch-switch status) * mode = 0x02: Automatic transmit mode 2 (Individual touch-switch status) * * @param mode The desired touch switch read mode. * @return none */ </span><span style="color: auto;"> void touchSwitchReadSetting(byte mode){ digitalWrite(CS, LOW); SPI.transfer(0x44); SPI.transfer(0x1f); SPI.transfer(0x4b); SPI.transfer(0x18); SPI.transfer(mode); digitalWrite(CS, HIGH); } </span> |
Right-click in the code area to copy all of the source code to your clipboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
<span style="color: auto;"> /* * Establish an I2C connection between an Ardunio UNO/PRO and a Noritake 4x4 Keypad. * This example uses the included Wire library from Arduino. * * Arduino wiring reference: * SDA = A4 * SCL = A5 * * ======== DISCLAIMER ======== * YOU MUST AGREE TO THIS TERMS AND CONDITIONS. THIS SOFTWARE IS * PROVIDED BY NORITAKE CO., INC "AS IS" AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR SORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================= */ </span><span style="color: auto;"> #include <Wire.h> byte pos = 0; byte empty; const int BUFFERMAX = 63; byte address = 0x00; void setup() { Wire.begin(); </span><span style="color: auto;">//Join I2C bus </span><span style="color: auto;"> defaultBuzzer(); </span><span style="color: auto;">//Play a buzzer sound to signify program start. </span><span style="color: auto;"> allLEDsON(); touchSwitchReadSetting(0x02); </span><span style="color: auto;">//Set the read setting to automatic transmit mode 2 </span><span style="color: auto;"> } void loop() { int valid = 0; byte touchResults[BUFFERMAX]; </span><span style="color: auto;">//Array to store incoming touch data. </span><span style="color: auto;"> Wire.requestFrom(address, BUFFERMAX); </span><span style="color: auto;">//Check if data is ready to be transmitted. </span><span style="color: auto;"> while(Wire.available()){ byte data = Wire.read(); </span><span style="color: auto;">//Read data from the transmit buffer. </span> <span style="color: auto;">//Check if a header byte has been received. </span><span style="color: auto;"> if(valid){ touchResults[pos] = data; </span><span style="color: auto;">//Save incoming data </span><span style="color: auto;"> pos++; </span><span style="color: auto;">//Increment position </span><span style="color: auto;"> } </span><span style="color: auto;">//Check if a header byte is received. </span><span style="color: auto;"> if(data == 0x11 && valid == 0){ pos = 0; </span><span style="color: auto;">//Reset the array position </span><span style="color: auto;"> touchResults[pos] = data; </span><span style="color: auto;">//Save the header byte to the first array position </span><span style="color: auto;"> valid = 1; </span><span style="color: auto;">//Enable valid flag </span><span style="color: auto;"> pos++; </span><span style="color: auto;">//Increment position </span><span style="color: auto;"> } empty = 0; </span><span style="color: auto;">//Reset the array empty flag. </span><span style="color: auto;"> } pos = 0; </span><span style="color: auto;">//Reset position </span><span style="color: auto;"> while(!empty){ </span><span style="color: auto;">//Check if a switch has been touched </span><span style="color: auto;"> if(touchResults[pos] == 0x11 && touchResults[pos + 2] == 0x01){ defaultBuzzer(); LED_control_OFF(touchResults[pos + 1]); } </span><span style="color: auto;">//Check if a switch has been released </span><span style="color: auto;"> if(touchResults[pos] == 0x11 && touchResults[pos + 2] == 0x00){ LED_control_ON(touchResults[pos + 1]); } </span><span style="color: auto;">//Check if another set of touch data is present </span><span style="color: auto;"> if(touchResults[pos + 3] == 0x11){ pos += 3; empty = 0; </span><span style="color: auto;">//Ensure that the empty flag is clear </span><span style="color: auto;"> } else { empty = 1; </span><span style="color: auto;">//Put the empty flag high </span><span style="color: auto;"> } } pos = 0; </span><span style="color: auto;">//Reset position </span> <span style="color: auto;">//Reset the results array </span><span style="color: auto;"> for(int i = 0; i <= 20; i++){ touchResults[i] = 0x00; } } </span><span style="color: auto;"> /* * Turn all of the LEDs ON on the keypad. * * @return none */ </span><span style="color: auto;"> void allLEDsON(){ Wire.beginTransmission(address); </span><span style="color: auto;">// transmit to keypad </span><span style="color: auto;"> Wire.write(0x1f); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0x4b); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0x20); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0xf0); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0x02); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0xff); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.write(0xff); </span><span style="color: auto;">// sends one byte </span><span style="color: auto;"> Wire.endTransmission(); </span><span style="color: auto;">// stop transmitting </span><span style="color: auto;"> } </span><span style="color: auto;"> /* * Turn an individal LED ON. * * @param LED The LED to turn ON. * @return none */ </span><span style="color: auto;"> void LED_control_ON(byte LED){ Wire.beginTransmission(address); Wire.write(0x1f); Wire.write(0x4b); Wire.write(0x21); Wire.write(LED); Wire.write(0xf0); Wire.endTransmission(); } </span><span style="color: auto;"> /* * Turn an individual LED OFF. * * @param LED The LED to turn OFF. */ </span><span style="color: auto;"> void LED_control_OFF(byte LED){ Wire.beginTransmission(address); Wire.write(0x1f); Wire.write(0x4b); Wire.write(0x21); Wire.write(LED); Wire.write(0x00); Wire.endTransmission(); } </span><span style="color: auto;"> /* * Plays the default buzzer sound on the keypad. * * @return none */ </span><span style="color: auto;"> void defaultBuzzer(){ Wire.beginTransmission(address); Wire.write(0x1f); Wire.write(0x4b); Wire.write(0x30); Wire.write(0x03); Wire.endTransmission(); } </span><span style="color: auto;"> /* * Set the touch switch read mode. * * mode = 0x00: Manual transmit mode (Send only in response to a read command) * mode = 0x01: Automatic transmit mode 1 (All touch-switch status) * mode = 0x02: Automatic transmit mode 2 (Individual touch-switch status) * * @param mode The desired touch switch read mode. * @return none */ </span><span style="color: auto;"> void touchSwitchReadSetting(byte mode){ Wire.beginTransmission(address); Wire.write(0x1f); Wire.write(0x4b); Wire.write(0x18); Wire.write(mode); Wire.endTransmission(); } </span> |