I added the initialisation code below, and now I always get back 4095 no matter if anything is touching the sensors. At least I’m not getting fluctuating numbers now! I’m just wondering if having the sensors plugged into a breadboard via some headers would signify a touch and therefore skewing everything.
`// MPR121
local MHD_R=0x2B;
local NHD_R=0x2C;
local NCL_R=0x2D;
local FDL_R=0x2E;
local MHD_F=0x2F;
local NHD_F=0x30;
local NCL_F=0x31;
local FDL_F=0x32;
local ELE0_T =0x41;
local ELE0_R =0x42;
local ELE1_T =0x43;
local ELE1_R =0x44;
local ELE2_T =0x45;
local ELE2_R =0x46;
local ELE3_T =0x47;
local ELE3_R =0x48;
local ELE4_T =0x49;
local ELE4_R =0x4A;
local ELE5_T =0x4B;
local ELE5_R =0x4C;
local ELE6_T =0x4D;
local ELE6_R =0x4E;
local ELE7_T =0x4F;
local ELE7_R =0x50;
local ELE8_T =0x51;
local ELE8_R =0x52;
local ELE9_T =0x53;
local ELE9_R =0x54;
local ELE10_T =0x55;
local ELE10_R =0x56;
local ELE11_T =0x57;
local ELE11_R =0x58;
local FIL_CFG =0x5D;
local ELE_CFG =0x5E;
local GPIO_CTRL0 = 0x73;
local GPIO_CTRL1 = 0x74;
local GPIO_DATA = 0x75;
local GPIO_DIR = 0x76;
local GPIO_EN = 0x77;
local GPIO_SET = 0x78;
local GPIO_CLEAR = 0x79;
local GPIO_TOGGLE = 0x7A;
local ATO_CFG0 = 0x7B;
local ATO_CFGU = 0x7D;
local ATO_CFGL = 0x7E;
local ATO_CFGT = 0x7F;
local TOU_THRESH = 0x06;
local REL_THRESH = 0x0A;
hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
local alsAddr = (0x5A<<1);
function write(register, data)
{
local blah = hardware.i2c89.write(alsAddr, format("%c%c", register, data));
server.log(blah);
}
function setupMPR121() {
write(ELE_CFG, 0x00);
// Section A - Controls filtering when data is > baseline.
write(MHD_R, 0x01);
write(NHD_R, 0x01);
write(NCL_R, 0x00);
write(FDL_R, 0x00);
// Section B - Controls filtering when data is < baseline.
write(MHD_F, 0x01);
write(NHD_F, 0x01);
write(NCL_F, 0xFF);
write(FDL_F, 0x02);
// Section C - Sets touch and release thresholds for each electrode
write(ELE0_T, TOU_THRESH);
write(ELE0_R, REL_THRESH);
write(ELE1_T, TOU_THRESH);
write(ELE1_R, REL_THRESH);
write(ELE2_T, TOU_THRESH);
write(ELE2_R, REL_THRESH);
write(ELE3_T, TOU_THRESH);
write(ELE3_R, REL_THRESH);
write(ELE4_T, TOU_THRESH);
write(ELE4_R, REL_THRESH);
write(ELE5_T, TOU_THRESH);
write(ELE5_R, REL_THRESH);
write(ELE6_T, TOU_THRESH);
write(ELE6_R, REL_THRESH);
write(ELE7_T, TOU_THRESH);
write(ELE7_R, REL_THRESH);
write(ELE8_T, TOU_THRESH);
write(ELE8_R, REL_THRESH);
write(ELE9_T, TOU_THRESH);
write(ELE9_R, REL_THRESH);
write(ELE10_T, TOU_THRESH);
write(ELE10_R, REL_THRESH);
write(ELE11_T, TOU_THRESH);
write(ELE11_R, REL_THRESH);
// Section D
// Set the Filter Configuration
// Set ESI2
write(FIL_CFG, 0x04);
// Section E
// Electrode Configuration
// Set ELE_CFG to 0x00 to return to standby mode
write(ELE_CFG, 0x0C); // Enables all 12 Electrodes
write(ELE_CFG, 0x0C);
}
function readCap() {
hardware.i2c89.write(alsAddr, “\x00”);
local result = hardware.i2c89.read(alsAddr, “”, 2);
if (result != null) {
//server.log(result[1]);
local bits = (result[1]<<8)|result[0];
server.log(bits);
local key_code = 0;
for (local i=0; i < 12; i++){
if (((bits>>i) & 0x01)==1) {
key_code=i+1;
}
}
}
imp.wakeup(0.5, readCap);
}`