Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. When i want to map the val (string) it says it must be a float to be mapped. I've tried to convert the string into a float but it doesn't work at all..

Hope someone can help me out!

My 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 waardeX;                                      //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(waardeX1, waardeY, 20, 20);               //20,20 is grootte/breedte cirkel
  fill(200, 10, 100);                                       //maak het bolletje zwart

  waardeX1 = (int)map(waardeX, 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(waardeX); //print it out in the console
  }
}
share|improve this question
    
Hi, Welcome to SE Arduino. I'm sorry but, its really difficult for me to follow your code. You seem to be mixing the use of waardeX and waardeX1 and using values before you have calculated them, meaning they are old values. Can I suggest you put this code on the shelf and start a new minimal program that creates an ellipse at 10, 10, 20, 20 and 20, 20, 20, 20 and 30, 30, 20, 20 and map those. If they work as expected then we now its not the functions that are the problems, just the data. (Also a simple program like that will help people understand your code better, hopefully). – Matt Jun 24 '16 at 12:06

The problem you are facing is that you are sending the string:

X:⇥23.87⇥Y:⇥11.34

and trying to interpret that as a single number. It's just not going to happen. There's two numbers and two labels there.

Instead you are going to have to parse the string to get at the individual components.

You might want to look at the java String.split() function to convert the string into an array of 4 items, then you can use the second and fourth slices (slices 1 and 3 since it's zero-based) and convert them to floats.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.