Hello,
I need to use more than one mpr121 sensors in my project.(12 Key Capacitive I2C Touch Sensor V2(MPR121) I changed the adress of the hardware, so they have different adresses. Both sensors and the changed adresses work when I use them individually. When both connected only one works. The one which is “nearer” to my seeeduino nano. Between the nano and the sensors I’m using the grove I2C hub (6 Port). I only need to use 20 spots, that’s why I used 12 on one sensor and 8 on the other.
Anyone has an idea what could be the problem? Thanks for you help!
Best,
fee
#include "Seeed_MPR121_driver.h"
#define touch_sensor_1_ch_count 12
#define touch_sensor_2_ch_count 8
#define touch_sensors_ch_count (touch_sensor_1_ch_count + touch_sensor_2_ch_count) //it will be 20!
Mpr121 touch_sensor_1(0x5C); //from 0 - touch_sensor_1_ch_count
Mpr121 touch_sensor_2(0x5B); //touch_sensor_1_ch_count - touch_sensors_ch_count
u16 touch_status[touch_sensors_ch_count]={false};
bool touch[touch_sensors_ch_count] = {false};
bool touch_changed[touch_sensors_ch_count]={false};
void setup()
{
Serial.begin(115200);
//initialize both touch sensor boards!
if( (touch_sensor_1.begin()<0) && (touch_sensor_2.begin()<0) )
{
//touch_sensor_1.set_debounce(0xF0);
touch_sensor_1.set_threshold(0x10);
touch_sensor_2.set_threshold(0x10);
Serial.println("Can't detect touch devices!!!!");
}
else
{
Serial.println("Touch sensors initialization is OK!");
}
delay(100);
}
void check_touch_sensor(Mpr121 &touch_sensor, int offset, int ch_count)
{
u16 result = 0;
u16 filtered_data_buf[ch_count]={0};
result = touch_sensor.check_status_register();
touch_sensor.get_filtered_reg_data(&result, filtered_data_buf);
for(int i=0; i<ch_count ;i++)
{
touch_changed[offset+i] = false;
if(result & (1<<i)) /*key i is pressed!!*/
{
if(0==touch_status[offset+i])
{
touch_status[offset+i]=1;
}
}
else
{
if(1==touch_status[i])
{
touch[offset+i] = !touch[offset+i];
touch_changed[offset+i] = true;
touch_status[i]=0;
}
}
}
}
void loop()
{
check_touch_sensor(touch_sensor_1, 0, touch_sensor_1_ch_count);
check_touch_sensor(touch_sensor_2, touch_sensor_1_ch_count, touch_sensor_2_ch_count);
for (int i = 0; i < touch_sensors_ch_count; i++ )
{
if(touch_changed[i]) //something changed
{
Serial.print("key ");
Serial.print(i);
Serial.print(" is ");
if(touch[i] == true)
{
Serial.print(" ON");
}
else
{
Serial.print(" OFF");
}
Serial.println();
//mark as read
touch_changed[i] = false;
}
}
delay(50);
}