Serial Communication


hey guys!

i'm busy making project accelerometer @arduino can controle ellipse @processing

but problem is, doesn't work. think has receiving 2 data's in processing @ same moment (value waardex & value waardey).

hope can me out!

my arduino code:

code: [select]
#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 '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);

  /* new sensor event */
  sensors_event_t event;

  serial.print(waardex, dec);
  serial.print(",");
  serial.print(waardey, dec);
  serial.print("|");                                   //schrijf waardes x & y naar processig
}


my processing code:

code: [select]
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 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 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);

  {
    println(waardex);
  }
}

void serialevent(serial myport) {
  string mystring = myport.readstringuntil(124); //the ascii value of "|" character
  if (mystring != null ) {
    mystring = trim(mystring); //remove whitespace around our values
    int inputs[] = int(split(mystring, ','));
    //now assign values in processing
    if (inputs.length == 2) {
      waardex = inputs[0];
      waardey = inputs[1];
    }
  }
}

in arduino code, sending "x:<tab>xxx<tab>y:<tab>yyy<cr><lf>0,0|" each time through loop(), 1/2 second pause after <lf>.

in processing code, reading data pipe symbol, , splitting @ commas. first string therefore "x:<tab>xxx<tab>y:<tab>yyy<cr><lf>0" , second string "0".

how can expect first string converted int?

why never assigning values waardex , waardey in arduino? why sending x:, y:, tabs, carriage return, line feed , sensor values when don't plan (properly) use them?

shouldn't assigning mma.x waardex , mma.y waardey?


Arduino Forum > Using Arduino > Programming Questions > Serial Communication


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'