Advertisement
talofer99

MPR121 - Simple Touch

May 31st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. /*******************************************************************************
  2.  
  3.  Bare Conductive MPR121 library
  4.  ------------------------------
  5.  
  6.  SimpleTouch.ino - simple MPR121 touch detection demo with serial output
  7.  
  8.  Based on code by Jim Lindblom and plenty of inspiration from the Freescale
  9.  Semiconductor datasheets and application notes.
  10.  
  11.  Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
  12.  
  13.  This work is licensed under a MIT license https://opensource.org/licenses/MIT
  14.  
  15.  Copyright (c) 2016, Bare Conductive
  16.  
  17.  Permission is hereby granted, free of charge, to any person obtaining a copy
  18.  of this software and associated documentation files (the "Software"), to deal
  19.  in the Software without restriction, including without limitation the rights
  20.  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21.  copies of the Software, and to permit persons to whom the Software is
  22.  furnished to do so, subject to the following conditions:
  23.  
  24.  The above copyright notice and this permission notice shall be included in all
  25.  copies or substantial portions of the Software.
  26.  
  27.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28.  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29.  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30.  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31.  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32.  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33.  SOFTWARE.
  34.  
  35. *******************************************************************************/
  36.  
  37. #include <MPR121.h>
  38. #include <Wire.h>
  39.  
  40. #define numElectrodes 12
  41.  
  42. void setup()
  43. {
  44.   Serial.begin(115200);
  45.   while(!Serial);  // only needed if you want serial feedback with the
  46.            // Arduino Leonardo or Bare Touch Board
  47.  
  48.   Wire.begin();
  49.  
  50.   // 0x5C is the MPR121 I2C address on the Bare Touch Board
  51.   if(!MPR121.begin(0x5A)){
  52.     Serial.println("error setting up MPR121");  
  53.     switch(MPR121.getError()){
  54.       case NO_ERROR:
  55.         Serial.println("no error");
  56.         break;  
  57.       case ADDRESS_UNKNOWN:
  58.         Serial.println("incorrect address");
  59.         break;
  60.       case READBACK_FAIL:
  61.         Serial.println("readback failure");
  62.         break;
  63.       case OVERCURRENT_FLAG:
  64.         Serial.println("overcurrent on REXT pin");
  65.         break;      
  66.       case OUT_OF_RANGE:
  67.         Serial.println("electrode out of range");
  68.         break;
  69.       case NOT_INITED:
  70.         Serial.println("not initialised");
  71.         break;
  72.       default:
  73.         Serial.println("unknown error");
  74.         break;      
  75.     }
  76.     while(1);
  77.   }
  78.  
  79.   // pin 4 is the MPR121 interrupt on the Bare Touch Board
  80.   MPR121.setInterruptPin(4);
  81.  
  82.   // this is the touch threshold - setting it low makes it more like a proximity trigger
  83.   // default value is 40 for touch
  84.   MPR121.setTouchThreshold(40);
  85.  
  86.   // this is the release threshold - must ALWAYS be smaller than the touch threshold
  87.   // default value is 20 for touch
  88.   MPR121.setReleaseThreshold(20);  
  89.  
  90.   // initial data update
  91.   MPR121.updateTouchData();
  92. }
  93.  
  94. void loop()
  95. {
  96.   if(MPR121.touchStatusChanged()){
  97.     MPR121.updateTouchData();
  98.     for(int i=0; i<numElectrodes; i++){
  99.       if(MPR121.isNewTouch(i)){
  100.         Serial.print("electrode ");
  101.         Serial.print(i, DEC);
  102.         Serial.println(" was just touched");  
  103.       } else if(MPR121.isNewRelease(i)){
  104.         Serial.print("electrode ");
  105.         Serial.print(i, DEC);
  106.         Serial.println(" was just released");  
  107.       }
  108.     }
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement