I am trying to store multiple numbers entered from a capacitive key pad (made by sparkfun).
They've supplied some starter code (does all of the i2c heavy lifting) from which I am trying to build off of for my project.
In essence, they have it set up to print one digit that corresponds to the number pressed and this works great. However, I would like to store multiple numbers (i.e. press "2,5" and I would like to store "25" as either an int, char or String - either way it will hopefully end up logged on an sdcard).
I would like the getNumber() function to return a value (i could then run the getNumber function inside a for loop that would store the values outputted by getNumber into an array). I am stuck because attachInterrupt() calls a function (in this case getNumber) but this function is NOT allowed to return anything. Is there anyway of storing this value? My best guess at this point is probably to write the value temporarily to EEPROM and then read it out in a second function that opens the sdwrite functionality. Is writing to memory a feasible solution or is there a 'better' way?
Here's the setup() and getNumber() functions:
void setup()
{
//make sure the interrupt pin is an input and pulled high
pinMode(irqPin, INPUT);
digitalWrite(irqPin, HIGH);//for testing purposes
pinMode(ledPin, OUTPUT);//configure serial out
Serial.begin(9600);// initalize I2C bus. Wiring lib not used.
i2cInit();// initialize mpr121
mpr121QuickConfig();// Create and interrupt to trigger when a button
// is hit, the IRQ pin goes low, and the function getNumber is run.
attachInterrupt(0,getNumber,LOW);// prints 'Ready...' when you can start hitting numbers
Serial.println("Ready...");
}void getNumber()
{
int touchNumber = 0;
uint16_t touchstatus;
char digits;touchstatus = getTouchStatus();
for (int j=0; j<12; j++) // Check how many electrodes were pressed
{
if ((touchstatus & (1<<j)))
touchNumber++;
}// check to see what touchNumber is equal to
Serial.println(touchNumber);if (touchNumber == 1)
{
if (touchstatus & (1<<SEVEN))
{
digits = '7';
}
else if (touchstatus & (1<<FOUR))
{
digits = '4';
}
else if (touchstatus & (1<<ONE))
{
digits = '1';
}
else if (touchstatus & (1<<EIGHT))
{
digits = '8';
}
else if (touchstatus & (1<<FIVE))
{
digits = '5';
}
else if (touchstatus & (1<<TWO))
{
digits = '2';
}
else if (touchstatus & (1<<NINE))
{
digits = '9';
}
else if (touchstatus & (1<<SIX))
{
digits = '6';
}
else if (touchstatus & (1<<THREE))
{
digits = '3';
}
//Serial.println(touchstatus, HEX);
Serial.println(digits);
//Serial.println("end");
}
//do nothing if more than one button is pressed, or if all are released
else if (touchNumber == 0)
Serial.println("touch number is 0");
else
Serial.print(touchNumber); // this should be the same thing as saying return touch; (last line of script)}
/* getTouchStatus() will return a 16-bit value that relates the
current touched status of each button. The LSB represents electrodes
0-7 (bit 0 = electrode 0), and the lowest 4 bits of the MSB
represent electrods 8-11. A 1 means a button is being touched.*/
int getTouchStatus()
{
int touch;touch = mpr121Read(0x01) << 8;
touch |= mpr121Read(0x00); //note: "x |= y" is the same as "x = x | y"Serial.println(touch) //for debugging
return touch;
}