I had a bit of trouble with this week’s MPR121 but this means I spent a lot of time troubleshooting and it made me understand the code really well.

Eventually I found out the main issue is that the MPR121 I was using is broken which also means after troubleshooting, my conclusion was right.

So in my project this week, I was trying to wire these coins so that when you touch individual ones, the amount that they are show up. Then I tried to make the series ask for a random amount of money up to £5 and I would have 5 seconds to tap on the change to make up that amount. But I had trouble making the series ask for the random amount.

Here’s the video of what I managed to make:


#include <MPR121.h>
#include <Wire.h>

#define numElectrodes 12
//float payme;
//float pen;

void setup()
{
Serial.begin(9600);
while(!Serial); // only needed if you want serial feedback with the
// Arduino Leonardo or Bare Touch Board

Wire.begin();

// 0x5C is the MPR121 I2C address on the Bare Touch Board
if(!MPR121.begin(0x5A)){
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);
}

// pin 4 is the MPR121 interrupt on the Bare Touch Board
MPR121.setInterruptPin(4);

// this is the touch threshold – setting it low makes it more like a proximity trigger
// default value is 40 for touch
MPR121.setTouchThreshold(40);

// this is the release threshold – must ALWAYS be smaller than the touch threshold
// default value is 20 for touch
MPR121.setReleaseThreshold(20);

// initial data update
MPR121.updateTouchData();
Serial.println(“Pay me £2.23”);
}

void loop()
{

if(MPR121.touchStatusChanged()){

MPR121.updateTouchData();
// for(int i=0; i<numElectrodes; i++){
// if(MPR121.isNewTouch(i)){
// Serial.print(“electrode “);
// Serial.print(i, DEC);
// Serial.println(” was just touched”);
// } else if(MPR121.isNewRelease(i)){
// Serial.print(“electrode “);
// Serial.print(i, DEC);
// Serial.println(” was just released”);
// }

if (MPR121.isNewTouch(1)){
Serial.print(“0.50”);
}
else if (MPR121.isNewTouch(3)){
Serial.print(“0.01”);
}
else if (MPR121.isNewTouch(5)){
Serial.print(“0.10”);
}
else if (MPR121.isNewTouch(7)){
Serial.print(“0.05”);
}
}
}