void loop()
{
int a;
int value = 0;
// a =readValue( 3 );
//3 is reference voltage
value = gForce( xAxis );
Serial.print( "x = " );
Serial.print( value );
//a =readValue( 3 );
value = gForce( yAxis );
Serial.print( "\ty = " );
Serial.print( value );
//a =readValue( 3 );
value = gForce( zAxis );
Serial.print( "\tz = " );
Serial.println( value );
delay( 200 ); // wait 1sec
}
int gForce( int adcValue, int refValue )
{
if( adcValue >= refValue )
return umulf( adcValue - refValue, GfCnv );
else
return -umulf( refValue - adcValue, GfCnv );
}
int gForce( int axis )
{
int refValue = readValue( 3 );
//Serial.println(refValue);
//delay( 1 ); // wait 1msec
int adcValue = readValue( axis );
//Serial.println(adcValue);
return gForce( adcValue, refValue );
}
int readValue( int channel )
{
int data = 0;
int sig = ( ( 0x0018 + channel ) << 11 );
digitalWrite(csPin, LOW);
myShiftOut(dioPin, clkPin, 5, 1, sig );
//delayMicroseconds( 200 );
data = myShiftIn( dioPin, clkPin, 13 );
digitalWrite( csPin, HIGH );
//Serial.println(data);
return data;
}
void myShiftOut(int dataPin, int clockPin, int bitCount, int bitOrder, int val ) {
pinMode( dataPin, OUTPUT );
for( int i = 0; i < bitCount; i++) {
if (bitOrder < 0) { // if shiftout from LSB
digitalWrite( dataPin, ( val & 1) );
delayMicroseconds( 50 );
digitalWrite( clockPin, HIGH );
val = val >> 1;
}
else { // else shiftout from MSB
digitalWrite( dataPin, ((val >> 15)&1) );
delayMicroseconds( 50 );
digitalWrite( clockPin, HIGH );
val = val << 1;
}
digitalWrite( clockPin, LOW );
}
}
int myShiftIn(int dataPin, int clockPin, int bitCount) {
int pinState = 0;
int dataIn = 0;
pinMode( clockPin, OUTPUT );
pinMode( dataPin, INPUT );
for(int i = 0; i < bitCount; i++) {
int temp = -1;
digitalWrite(clockPin, HIGH) ;
//delayMicroseconds( 20 );
temp = digitalRead( dataPin );