Serial communication Processing --> Arduino

edited June 2016 in Arduino

Hey Guys!

I'm busy making a project where my accelerometer @arduino can controle a ellipse @processing

But the problem is, it doesn't work. With the code I have now, I can receive the data from the accelerometer in processing. But my map function doesn't seem to work because with my code, the ellipse doesn't move at all.

Hope someone can help me out!

Arduino code:

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

int waardeX;
int waardeY;

Adafruit_MMA8451 mma = Adafruit_MMA8451();            //maak object acceleromter aan

void setup(void) {
  Serial.begin(9600);                                 //begin Seriële verbinding
  Serial.println("Adafruit MMA8451 test!");           //print waardes accelerometer

  if (! mma.begin()) {                                //mma.begin() om de sensor te herkennen.
    Serial.println("Couldnt start");                  //"MMA8451 found!" bij goed signaal, "Couldnt start" bij slecht signaal
    while (1);
  }
  Serial.println("MMA8451 found!");

  mma.setRange(MMA8451_RANGE_2_G);                    //lees range van 2_G (voor relatief slome bewegingen)

  Serial.print("Range = "); Serial.print(2 << mma.getRange());
  Serial.println("G");

}

void loop() {

  // Read the 'raw' data in 14-bit counts
  mma.read();                                         //lees rauwe data accelerometer uit
  Serial.print("X:\t"); Serial.print(mma.x);          //lees x waardes
  Serial.print("\tY:\t"); Serial.print(mma.y);        // lees y waardes
  Serial.println();                                   //print de waardes met een delay van 500ms
  delay(500);

  /* Get a new sensor event */
  sensors_event_t event;

  Serial.write(waardeX);
  Serial.write(waardeY);
}

Processing code:

import processing.serial.*;

Serial myPort;                                   //Maak Serial object aan
String val;                                      //Ontvangen data van serial poort.

int waardeX;
int waardeX1;
int waardeY2;
int waardeY;

void setup() {
  printArray(Serial.list());
  String portName = Serial.list()[0];            //0 is mijn Arduino poort. 
  myPort = new Serial(this, portName, 9600);     //open de poort. Zelfde als serial.begin(9600) bij arduino zodat arduino en processing hetzelfde communiceren.

  size(500, 500);                                //Grootte van het scherm
}

void draw() {
  background(255);                               //Achtergrondkleur
  ellipse(waardeX, waardeY, 20, 20);               //20,20 is grootte/breedte cirkel
  fill(200, 10, 100);                                       //maak het bolletje zwart

  waardeX = (int)map(waardeX1, 4000, -4000, 0, 500);        //waardeX mappen naar waa
  waardeY = (int)map(waardeY2, -4000, 4000, 0, 500);

  {
    if ( myPort.available() > 0) 
    {  // Als de data beschikbaar is:
      val = myPort.readStringUntil('\n');         //Lees het en zet het in val
    } 
        println(val); //print it out in the console
  }
}
Tagged:

Answers

  • Indeed, you're receiving data from Serial recorded in a variable called val but you're not using it. You should parse this String in order to update your waardeX1 and waardeY2 values with something like this:

    val = myPort.readStringUntil('\n');
    String[] tmp = val.split("\t");
    if( tmp.length == 4 ){
        waardeX1 = int( tmp[1] );
        waardeY2 = int( tmp[3] );
    }
    
Sign In or Register to comment.