/*
ADOPTED for R89m library MPR121Button_Fast example. This shows a basic example of how to receive events from electrodes on an MPR121 Capactive Touch IC, but using a local variable to reduce the amount of requests over the i2c bus
*/
#include <Button.h>
#include <ButtonEventCallback.h>
#include <MPR121Button.h>
#include <Adafruit_MPR121.h> // https://github.com/adafruit/Adafruit_MPR121_Library
#include <Wire.h>
// Attach the MPR121's IRQ pin to digital pin 7
const int PIN_TOUCH_IRQ = 7;
char touch[6] = {'A','B','C','D','E','F'};
int long ENDtime = 0;
int long STARTtime;
String code;
// Create an instance of Adafruit_MPR121 to communicate with your IC via i2C
Adafruit_MPR121 touchSensor = Adafruit_MPR121();
// Create a few instances of MPRButton reading electrodes 1, 2, 3, 4 and 5
//MPR121Button button0 = MPR121Button(touchSensor, 0);
MPR121Button button1 = MPR121Button(touchSensor, 1);
MPR121Button button2 = MPR121Button(touchSensor, 2);
MPR121Button button3 = MPR121Button(touchSensor, 3);
MPR121Button button4 = MPR121Button(touchSensor, 4);
MPR121Button button5 = MPR121Button(touchSensor, 5);
void setup() {
// Open up the serial port so that we can write to it
Serial.begin(115200);
// Initialise the touch sensor
touchSensor.begin();
/* ==== Electrode ==== */
// When electrode X is first pressed, call the function onButtonPressed (further down the page)
button1.onPress(onButtonPressed1);
button2.onPress(onButtonPressed2);
button3.onPress(onButtonPressed3);
button4.onPress(onButtonPressed4);
button5.onPress(onButtonPressed5);
/* ==== Electrode ==== */
// Once electode has been held for 1 second (1000ms) call onButtonHeld. Call it again every 0.5s (500ms) until it is let go
button1.onHoldRepeat(1000, 500, onButtonHeld);
button3.onHoldRepeat(1000, 500, onButtonHeld);
button4.onHoldRepeat(1000, 500, onButtonHeld);
button5.onHoldRepeat(1000, 500, onButtonHeld);
/* ==== Electrode ==== */
// When electrodeis released, call onButtonReleased
button1.onRelease(onButtonReleased1);
button2.onRelease(onButtonReleased2);
button3.onRelease(onButtonReleased3);
button4.onRelease(onButtonReleased4);
button5.onRelease(onButtonReleased5);
}
void loop()
{
// Check the state of the button
// Check the IRQ line - if it is LOW that Touch Sensor IC has new data for us
if (digitalRead(PIN_TOUCH_IRQ) == LOW) {
STARTtime = millis();
if (STARTtime - ENDtime <= 400) {
// Get the latest touch readings from the sensor. Do this here means we only call .touched() once instead of 3 times
int latestTouchReading = touchSensor.touched();
button1.update(latestTouchReading);
button2.update(latestTouchReading);
button3.update(latestTouchReading);
button4.update(latestTouchReading);
button5.update(latestTouchReading);
}
else {
ENDtime = STARTtime;
code = "";
}
} //END Interrupt
// convertor();
} //END void loop
//START of MY CODE
// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed1(Button& btn) {
Serial.print(" electrode touched 1");
}
// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed2(Button& btn) {
Serial.print(" electrode touched 2");
}
// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed3(Button& btn) {
Serial.print(" electrode touched 3");
}
// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed4(Button& btn) {
Serial.print(" electrode touched 4");
}
// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed5(Button& btn) {
Serial.print(" electrode touched 5");
}
// duration reports back how long it has been since the button was originally pressed.
// repeatCount tells us how many times this function has been called by this button.
void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount) {
Serial.print(" electrode has been held for ");
Serial.print(duration);
Serial.print(" ms; this event has been fired ");
Serial.print(repeatCount);
Serial.println(" times");
}
// duration reports back the total time that the button was held down
void onButtonReleased1(Button& btn, uint16_t duration) {
Serial.print(" electrode 1 released after ");
Serial.print(duration);
Serial.println(" ms");
//Serial.print(char(touch[1]));
code += (touch[1]);
Serial.print(code);
}
void onButtonReleased2(Button& btn, uint16_t duration) {
Serial.print(" electrode 2 released after ");
Serial.print(duration);
Serial.println(" ms");
code += (touch[2]);
Serial.print(code);
}
void onButtonReleased3(Button& btn, uint16_t duration) {
Serial.print(" electrode 3 released after ");
Serial.print(duration);
Serial.println(" ms");
code += (touch[3]);
Serial.print(code);
}
void onButtonReleased4(Button& btn, uint16_t duration) {
Serial.print(" electrode 4 released after ");
Serial.print(duration);
Serial.println(" ms");
code += (touch[4]);
Serial.print(code);
}
void onButtonReleased5(Button& btn, uint16_t duration) {
Serial.print(" electrode 5 released after ");
Serial.print(duration);
Serial.println(" ms");
code += (touch[5]);
Serial.print(code);
}
//MORSE CODE integration from
/*
PROGRAM TO DECIPHER MORSE CODE USING A PUSH BUTTON AND DISPLAY IT ON THE SERIAL MONITOR
DATE: 20 JANUARY 2017
AUTHORS: PINAKI SADHUKHAN AND PRIYANKA SADHUKHAN
*/
void convertor()
{
static String letters[] = {"A", "B", "C", "D", "E", "F", "AA", "BB", "CC", "DD", "EE", "FF", "AAA", "BBB", "CCC", "DDD", "EEE",
"FFF", "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "AAAAA", "BBBBB", "G"
};
int i = 0;
if (code == "55555")
{
Serial.print("."); //for break
}
else
{
while (letters[i] != "G") //loop for comparing input code with letters array
{
if (letters[i] == code)
{
Serial.print(char('A' + i));
break;
}
i++;
}
if (letters[i] == "G")
{
Serial.println("<Wrong input>"); //if input code doesn't match any letter, error
}
}
code = ""; //reset code to blank string
}