Hi,
maybe someone can help me with this.
I have a code for an mpr121 generating sounds from 8 sensor pads connected with an Arduino and a loudspeaker.
I bought an openlog and want to change this code now, so the signal of the sensors pad of the mpr121 will directly write on the sd card of the openlog.
I probably need to change the PWM value for generating the sound and simply write the message then by logging with serial.print() connecting openlog to an arduino pro mini interface. But i am not clear how to do this practically. Maybe someone can give me help with this. Enclosed below please find the code.
/*
MPR121 capacitive keyboard
Based on http://www.prizepony.us/2010/10/using-a-mpr121-with-arduino/
Tested on an Arduino Mega
*/
#include <Wire.h> // Wiring two-wire library for I2C
#include "mpr121.h" // register definitions
int notes[]={239,213,190,179,159,142,127,119};
char touch_previous;
void setup()
{
Serial.begin(9600);
// PWM SETUP
//http://www.arcfn.com/2009/07/secrets-of-arduino-pwm.html
//Varying the timer top limit: phase-correct PWM
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS22);
OCR2B = 10;
// MPR121 CONFIG
Wire.begin();
mpr121QuickConfig();
touch_previous=0;
}
void loop()
{
char buffer[32];
int i=0;
int note=0;
Wire.beginTransmission(0x5A);
Wire.send(0);
Wire.requestFrom(0x5A, 18);
while(Wire.available())
{
buffer[i] = Wire.receive();
i++;
}
Wire.endTransmission();
if (buffer[0] != touch_previous){
touch_previous=buffer[0];
Serial.print("TOUCH STATUS ");
Serial.println(buffer[0], DEC);
note=0;
for(i=0;i<8;i++){
if ((buffer[0]>>i)&1){
note=i;
}
}
if (buffer[0]){
OCR2A = notes[note];
}else{
OCR2A = 0;
}
}
}
char mpr121Read(unsigned char address)
{
char data;
Wire.beginTransmission(0x5A); // begin communication with the MPR121 on I2C address 0x5A
Wire.send(address); // sets the register pointer
Wire.requestFrom(0x5A, 1); // request for the MPR121 to send you a single byte
// check to see if we've received the byte over I2C
if(1 <= Wire.available())
{
data = Wire.receive();
}
Wire.endTransmission(); // ends communication
return data; // return the received data
}
void mpr121Write(unsigned char address, unsigned char data)
{
Wire.beginTransmission(0x5A); // begin communication with the MPR121 on I2C address 0x5A
Wire.send(address); // sets the register pointer
Wire.send(data); // sends data to be stored
Wire.endTransmission(); // ends communication
}
// MPR121 Quick Config
// This will configure all registers as described in AN3944
// Input: none
// Output: none
void mpr121QuickConfig(void)
{
// Section A
// This group controls filtering when data is > baseline.
mpr121Write(MHD_R, 0x01);
mpr121Write(NHD_R, 0x01);
mpr121Write(NCL_R, 0x00);
mpr121Write(FDL_R, 0x00);
// Section B
// This group controls filtering when data is < baseline.
mpr121Write(MHD_F, 0x01);
mpr121Write(NHD_F, 0x01);
mpr121Write(NCL_F, 0xFF);
mpr121Write(FDL_F, 0x02);
// Section C
// This group sets touch and release thresholds for each electrode
mpr121Write(ELE0_T, TOU_THRESH);
mpr121Write(ELE0_R, REL_THRESH);
mpr121Write(ELE1_T, TOU_THRESH);
mpr121Write(ELE1_R, REL_THRESH);
mpr121Write(ELE2_T, TOU_THRESH);
mpr121Write(ELE2_R, REL_THRESH);
mpr121Write(ELE3_T, TOU_THRESH);
mpr121Write(ELE3_R, REL_THRESH);
mpr121Write(ELE4_T, TOU_THRESH);
mpr121Write(ELE4_R, REL_THRESH);
mpr121Write(ELE5_T, TOU_THRESH);
mpr121Write(ELE5_R, REL_THRESH);
mpr121Write(ELE6_T, TOU_THRESH);
mpr121Write(ELE6_R, REL_THRESH);
mpr121Write(ELE7_T, TOU_THRESH);
mpr121Write(ELE7_R, REL_THRESH);
mpr121Write(ELE8_T, TOU_THRESH);
mpr121Write(ELE8_R, REL_THRESH);
mpr121Write(ELE9_T, TOU_THRESH);
mpr121Write(ELE9_R, REL_THRESH);
mpr121Write(ELE10_T, TOU_THRESH);
mpr121Write(ELE10_R, REL_THRESH);
mpr121Write(ELE11_T, TOU_THRESH);
mpr121Write(ELE11_R, REL_THRESH);
// Section D
// Set the Filter Configuration
// Set ESI2
//mpr121Write(FIL_CFG, 0x04);
mpr121Write(FIL_CFG, 0x00);
// Section E
// Electrode Configuration
// Enable 6 Electrodes and set to run mode
// Set ELE_CFG to 0x00 to return to standby mode
mpr121Write(ELE_CFG, 0x0C); // Enables all 12 Electrodes
//mpr121Write(ELE_CFG, 0x06); // Enable first 6 electrodes
// Section F
// Enable Auto Config and auto Reconfig
mpr121Write(ATO_CFG0, 0x0B);
mpr121Write(ATO_CFGU, 0xC9); // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V
mpr121Write(ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
mpr121Write(ATO_CFGT, 0xB5); // Target = 0.9*USL = 0xB5 @3.3V
}
/*
MPR121.h
*/
// MPR121 Register Defines
#define MHD_R 0x2B
#define NHD_R 0x2C
#define NCL_R 0x2D
#define FDL_R 0x2E
#define MHD_F 0x2F
#define NHD_F 0x30
#define NCL_F 0x31
#define FDL_F 0x32
#define ELE0_T 0x41
#define ELE0_R 0x42
#define ELE1_T 0x43
#define ELE1_R 0x44
#define ELE2_T 0x45
#define ELE2_R 0x46
#define ELE3_T 0x47
#define ELE3_R 0x48
#define ELE4_T 0x49
#define ELE4_R 0x4A
#define ELE5_T 0x4B
#define ELE5_R 0x4C
#define ELE6_T 0x4D
#define ELE6_R 0x4E
#define ELE7_T 0x4F
#define ELE7_R 0x50
#define ELE8_T 0x51
#define ELE8_R 0x52
#define ELE9_T 0x53
#define ELE9_R 0x54
#define ELE10_T 0x55
#define ELE10_R 0x56
#define ELE11_T 0x57
#define ELE11_R 0x58
#define FIL_CFG 0x5D
#define ELE_CFG 0x5E
#define GPIO_CTRL0 0x73
#define GPIO_CTRL1 0x74
#define GPIO_DATA 0x75
#define GPIO_DIR 0x76
#define GPIO_EN 0x77
#define GPIO_SET 0x78
#define GPIO_CLEAR 0x79
#define GPIO_TOGGLE 0x7A
#define ATO_CFG0 0x7B
#define ATO_CFGU 0x7D
#define ATO_CFGL 0x7E
#define ATO_CFGT 0x7F
// Global Constants
#define TOU_THRESH 0x05
#define REL_THRESH 0x02