I've seen some discussions here from some years ago about similar projects. I based myself on them but it doesnt seam to work.

The render.cpp and the _main.pd files are posted on github (https://github.com/ArianaMarta/music-game/).

I'm Just using one MPR121 sensor at the moment, and it works when i use the example code so i believe the wiring is correct. I also believe that the pd patch is correct.

What happens is that when i touch the sensor the red LED is constantly on, which means that it gets stuck somewhere so i believe the problem is in the render.cpp file.

Here is the render.cpp:

#include <Bela.h>
#include <cmath>
#include "I2C_MPR121.h"
#include <libraries/libpd/libpd.h>


// How many pins there are
#define NUM_TOUCH_PINS 12

// Define this to print data to terminal
#undef DEBUG_MPR121

// Change this to change how often the MPR121 is read (in Hz)
int readInterval = 50;

// Change this threshold to set the minimum amount of touch
int threshold = 40;

// This array holds the continuous sensor values
int sensorValue[NUM_TOUCH_PINS];


// ---- internal stuff -- do not change -----

I2C_MPR121 mpr121;			// Object to handle MPR121 sensing
AuxiliaryTask i2cTask;		// Auxiliary task to read I2C

int readCount = 0;			// How long until we read again...
int readIntervalSamples = 0; // How many samples between reads

void readMPR121(void*);


bool setup(BelaContext *context, void *userData)
{
	if(!mpr121.begin(1, 0x5A)) {
		rt_printf("Error initialising MPR121\n");
		return false;
	}

	i2cTask = Bela_createAuxiliaryTask(readMPR121, 50, "bela-mpr121");
	readIntervalSamples = context->audioSampleRate / readInterval;
	
	
	//initialize libpd. This clears the search path
	libpd_init();
	//libpd_start_message(NUM_TOUCH_PINS); //get data from all 8 MPR121 pins
	//libpd_add_float(1.0f);
	//libpd_finish_message("pd", "dsp");
	
	return true;
}

void render(BelaContext *context, void *userData)
{	
	libpd_start_message(12);
	for(int n = 0; n < 12; n++){
		libpd_add_float(sensorValue[n]);
	}
	libpd_finish_list("sensorValue");
	libpd_finish_message("pd", "dsp");
	

}

void cleanup(BelaContext *context, void *userData)
{ }

// Auxiliary task to read the I2C board
void readMPR121(void*)
{
	for(int i = 0; i < NUM_TOUCH_PINS; i++) {
		sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
		sensorValue[i] -= threshold;
		if(sensorValue[i] < 0)
			sensorValue[i] = 0;
#ifdef DEBUG_MPR121
		rt_printf("%d ", sensorValue[i]);
#endif
	}
#ifdef DEBUG_MPR121
	rt_printf("\n");
#endif

	// You can use this to read binary on/off touch state more easily
	//rt_printf("Touched: %x\n", mpr121.touched());
}

I'm short in time to finish this project. So if this way doesn't work i was thinking to get the values using arduino (cause that works) and then send them to bela somehow. Any Ideas as well?

Otherwise theres also the option of using 2 trill sensores connected to the same bela. I need exactly 64 capacitive touch inputs.

Thank you

What LED? The one on the Bela cape?

Your code is not running any Pd processing, you stripped out way too much of the c++ wrapper. Also, you shouldn't be sending to dsp, as that's a Pd internal receiver. You are also not running the readMPR121() function: only creating a task with it but not actually running it.

I recommend you update your board to the latest dev branch (see here) and then you can have a simple C++ file like this:

#include <Bela.h>
#include "I2C_MPR121.h"
#include <libraries/BelaLibpd/BelaLibpd.h>

I2C_MPR121 mpr121;
#define NUM_TOUCH_PINS 12
// This array holds the continuous sensor values
int sensorValue[NUM_TOUCH_PINS];
// Change this threshold to set the minimum amount of touch
int threshold = 40;
volatile int newReading = 0;

void readMPR121(void*)
{
	while(!Bela_stopRequested())
	{
		for(int i = 0; i < NUM_TOUCH_PINS; i++) {
			sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
			sensorValue[i] -= threshold;
			if(sensorValue[i] < 0)
				sensorValue[i] = 0;
		}
		newReading = true;
		usleep(100000);
	}
}

void Bela_userSettings(BelaInitSettings *settings)
{
	settings->uniformSampleRate = 1;
	settings->interleave = 0;
	settings->analogOutputsPersist = 0;
}

bool setup(BelaContext *context, void *userData)
{
	if(!BelaLibpd_setup(context, userData, {}))
		return false;
	if(!mpr121.begin(1, 0x5A)) {
		fprintf(stderr, "Error initialising MPR121\n");
		return false;
	}
	Bela_runAuxiliaryTask(readMPR121);
	return true;
}

void render(BelaContext *context, void *userData)
{
	if(newReading)
	{
		newReading = false;
		libpd_start_message(NUM_TOUCH_PINS);
		for(size_t n = 0; n < NUM_TOUCH_PINS; ++n)
			libpd_add_float(sensorValue[n]);
		libpd_finish_list("bela_mpr121");
	}
	BelaLibpd_render(context, userData);
}

void cleanup(BelaContext *context, void *userData)
{
	BelaLibpd_cleanup(context, userData);
}

and the Pd file can be:

[receive bela_mpr121]
|
[print]

The LED i mean is the one from the MPR121.

I'm not being able to update the bela. I'm trying to download the zip file from git but its always canceled. Is there a way you can send the zip file here?

Because i tryed the code you just send and is giving errors in the "BelaLibpd" library which i guess is because i dont have the software updated.

Thank you

I think i manage to update bela and i still have the error message about the " #include <libraries/BelaLibpd/BelaLibpd.h>" library