Show Posts
Pages: 1 ... 19 20 [21]
301  Forum 2005-2010 (read only) / Interfacing / Re: ADXL330 and Cube 3D, Processing and Arduino on: August 09, 2010, 05:34:42 pm
Thanks, would you please explain the following code?
Code:
int angleX = 179-(180/3.14159)*acos(accelerationX/1000)+potVal;
  int angleY = (180/3.14159)*acos(accelerationY/1000)+potVal2;  
302  Forum 2005-2010 (read only) / Interfacing / ADXL330 and Cube 3D, Processing and Arduino on: August 09, 2010, 01:33:49 pm
Hi guys,

I'm working on creating a virtual 3D cube which should follow the movements of a physical 3D cube which has an ADXL330 inside of it which is connected to the Arduino board. The program which displays the virtual cube is a Processing sketch.

I think I'm pretty near implementing this but somehow I'm stuck.. the virtual cube rotates fine when I just rotate one axis and I comment out the rotations on the other 2 axes, this for all 3 axes.

But, when I'm using rotations on all of the 3 axes it displays a spin when an axis is passing from - to + ..  when it just shouldn't.

Note that instead of hardcoding sensitivity, voltages inside the program I preferred to use a calibration process.

Now, this is the Arduino code:
Code:
/**
* Reads from a 3 axis accelerometer and send values to serial
*/

// CONSTANTS
#define SAMPLES 64

// INPUTS
#define Y 0
#define Z 1
#define X 2
#define RESET 2


// board inputs variables

int x = 0;
int y = 0;
int z = 0;
int reset = 0;


void setup() {
  analogReference(EXTERNAL);
  
  pinMode(RESET, INPUT);
  digitalWrite(RESET, HIGH); // enable pullup resitor
  
  Serial.begin(9600);  
}


void loop() {
  delay(10); // debounces switches
  
  x = 0;
  y = 0;
  z = 0;
  
  for(int i=0; i<SAMPLES; i++) {
    x += analogRead(X);
    y += analogRead(Y);
    z += analogRead(Z);
    reset = digitalRead(RESET);
  }
  x = x / SAMPLES;
  y = y / SAMPLES;
  z = z / SAMPLES;
  
  Serial.print(x, HEX);
  Serial.print(',');
  Serial.print(y, HEX);
  Serial.print(',');
  Serial.print(z, HEX);
  Serial.print(',');
  Serial.print(reset, HEX);
  Serial.println(',');
  //delay(100);
}

and this is the Processing code:
Code:
/**
 * Displays values coming from an accelerometer connected to Arduino
*/

import processing.serial.*;

Serial myPort;  // Create object from Serial class

int [] a = {0, 0, 0}; // accelerations x,y,z
int [] zero = {0, 0, 0}; // values for no acceleration
int [] gmax = {0, 0, 0}; // values when applied full positive g acc
int [] gmin = {0, 0, 0}; // values when applied full negative g acc


float Xrot;
float Yrot;
float Zrot;


int reset = 1;
int oldreset = 1;


final int VIEW_SIZE_X = 600, VIEW_SIZE_Y = 600;

int lf = 10; // 10 is '\n' in ASCII
final int BUFFLEN = 100;
byte[] inBuffer = new byte[BUFFLEN];

boolean calibrated = false;
final int ACCTHRESHOLD = 3;

void setup()
{
  size(VIEW_SIZE_X, VIEW_SIZE_Y, P3D);
  myPort = new Serial(this, "/dev/ttyUSB0", 9600);
}


void draw() {
  
  if(!calibrated) {
    calibrate();
  }
  
  
  int [] values = readAccelerometer();
  
  for(int i = 0; i<3; i++) {
    /*if(values.length < 4) {
      printArr(values); exit();
    }*/
    int newacc = zero[i] - values[i];
    a[i] = (abs(newacc - a[i]) > ACCTHRESHOLD) ? newacc : a[i];
  }
  
  float g = (0.3 * 3) / 1024; // sensitivity V/g : max volt = g : max digital sesitivity
  //println(g);
  background(#000000);
  
  
  translate(VIEW_SIZE_X / 2, VIEW_SIZE_Y / 2, -400);
  
  //println(((float) a[0] * 3.0 / 1024.0 - 1.5) / 0.3);
  
  pushMatrix();
    scale(6,6,14);
      
    
    Xrot = -atan2(a[1], a[2]);
    Yrot = atan2(a[0], a[2]); // OK
    Zrot = atan2(a[0], a[1]); // OK
    
    
    rotateX(Xrot); // OK
    rotateY(Zrot); // OK
    rotateZ(Yrot); // OK
    
    
    buildBoxShape();
    
  popMatrix();
  
  fill(#ffffff);
  translate(-VIEW_SIZE_X/2, -VIEW_SIZE_Y/2, 0);
  PFont font;
  // The font must be located in the sketch's
  // "data" directory to load successfully
  font = loadFont("CourierNew36.vlw");
  textFont(font);
  String debug = a[0] + " " + a[1] + " " + a[2] + "\n" + degrees(Xrot) + " " + degrees(Yrot) + " " + degrees(Zrot)/*+ "\n" + xmin + " " + ymin + " " + zminhx + " " + hy + " " + hz */;
  text(debug, 20, 0);
  
}


void buildBoxShape() {
  //box(60, 10, 40);
  noStroke();
  beginShape(QUADS);
  
  //Z+ (to the drawing area)
  fill(#00ff00);
  vertex(-30, -5, 20);
  vertex(30, -5, 20);
  vertex(30, 5, 20);
  vertex(-30, 5, 20);
  
  //Z-
  fill(#0000ff);
  vertex(-30, -5, -20);
  vertex(30, -5, -20);
  vertex(30, 5, -20);
  vertex(-30, 5, -20);
  
  //X-
  fill(#ff0000);
  vertex(-30, -5, -20);
  vertex(-30, -5, 20);
  vertex(-30, 5, 20);
  vertex(-30, 5, -20);
  
  //X+
  fill(#ffff00);
  vertex(30, -5, -20);
  vertex(30, -5, 20);
  vertex(30, 5, 20);
  vertex(30, 5, -20);
  
  //Y-
  fill(#ff00ff);
  vertex(-30, -5, -20);
  vertex(30, -5, -20);
  vertex(30, -5, 20);
  vertex(-30, -5, 20);
  
  //Y+
  fill(#00ffff);
  vertex(-30, 5, -20);
  vertex(30, 5, -20);
  vertex(30, 5, 20);
  vertex(-30, 5, 20);
  
  endShape();
}


void calibrate() {
  println("Put accelerometer with Z pointing down, then press reset button");
  int [] values = getCalibration();
  
  zero[0] = values[0]; // X doesn't get any acceleration
  zero[1] = values[1]; // Y doesn't get any acceleration
  
  gmax[2] = values[2]; // store g value when Z pointing down
  
  
  println("Put accelerometer with Z pointing up, then press reset button");
  values = getCalibration();  
  gmin[2] = values[2]; // store g value when Z pointing up
  
  println("Put accelerometer with X pointing up, then press reset button");
  values = getCalibration();  
  gmin[0] = values[0]; // store g value when Z pointing up
  zero[2] = values[2]; // Z doesn't get any acceleration
  
  println("Put accelerometer with X pointing down, then press reset button");
  values = getCalibration();  
  gmax[0] = values[0]; // store g value when Z pointing up
  
  
  println("Put accelerometer with Y pointing up, then press reset button");
  values = getCalibration();  
  gmin[1] = values[1]; // store g value when Z pointing up
  
  println("Put accelerometer with Y pointing down, then press reset button");
  values = getCalibration();  
  gmax[1] = values[1]; // store g value when Z pointing up
  
  println("zero: ");
  printArr(zero);
  
  println("gmax: ");
  printArr(gmax);
  
  println("gmin: ");
  printArr(gmin);
  
  calibrated = true;
}


/**
 * Returns values read from accelerometer only when reset button released
*/
int [] getCalibration() {
  int oldreset = 1;
  int [] values = {0, 0, 0, 1};
  
  while(values == null || !(values[3] == 1 && oldreset == 0)) { // cycle until we are not releasing button
    if(values != null) {
      oldreset = values[3];
      //println(values[3] + " " + oldreset);
    }
    values = readAccelerometer();
  }
  return values;
}


/**
 * Get x,y,z and reset values coming from Serial interface.
*/
int [] readAccelerometer() {
  int [] values = null;
  
  if(myPort.available() > 0 && myPort.readBytesUntil(lf, inBuffer) > 0) {
    values = new int[4];
    
    String inputString = new String(inBuffer);
    String [] inputStringArr = split(inputString, ',');
    
    values[0] = unhex(inputStringArr[0]); // x
    values[1] = unhex(inputStringArr[1]); // y
    values[2] = unhex(inputStringArr[2]); // z
    values[3] = unhex(inputStringArr[3]); // reset
    
    return values;
  }
  else {
    delay(10);
    return readAccelerometer();
  }
}


void printArr(int [] arr) {
  for(int i = 0; i < arr.length; i++) {
    print(arr[i] + " ");
  }
  println("");
}


Thanks for your help,

Fabio Varesano
303  Forum 2005-2010 (read only) / Exhibition / Re: My first Arduino project on: December 12, 2010, 06:13:58 pm
nice work.

btw, you might want to open the video for comments and listing and provide some meaningful title and description. Maybe also a link to your Arduino sources and schematics.

This way, what you did will also be helpful for other people as they'll be able to find your video and your work.

Take care,

FV
304  Forum 2005-2010 (read only) / Exhibition / 9 DOM/DOF MARG IMU orientation filter on: December 05, 2010, 05:18:32 pm
I present you my implementation of Sebastian Madgwick's implementation of Mayhony's DCM filter incorporating Sebastian's magnetic distortion compensation.

The sensors used where the ADXL345, the ITG3200 and the HMC5843.

All the details available at http://www.varesano.net/blog/fabio/initial-implementation-9-domdof-marg-imu-orientation-filter-adxl345-itg3200-and-hmc5843-a
305  Forum 2005-2010 (read only) / Exhibition / Exploring the Advanced features of the ADXL345 on: November 22, 2010, 11:22:16 am
I spent a couple of days playing with the advanced features of the ADXL345 accelerometer.

I'm now able to detect single & double tap, activity, inactivity, free fall and I also implemented some kind of power saving.

You find a video example and all the programs at http://www.varesano.net/blog/fabio/exploring-advanced-features-adxl345-accelerometer-single-double-tap-activity-inactivity-f
306  Forum 2005-2010 (read only) / Exhibition / An homebrew DIY breakout board for the ADXL345 on: September 25, 2010, 12:44:32 pm
This is the last project I've been working on. Creating an homebrew DIY breakout board for the ADXL345.

Why? Because it teach you a lot and once you understand the process , possibilities are endless.

So, this is the result:
http://www.varesano.net/blog/fabio/homebrew-diy-breakout-board-adxl345-accelerometer-first-process-try-tested-arduino-proces

Next step: an ITG-3200 breakout board and one for the HMC5843 ...
then combine everything into a completely digital (I2C) DIY IMU.. eheheh
307  Forum 2005-2010 (read only) / Frequently-Asked Questions / tilt sensor switches: where to buy this one? on: July 14, 2010, 04:59:36 am
Anyone knows the identification name of this tilt switch/sensor? http://yfrog.com/c9tiltsensorj

That's the one which comes with the arduino base workshop kit I have.

The kit comes with just 2 of them and I need another one.

I'm looking for online sellers/shops, possibly in Italy or Europe. I've not been able to find this one in local shops (Torino, Italy).

Any suggestion?

Thanks,

Fabio Varesano
308  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Practical uses of Capacitors in Arduino projects? on: July 12, 2010, 06:23:30 pm
Thanks guys for the replies. Do you remember practical projects here on Arduino website or everywhere on the web with cool things which uses (also as side components) capacitors?

The problem is: I do know what a capacitor does. The theory around it. Still I don't see how can I use in my Arduino projects, even if as a side component. Instead I always see lot of usages for eg resistors, buttons, transistors, etc.. Capacitors are still kind of vodoo to me.
309  Forum 2005-2010 (read only) / Frequently-Asked Questions / Practical uses of Capacitors in Arduino projects? on: July 11, 2010, 03:49:15 am
Hi guys,

I'm moving my first steps with Arduino. I have a base workshop kit with come with a lot of different capacitors. I read about what are they usually used, anyway I still don't see a practical use scenario for them on simple arduino projects..

Would you suggest example of usages of capacitors in Arduino projects? Both link than explainations.

Thanks,

Fabio Varesano
310  Forum 2005-2010 (read only) / Frequently-Asked Questions / Wires colors conventions on: July 08, 2010, 04:37:29 am
Hi guys,

moving my first steps with Arduino. I have a standard workshop kit which comes with lot of different color wires: green, yellow, blue, red, orange, gray, brown, white.

What are the conventions on using this colors?

Thanks,

Fabio Varesano
311  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Analog In and Resistors on: July 08, 2010, 04:35:02 am
The link to the image is http://yfrog.com/6ximage217qj
312  Forum 2005-2010 (read only) / Frequently-Asked Questions / Analog In and Resistors on: July 08, 2010, 04:34:20 am
Hi guys,

I'm moving my first steps with Arduino.. now I'm trying some reading from the Analog In. This is my circuit: yfrog.com/6ximage217qj

This is my code:

/* read the value from a potentiometer and use it as delay */

int potPin = 0;
int ledPin = 13;
int val = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  Serial.println(val);
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}


I don't get why I always get 1023 on serial monitor.

Given that the current flows from 5V to the two resistors I would have expected a decrease in the voltage thus reading something smaller than 1023.

What am I doing wrong? If this has something with wrong circuit stuff, would you point me so some documentation about it?

Thanks,

Fabio Varesano
Pages: 1 ... 19 20 [21]