Accelerometer and Arduino

Hello guys,

I’m having trouble trying to run a program, I hope this is not too confusing. I will break down this problem into two parts.

I’m basically trying to interface an accelerometer with arduino UNO, my task is to draw a sphere which will simulate the movement of the accelerometer as I tilt it in different directions based on the X, Y and Z coordinates. When I run the program individually, that is, when I run the program that simulates the sphere rotating and in another file I run a program that just prints the X,Y and Z values of the accelerometer they both run perfectly, the problem comes when I put both codes together and then I run it, I get an error that says “Error inside Serial.()”.

I changed the position of the line of code “arduino = new Arduino(this, Arduino.list()[1], 57600);” within the “void setup” method and then the program runs but the sphere won’t move. Can anybody help me run this program?

import processing.serial.;
import cc.arduino.
;
import processing.opengl.*;

Arduino arduino;
int array = {0,1,2};

void setup() {
frameRate(30);
arduino = new Arduino(this, Arduino.list()[1], 57600);
size(450, 450, OPENGL);
fill(238,203,173, 100);
stroke(248,248,255);
// fill(0,0,205, 100); it is just a blue sphere
}

void draw() {

int xVal = arduino.analogRead(array[0]);
int yVal = arduino.analogRead(array[1]);
int zVal = arduino.analogRead(array[2]);

background(0);
translate(width/2, height/2);
sphere(100);
//sphereDetail(20,20);
rotateX(xVal);
rotateY(yVal);
rotateZ(zVal);

println(" X"+"\t"+" Y"+"\t"+" Z");
println(xVal+"\t"+yVal+"\t"+zVal); //Values given by accelerometer

delay(100);
}

This might also be a bit confusing, it also has to do with a bit of Math:

I converted the X and Y readings from the accelerometer by mapping the values into degrees (eg. for X: 355-522 -----> 0 - 90º). I only knew how to do it with the readings from the X and Y axis, however, having the X and Y values in degrees, is there any way I can calculate Z? is there any way I can calculate that Z position in space, given in this case in degrees? maybe it is easier to understand this if we imagine a dot or just “any object” you want to think of somewhere in a spherical coordinate system.

Thanks a million!!!

but the sphere won't move. Can anybody help me run this program?

What output are you seeing? We can't see your output.

I converted the X and Y readings from the accelerometer by mapping the values into degrees

Which accelerometer do you have? Typically, the output is not in degrees.

maybe it is easier to understand this if we imagine a dot or just "any object" you want to think of somewhere in a spherical coordinate system.

Values in spherical coordinate systems can be mapped to Cartesian coordinates. I'd be willing to bet that google can even explain how.

Of course, this implies that you actually have angles, which I doubt.

PaulS:

What output are you seeing? We can't see your output.

X Y Z
353 370 119

But the sphere is just static.

Which accelerometer do you have?"

MMA7361L.

Typically, the output is not in degrees.

I know man, that's why I mapped the values into degrees (eg. for X: 355 (minimum value when the accelerometer is horizontal)-522 (maximum value when the accelerometer is vertical) -----> 0 - 90º).

that's why I mapped the values into degrees (eg. for X: 355 (minimum value when the accelerometer is horizontal)-522 (maximum value when the accelerometer is vertical) -----> 0 - 90º).

I would really expect that the range of values output by the accelerometer to range from -90 (on edge, top facing one way) to 90 (on edge, top facing the other way).

But the sphere is just static.

Then, the problem isn't with getting data from the accelerometer. It is with using that data to transform the sphere.

Although, how you expect to see a sphere look any different if you rotate it 10 degrees escapes me. Your eye position and lighting position haven't change, so the sphere should look the same, unless you have some texture mapping going on, which I don't see.

Are rotateX(), rotateY(), and rotateZ() Processing functions or your functions?

PaulS:

I would really expect that the range of values output by the accelerometer to range from -90 (on edge, top facing one way) to 90 (on edge, top facing the other way).

Although, how you expect to see a sphere look any different if you rotate it 10 degrees escapes me. Your eye position and lighting position haven't change, so the sphere should look the same, unless you have some texture mapping going on, which I don't see.

Well they actually do range from -90 to 90 :wink: but that's isn't really the problem, the problem is that the sphere does not move at all even though when I move (or rotate) the accelerometer the values on the screen go from -90 to 90 degrees.

Are rotateX(), rotateY(), and rotateZ() Processing functions or your functions?

They're Processing functions.

the problem is that the sphere does not move at all even though when I move (or rotate) the accelerometer the values on the screen go from -90 to 90 degrees.

Is there any change to a cube?

A sphere centered at the origin, being perfectly round, will look the same no matter what you do with the camera, which is what rotateX, rotateY, and rotateZ effectively does.

PaulS:

A sphere centered at the origin, being perfectly round, will look the same no matter what you do with the camera, which is what rotateX, rotateY, and rotateZ effectively does.

What you're saying makes sense :wink: thing is, the sphere has a grid-like surface which allows you to easily see changes when it's moving. The following code is also a proof of what I'm saying:

import processing.opengl.*;

float a;

void setup() {
size(450, 450, OPENGL);
fill(238,203,173, 100);
stroke(248,248,255);
// fill(0,0,205, 100); Blue colour
}

void draw() {
background(0);
translate(width/2, height/2);
rotateX(a);
rotateY(a*2);
sphere(100);
//sphereDetail(20,20);
rotateX(PI/2);
//rotateZ();

a += 0.01;
}

I'll try with a cube but I'm almost sure that's not the problem either, thanks for helping anyway.

That last code differs from the previous code in the order of operations. In the earlier code you had:
Draw the sphere
Rotate the draw plane
Rotate the draw plane
Rotate the draw plane

In the code you have:
Rotate the draw plane
Rotate the draw plane
Draw the sphere
Rotate the draw plane

I’m not particularly surprised at the different results.

PaulS:
That last code differs from the previous code in the order of operations. In the earlier code you had:
Draw the sphere
Rotate the draw plane
Rotate the draw plane
Rotate the draw plane

In the code you have:
Rotate the draw plane
Rotate the draw plane
Draw the sphere
Rotate the draw plane

I'm not particularly surprised at the different results.

Thanks Paul, that was the problem! I'm surprised because I don't understand why in the code the function "rotate" is used before drawing the sphere but I guess that doesn't matter since "draw method" acts as a loop? I'm guessing the loop starts running correctly from the second time onwards?

I actually didn't write the last code I posted (the one that works). The first time I ran through it I realized it was so simple that I didn't think I would have to look at it again in order to write my own code. The one thing I did miss which was very necessary was the rotate(PI/2) BUT my assumption said that the function "rotate" could do that automatically, after all, the function is called "rotate", isn't it? My bad anyway, I would've saved a lot of time just by comparing that part of the code.

Thanks again :wink: