Skip to content

Instantly share code, notes, and snippets.

@stefandz
Created April 15, 2015 17:04
Show Gist options
  • Save stefandz/1b9041a6b3ebfae29f45 to your computer and use it in GitHub Desktop.
Save stefandz/1b9041a6b3ebfae29f45 to your computer and use it in GitHub Desktop.
Bare Conductive Proximity to LED intensity demo
// maps intensity of the onboard D13 LED on the Touch Board
// to the proximity of a hand to a 85mmx85mm electrode
// connected to electrode 0 on the board
#include <MPR121.h>
#include <Wire.h>
#define LOW_DIFF 0
#define HIGH_DIFF 50
#define filterWeight 0.3f
float lastProx = 0;
#define ELECTRODE 0
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
analogWrite(LED_BUILTIN, 0);
Serial.begin(9600);
//while(!Serial);
if(!MPR121.begin(0x5C)){
Serial.println("error setting up MPR121");
switch(MPR121.getError()){
case NO_ERROR:
Serial.println("no error");
break;
case ADDRESS_UNKNOWN:
Serial.println("incorrect address");
break;
case READBACK_FAIL:
Serial.println("readback failure");
break;
case OVERCURRENT_FLAG:
Serial.println("overcurrent on REXT pin");
break;
case OUT_OF_RANGE:
Serial.println("electrode out of range");
break;
case NOT_INITED:
Serial.println("not initialised");
break;
default:
Serial.println("unknown error");
break;
}
while(1);
}
}
void loop() {
MPR121.updateAll();
int reading = MPR121.getBaselineData(ELECTRODE)-MPR121.getFilteredData(ELECTRODE);
Serial.println(reading);
unsigned int prox = constrain(reading, LOW_DIFF, HIGH_DIFF);
prox = (sq(prox-LOW_DIFF)/(HIGH_DIFF-LOW_DIFF))+LOW_DIFF;
lastProx = (filterWeight*lastProx) + ((1-filterWeight)*(float)prox);
uint8_t thisOutput = (uint8_t)map(lastProx,LOW_DIFF,HIGH_DIFF,0,255);
analogWrite(LED_BUILTIN, thisOutput);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment