Skip to content

Instantly share code, notes, and snippets.

@stefandz
Created February 19, 2015 11:12
Show Gist options
  • Save stefandz/4436aff87c5b5f5f9141 to your computer and use it in GitHub Desktop.
Save stefandz/4436aff87c5b5f5f9141 to your computer and use it in GitHub Desktop.
Bare Conductive LED controller
/*******************************************************************************
Bare Conductive LED controller
------------------------------
Touch_LEDs.ino - touch triggered LED control - maps each electrode to a digital
output and toggles it on and off.
Based on code by Jim Lindblom and plenty of inspiration from the Freescale
Semiconductor datasheets and application notes.
Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0
Unported License (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
// compiler error handling
//#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
// maps electrode 0 to digital 0, electrode 2 to digital 1, electrode 3 to digital 10 and so on...
// A0..A5 are the analogue input pins, used as digital outputs in this example
const int pinMap[12] = {0, 1, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};
void setup(){
Serial.begin(57600);
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
for(int i=firstPin; i<=lastPin; i++){
pinMode(pinMap[i], OUTPUT);
digitalWrite(pinMap[i], LOW);
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if(MPR121.getNumTouches()<=1){
for (int i=0; i < 12; i++){ // Check which electrodes were pressed
if(MPR121.isNewTouch(i)){
digitalWrite(pinMap[i], !digitalRead(pinMap[i]));
Serial.print("Toggling pin ");
Serial.println(pinMap[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment