MPR121sensor attached to Arduino and serial communication to Processing

Hi there,

I’m new to processing but I’m working on a project with a MPR121 sensor with 12 channels. I am done with my processing code and i have a sketch from the Arduino Adafruit MPR121 and it works on my project. Now i just need to fully serial communicate between the two. But i have no clue how to do this, or how even to identify each channel (sensor).
Is there anyone that can help me out? I’ve searched all over the internet but there is not much information about the MPR121 serial communication to Processing only Python. Is there anyone who can help me, because I’ve done the code in Processing but i don’t understand what to do next.

PROCESSING CODE:

import processing.serial.*;
import cc.arduino.*; // knop
Arduino arduino;
import processing.pdf.*;
import ddf.minim.*;
Serial myPort;


int index;

float p1_x;
float p1_y;
float p2_x;
float p2_y;
float p3_x;
float p3_y;
float p4_x;
float p4_y;
float total;
//boolean knopStop = false;


AudioPlayer player;
Minim minim;

String[] tableau ={
  "Orbital.mp3", "Repose.mp3", "Parallel_Dimension.mp3", "Yoga_Depth.mp3", "Barceloneta.mp3", "Waves.mp3", "Illuminous.mp3", "Autumn_Forest.mp3", "Will_you_be_there_when_I_wake_up.mp3"
};


boolean debounce = true;
Vierkant vierkant1;
Vierkant vierkant2;
Vierkant vierkant3;
Vierkant vierkant4;
Vierkant vierkant5;
Vierkant vierkant6;
Vierkant vierkant7;
Vierkant vierkant8;
Vierkant vierkant9;


int vierkantGrootte = 140; 
ArrayList<PVector> selectedPoints = new ArrayList<PVector>();
ArrayList<Points> points = new ArrayList<Points>();
ArrayList<hover> hovers = new ArrayList<hover>();




color[] palette1 = {color(9, 63, 62), color(164, 140, 21), color(8, 57, 72), color(32, 106, 106)};
color[] palette2 = {color(232, 143, 187), color(226, 72, 147), color(142, 58, 142), color(71, 98, 171)};
color[] palette3 = {color(248, 233, 35), color(238, 114, 28), color(229, 42, 18), color(126, 37, 16)};




void setup() {
  size(500, 500);
  frameRate(4);

  //myPort = new Serial(this,"COM3",9600);
  //myPort.clear();

  //arduino.pinMode(7, Arduino.INPUT_PULLUP);


  minim = new Minim(this);

  int meditatieMuziek = int (random(tableau.length));
  String meditatie = tableau[meditatieMuziek];
  player = minim.loadFile(meditatie);
  // player.pause();
  player.loop();

  vierkant1 = new Vierkant(20, 20, 140, 140, 50, 50, 130, 50, 50, 130, 130, 130);
  vierkant2 = new Vierkant(20+160, 20, 140, 140, 210, 50, 290, 50, 210, 130, 290, 130);
  vierkant3 = new Vierkant(20+320, 20, 140, 140, 370, 50, 450, 50, 370, 130, 450, 130);
  vierkant4 = new Vierkant(20, 180, 140, 140, 50, 210, 130, 210, 50, 290, 130, 290);
  vierkant5 = new Vierkant(20+160, 180, 140, 140, 210, 210, 290, 210, 210, 290, 290, 290);
  vierkant6 = new Vierkant(20+320, 180, 140, 140, 370, 210, 450, 210, 370, 290, 450, 290);
  vierkant7 = new Vierkant(20, 340, 140, 140, 50, 370, 130, 370, 50, 450, 130, 450);
  vierkant8 = new Vierkant(20+160, 340, 140, 140, 210, 370, 290, 370, 210, 450, 290, 450);
  vierkant9 = new Vierkant(20+320, 340, 140, 140, 370, 370, 450, 370, 370, 450, 450, 450);

  for (int i=0; i < 9; i++) {
    hovers.add(new hover(random(50, 450), random(150, 450), random(5,20)));
  }
}

void draw() {

  //while (myPort.available() > 0) {
  //myString = myPort.readStringUntil(lf); 

  //if (myString != null) { 
  //print(myString);
  //int valueStart = myString.indexOf(":");
  //if (valueStart > 0) {
  //String mySensor = myString.substring(0, valueStart); 
  // String myData = myString.substring(valueStart+1, myString.length()-1); 

  // myData = myData.trim();

  //if (mySensor.equals("touched")){
  // for (uint8_t i=0; i<12; i++) {
  // it if *is* touched and *wasnt* touched before, alert!
  //if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
  // Serial.print(i); Serial.println(" touched");
  //  }
  // if it *was* touched and now *isnt*, alert!
  //if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
  // Serial.print(i); Serial.println(" released");
  // }
  //  }
  //  }
  //  { 
  //  currtouched = float(myData);
  //  println("touched:" + myData);
  // }
  //  }



  if (player.isPlaying() == false) {
    int meditatieMuziek = int(random(tableau.length));
    String meditatie = tableau[meditatieMuziek];
    player = minim.loadFile(meditatie);
    player.play();
  }



  // }
  background(0);
  //int button1 = arduino.digitalRead(6);


  vierkant1.display();
  vierkant1.checkOver();
  vierkant1.checkClick();
  vierkant2.display();
  vierkant2.checkOver();
  vierkant2.checkClick();
  vierkant3.display();
  vierkant3.checkOver();
  vierkant3.checkClick();
  vierkant4.display();
  vierkant4.checkOver();
  vierkant4.checkClick();
  vierkant5.display();
  vierkant5.checkOver();
  vierkant5.checkClick();
  vierkant6.display();
  vierkant6.checkOver();
  vierkant6.checkClick();
  vierkant7.display();
  vierkant7.checkOver();
  vierkant7.checkClick();
  vierkant8.display();
  vierkant8.checkOver();
  vierkant8.checkClick();
  vierkant9.display();
  vierkant9.checkOver();
  vierkant9.checkClick();

  beginShape(TRIANGLE_STRIP); //begin een vorm
  background(palette1[(int)random(1, 4)]);

  for (int i=0; i<selectedPoints.size(); i++) { //haal alle punten uit de lijst
    PVector p = selectedPoints.get(i);
    stroke(palette2[(int)random(1, 4)]);
    vertex(p.x, p.y);//voeg alle punten van de vorm toe
    noFill();
    //endShape();
  }


  if (keyPressed) {//button1 == arduino.HIGH && knopStop == FALSE){
    //knopStop = true;
    //total1 = total1 + 0.1;
    //}
    //if (button1 == arduino.LOW){
    //knopStop - false;
    //

    if (key == 'g') {
      total = total+1;
    }
  }
  for (int i=0; i<total; i++) {
    hover h = hovers.get(i);
    h.hover();
    frameRate(2);
  }

}

//saveFrame("ThinkConnectAct-####.ext"); with keypressed 'A' or something. 





void mouseReleased() {
  debounce = true;
}

void stop() {
  player.close();
  minim.stop();
  super.stop();
}


void keyPressed() {
  selectedPoints = new ArrayList<PVector>(); //clear de lijst door een nieuwe te maken (overschrijft)
}

This is my class for the ellipse and text

class hover {
  float x;
  float y;
  String [] s = new String[9];
  int index;
  float grootte;


  hover(float tempx, float tempy, float tempgrootte) {
    x = tempx;
    y = tempy;
    grootte = tempgrootte;
    s[0] = "Act";
    s[1] = "Love";
    s[2] = "Courage";
    s[3] = "Know";
    s[4] = "Feel";
    s[5] = "Do";
    s[6] = "Choose";
    s[7] = "Stress";
    s[8] = "Pathway";
    index = int(random(s.length));
  }



  void hover() {
    //noStroke();
    fill(255);
    textSize(20);
    text(s[index], random(x), random(y)); // Text wraps within text box
    ellipse(x, y, grootte, grootte);
  }
}

This is my class for the individual squares

class Vierkant {
  float x;
  float y;
  float grootte_w = 140;
  float grootte_h = 140;
  float p1_x; 
  float p1_y;
  float p2_x;
  float p2_y;
  float p3_x;
  float p3_y;
  float p4_x;
  float p4_y;


  boolean over;
  boolean selected;

  Vierkant(float tempx, float tempy, float tempgrootte_w, float tempgrootte_h, float tempp1_x, float tempp1_y, float tempp2_x, float tempp2_y, float tempp3_x, float tempp3_y, float tempp4_x, float tempp4_y) {
    x = tempx;
    y = tempy;
    grootte_w = tempgrootte_w;
    grootte_h = tempgrootte_h;
    p1_x = tempp1_x;
    p1_y = tempp1_y;
    p2_x = tempp2_x;
    p2_y = tempp2_y;
    p3_x = tempp3_x;
    p3_y = tempp3_y;
    p4_x = tempp4_x;
    p4_y = tempp4_y;

    selected = false;
  }


  void display() {
    if (selected == true) { //als ie geselecteer is

      selectedPoints.add(new PVector(p1_x, p1_y)); 
      selectedPoints.add(new PVector(p2_x, p2_y));
      selectedPoints.add(new PVector(p3_x, p3_y));
      selectedPoints.add(new PVector(p4_x, p4_y));
    }

    rect(x, y, grootte_w, grootte_h);
    stroke(0);
    strokeWeight(5);
    point(p1_x, p1_y);
    point(p2_x, p2_y);
    point(p3_x, p3_y);
    point(p4_x, p4_y);
  }


  
  void checkOver() {
    if (mouseX > x && mouseX < x + grootte_w && mouseY > y && mouseY < y+grootte_h) {
      over = true;
    } else {
      over = false;
    }
  }



  void checkClick() {
    if (mousePressed && over == true) {

      if (selected == false && debounce == true) {
        selected = true;
        debounce = false; 
      } else if (selected==true && debounce == true) {
        selected = false;
        debounce = false;
      }
    }
  }
}


If anyone wishes to help me maybe through Discord would be a nice way for me to understand it also in the future, since i like this sensor so much!

1 Like

Sorry, I haven’t touched an Arduino in years. Maybe this video contains something useful?

1 Like

Hello,

My suggestion is to write some working modular code (without any extra code or hardware) for later use that:

  • Tx Send data from Arduino (to Processing); data should represent the sensor data you will be sending.
  • Rx Read the data in Processing (from the Arduino) and display it to console and screen and be able to work with it; do some calculations and verify.
  • Once you have the above working integrate it into your code.

:)

1 Like