User:Sarah Labianca/Notebook/Smyth Lab/2012/04/05: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
Line 23: Line 23:
I wrote and uploaded my code to the Arduino and accelerometer using the [http://arduino.cc/hu/Main/Software Arduino 1.0 IDE] on Ubuntu 11.04. Help for getting the integrated development environment running on Ubuntu can be found [http://arduino.cc/playground/Linux/Ubuntu here]. The language is very similar to C.
I wrote and uploaded my code to the Arduino and accelerometer using the [http://arduino.cc/hu/Main/Software Arduino 1.0 IDE] on Ubuntu 11.04. Help for getting the integrated development environment running on Ubuntu can be found [http://arduino.cc/playground/Linux/Ubuntu here]. The language is very similar to C.


====ADXL345 and Arduino 1.0 IDE====
==== ====
To write code for the ADXL in Arduino 1.0, one needs to have a library containing a .cpp and a .h file. The library is inserted into the libraries folder in the Arduino 1.0 directory. Essentially, a library contains subroutines one may find useful in writing their code and helps a programmer interface with a device, without having to worry about all the details.


I used an [http://code.google.com/p/adxl345driver/downloads/list ADXL 345 library] that I found on a bildr tutorial.


====Code====
<source lang ="c">
//Arduino 1.0+ Only!
//Arduino 1.0+ Only!
#include <Wire.h>
#include <ADXL345.h>
ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
int x,y,z;
float Gx, Gy, Gz;
int count=0;
void setup(){
  Serial.begin(9600);
  adxl.powerOn();
  adxl.setRangeSetting(2);  //G sensing range from -2g to +2g
 
  //set activity/ inactivity thresholds (0-255)
  adxl.setActivityThreshold(75); //62.5mg per increment
  adxl.setInactivityThreshold(75); //62.5mg per increment
  adxl.setTimeInactivity(6); // how many seconds of no activity is inactive?
  //look of activity movement on this axes - 1 == on; 0 == off
  adxl.setActivityX(1);
  adxl.setActivityY(1);
  adxl.setActivityZ(1);
  //look of inactivity movement on this axes - 1 == on; 0 == off
  adxl.setInactivityX(1);
  adxl.setInactivityY(1);
  adxl.setInactivityZ(1);
  //setting all interupts to take place on int pin 1
  //I had issues with int pin 2, was unable to reset it
  adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,    ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,  ADXL345_INT1_PIN );
  //register interupt actions - 1 == on; 0 == off 
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,  1);
  adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}
void loop(){
  //To see current register values of ADXL345
  //printAllRegister();
  Scale();
  Output();
 
  //getInterruptSource clears all triggered actions after returning value
  //so do not call again until you need to recheck for triggered actions
  byte interrupts = adxl.getInterruptSource();




 
  //inactivity
  if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
  Serial.println("inactivity");
  PrintHeadings();
}
 
  //activity
//if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
//  Serial.println("activity"); 


  //delay(500);
 
 
// DetermineOffsets();
 
 
//}
}


 
 


<source lang ="c">
init.h
void Scale (void){
adxl.readAccel(&x, &y, &z);
//CORRECTING FOR OFFSET OF ACCELEROMETER DUE TO POSITION
  x=x-12;
  y=y-18;
  z=z-245;
//SCALING IN G'S
  //scale = total g range / 1024
  //-2g to +2g = 4/1024 = 0.00390625, -4g to +4g = 8/1024 = 0.0078125
  //-8g to +8g = 16/1024 = 0.015625, -16g to +16g = 32/1024 = 0.03125
  Gx = x * 0.0039;
  Gy = y * 0.0039;
  Gz = z * 0.0039;
 
  }
 
 
 
void Output(void){
  PrintData();
  }
 
 
 
 
 
 
void PrintHeadings(void){
//if(count == 0){
  Label();
// }
//if (count<50){
//count = count++;
//}
//if (count == 50){
  // count = 1;
  // Label();
//}
}
 
 
void Label (void){
  Serial.print("X Raw");
  Serial.print("\t");
 
  Serial.print("Y Raw");
  Serial.print("\t");
 
  Serial.print("Z Raw");
  Serial.print("\t");
 
  Serial.print("X Scaled");
  Serial.print("\t");
   
  Serial.print("Y Scaled");
  Serial.print("\t");
 
  Serial.println("Z Scaled");
  Serial.println("");
}
void PrintData (void){
  Serial.print(x);
  Serial.print("\t");
 
  Serial.print(y);
  Serial.print("\t");
 
  Serial.print(z);
  Serial.print("\t");
 
  Serial.print(Gx,3);
//  Serial.print(" G");
 
  Serial.print("\t");
 
  Serial.print(Gy,3);
//  Serial.print(" G");
  Serial.print("\t");
 
 
 
  Serial.print(Gz,3);
     
//  Serial.println(" G");
 
 
 
    Serial.println("");
}
 
 
//To Determine Offsets
  void DetermineOffsets(void){
    int x,y,z; 
    adxl.readAccel(&x, &y, &z);
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
 
    delay(3600);
  }
</source>
</source>



Revision as of 08:11, 6 April 2012

Project name <html><img src="/images/9/94/Report.png" border="0" /></html> Main project page
<html><img src="/images/c/c3/Resultset_previous.png" border="0" /></html>Previous entry<html>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html>Next entry<html><img src="/images/5/5c/Resultset_next.png" border="0" /></html>

Downloads

to be filled in with libraries, code, etc, later

Building a force gauge

About the ADXL345 and Arduino

Background reading on accelerometers. Our accelerometer of choice was the ADXL 345 from SparkFun Electronics. There are a bunch of code examples for the ADXL345, and the datasheet and quickstart guide were very helpful. The board we used to control and process data from the ADXL345 was an Arduino Uno R3.

Circuit

I connected the ADXL to the Arduino using a relatively simple circuit. I used the circuit setup from a bildr tutorial. The accelerometer came mounted on a breakout board, with holes where pins could go. I had to solder pins onto the breakout board

  • Arduino 3.3 v is connected to ADXL VCC and CS
  • Arduino ground is connected to ADXL ground
  • Arduino Analog In 4 (A4) is connected to ADXL SDA
  • Arduino Analog In 5 (A5) is connected to ADXL SCL

Development Environment

I wrote and uploaded my code to the Arduino and accelerometer using the Arduino 1.0 IDE on Ubuntu 11.04. Help for getting the integrated development environment running on Ubuntu can be found here. The language is very similar to C.

To write code for the ADXL in Arduino 1.0, one needs to have a library containing a .cpp and a .h file. The library is inserted into the libraries folder in the Arduino 1.0 directory. Essentially, a library contains subroutines one may find useful in writing their code and helps a programmer interface with a device, without having to worry about all the details.

I used an ADXL 345 library that I found on a bildr tutorial.

Code

<source lang ="c"> //Arduino 1.0+ Only! //Arduino 1.0+ Only!

  1. include <Wire.h>
  2. include <ADXL345.h>

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library int x,y,z; float Gx, Gy, Gz; int count=0;

void setup(){

 Serial.begin(9600);
 adxl.powerOn();
 adxl.setRangeSetting(2);  //G sensing range from -2g to +2g
 
 //set activity/ inactivity thresholds (0-255)
 adxl.setActivityThreshold(75); //62.5mg per increment
 adxl.setInactivityThreshold(75); //62.5mg per increment
 adxl.setTimeInactivity(6); // how many seconds of no activity is inactive?

 //look of activity movement on this axes - 1 == on; 0 == off 
 adxl.setActivityX(1);
 adxl.setActivityY(1);
 adxl.setActivityZ(1);

 //look of inactivity movement on this axes - 1 == on; 0 == off
 adxl.setInactivityX(1);
 adxl.setInactivityY(1);
 adxl.setInactivityZ(1);

 //setting all interupts to take place on int pin 1
 //I had issues with int pin 2, was unable to reset it
 adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,     ADXL345_INT1_PIN );
 adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT1_PIN );

 //register interupt actions - 1 == on; 0 == off  
 adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
 adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);

}


void loop(){

 //To see current register values of ADXL345
 //printAllRegister();
  Scale();
  Output();
 
 //getInterruptSource clears all triggered actions after returning value
 //so do not call again until you need to recheck for triggered actions
 byte interrupts = adxl.getInterruptSource();


 //inactivity
 if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
  Serial.println("inactivity");
  PrintHeadings();
}
 
 //activity
//if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
//  Serial.println("activity");  
  //delay(500);
  
  

// DetermineOffsets();


//}

}



void Scale (void){
adxl.readAccel(&x, &y, &z);
//CORRECTING FOR OFFSET OF ACCELEROMETER DUE TO POSITION
  x=x-12;
  y=y-18;
  z=z-245;
//SCALING IN G'S
  //scale = total g range / 1024
  //-2g to +2g = 4/1024 = 0.00390625, -4g to +4g = 8/1024 = 0.0078125
  //-8g to +8g = 16/1024 = 0.015625, -16g to +16g = 32/1024 = 0.03125
  Gx = x * 0.0039;
  Gy = y * 0.0039;
  Gz = z * 0.0039;
  
 } 
 
 
 
void Output(void){
  PrintData();
 }
  
  
  
  
  
  
void PrintHeadings(void){
//if(count == 0){ 
 Label();

// }

//if (count<50){

//count = count++;

//}
//if (count == 50){
 // count = 1;
 // Label();
//}
}
  
void Label (void){
  Serial.print("X Raw");
  Serial.print("\t");
  
  Serial.print("Y Raw");
  Serial.print("\t");
  
  Serial.print("Z Raw");
  Serial.print("\t");
  
  Serial.print("X Scaled");
  Serial.print("\t");
   
  Serial.print("Y Scaled");
  Serial.print("\t");
  
  Serial.println("Z Scaled"); 
  Serial.println(""); 
}

void PrintData (void){
  Serial.print(x);
  Serial.print("\t");
  
  Serial.print(y);
  Serial.print("\t");
  
  Serial.print(z);
  Serial.print("\t");
  
  Serial.print(Gx,3);
//  Serial.print(" G");
  
  Serial.print("\t");
  
  Serial.print(Gy,3);
//  Serial.print(" G");
  Serial.print("\t");
  
  
  
  Serial.print(Gz,3);
      
//  Serial.println(" G");
  
  
  
   Serial.println("");
}


//To Determine Offsets
 void DetermineOffsets(void){
   int x,y,z;  
   adxl.readAccel(&x, &y, &z); 
   Serial.println(x);
   Serial.println(y);
   Serial.println(z);
  
   delay(3600);
 }

</source>