Ruben's blog

Nothing to say

Arduino Library for MMA7260Q (Tilt Sensor)

| Comments

In the past days I published a couple of arduino libraries. All coming from by XBee sensor development. I’m still waiting for the MAX756 to arrive from Futurlec, I need the MAX756 to power the xbee sensor with batteries and mount it in its final position. So in the meanwhile I’m cleaning a little bit the code, moving thing to libraries, etc. Now it’s the turn of the MMA7260Q, (see other posts about MMA7260q) .

I created an Arduino library to use MMA7260Q as a Tilt Sensor that does autozero calibration.

The library contains an example that can be accessed through the File ⇒ Sketchbook ⇒ Examples ⇒ Library-Mma7260q ⇒ SimpleTiltSensor.

SimpleTiltSensor library

The example looks like this (also available as gist):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "mma7260q.h"

//set up MMA7260Q
Mma7260q mma7260q = Mma7260q(0,1,2);

long time = millis();

void setup()  {  
  
  Serial.begin(9600);
  delay(100);
  
  
  //initial calibration of the MMA7260q 
  // you can get the correct values for your MMA7260q
  // by running this program and copying the max and min values
  // after rotating the mma7260q 360degrees around all axis
  mma7260q.autoZeroCalibration(173,192,258);
  mma7260q.autoZeroCalibration(766,720,914);
  
  
  Serial.print("RST\r\n");delay(100);  
}


void loop() 
{
     float rho, phi,theta;
     mma7260q.readTilt(&rho,&phi,&theta);
     
     if ((millis()-time)>2000)
     {
       time = millis();
       Serial.print("rho : ");
       printFloat(rho,3);
       Serial.print(" phi : ");
       printFloat(phi,3);
       Serial.print(" the: ");
       printFloat(theta,3);
       Serial.print("\r\n");    
 
       int minx,miny,minz,maxx,maxy,maxz;
       mma7260q.getMinValues(&minx,&miny,&minz);
       mma7260q.getMaxValues(&maxx,&maxy,&maxz);
       Serial.print("minx: ");
       Serial.print((int)minx,DEC);
       Serial.print(" miny: ");
       Serial.print((int)miny,DEC);
       Serial.print(" minz: ");
       Serial.print((int)minz,DEC);
       Serial.print("\r\n");
  
       Serial.print("maxx: ");
       Serial.print((int)maxx,DEC);
       Serial.print(" maxy: ");
       Serial.print((int)maxy,DEC);
       Serial.print(" maxz: ");
       Serial.print((int)maxz,DEC);
       Serial.print("\r\n");
 
     }
}

void printFloat(float value, int places) {
   // this is used to cast digits
   int digit;
   float tens = 0.1;
   int tenscount = 0;
   int i;
   float tempfloat = value;

   // if value is negative, set tempfloat to the abs value
   // make sure we round properly. this could use pow from  
   //<math.h>, but doesn't seem worth the import
   // if this rounding step isn't here, the value  54.321 prints as  
   //54.3209

   // calculate rounding term d:   0.5/pow(10,places)
   float d = 0.5;
   if (value < 0)
     d *= -1.0;
   // divide by ten for each decimal place
   for (i = 0; i < places; i++)
     d/= 10.0;
   // this small addition, combined with truncation will round our  
   // values properly
   tempfloat +=  d;

   // first get value tens to be the large power of ten less than value
   // tenscount isn't necessary but it would be useful if you wanted  
   // to know after this how many chars the number will take

   if (value < 0)
     tempfloat *= -1.0;
   while ((tens * 10.0) <= tempfloat) {
     tens *= 10.0;
     tenscount += 1;
   }


   // write out the negative if needed
   if (value < 0)
     Serial.print('-');

   if (tenscount == 0)
     Serial.print(0, DEC);

   for (i=0; i< tenscount; i++) {
     digit = (int) (tempfloat/tens);
     Serial.print(digit, DEC);
     tempfloat = tempfloat - ((float)digit * tens);
     tens /= 10.0;
   }

   // if no places after decimal, stop now and return
   if (places <= 0)
     return;

   // otherwise, write the point and continue on
   Serial.print('.');

   // now write out each decimal place by shifting digits one by one  
   // into the ones place and writing the truncated value
   for (i = 0; i < places; i++) {
     tempfloat *= 10.0;
     digit = (int) tempfloat;
     Serial.print(digit,DEC);
     // once written, subtract off that digit
     tempfloat = tempfloat - (float) digit;
   }
}

References:

Comments