### Author Topic: Accelerometer Code Help  (Read 468 times)

#### dmm

• Newbie
• Posts: 17
##### Accelerometer Code Help
« on: November 08, 2016, 02:49:57 pm »
Hello everyone, I am attempting to implement a code that will take readings off the accelerometer and put them out in angles of pitch and roll.

I'm getting stuck because it is telling me that Dim is not acceptable for defining some variables and it needs to be Public or Private. So I tried using Public and now it is telling me that the variables aren't defined. I'm stuck on this, so if anyone has some suggestions I would appreciate it.

Here is the code:

Dim AccelX As Float = accel.Accel(LIS2DH12.X_AXIS)
Dim AccelY As Float = accel.Accel(LIS2DH12.Y_AXIS)
Dim AccelZ As Float = accel.Accel(LIS2DH12.Z_AXIS)

Dim pitch As Float = pitchCalc(AccelX, AccelY, AccelZ)
Dim roll As Float = rollhCalc(AccelX, AccelY, AccelZ)

Private Function pitchCalc(x As Float, y As Float, z As Float) As Float
Return (((x / (y * y + z * z).Sqrt).ATan) * 180.0) / Float.Pi
End Function

Private Function rollCalc(x As Float, y As Float, z As Float) As Float
Return ((y / (x * x + z * z).Sqrt).ATan* 180.0) / Float.Pi
End Function

Then from there I would just read out pitch and roll and transmit it.

#### dmm

• Newbie
• Posts: 17
##### Re: Accelerometer Code Help
« Reply #1 on: November 08, 2016, 05:03:28 pm »
Hey everyone,

I got some great help and thought I would share the code here:

Class TiltMeter

Shared accel As LIS2DH12

Shared Event Boot()
accel = New LIS2DH12
accel.Setup(LIS2DH12.HIGH_RESOLUTION_MODE, LIS2DH12.DATA_RATE_100HZ, LIS2DH12.SCALE_2G)
End Event

Shared Event checkTilt() RaiseEvent Every 30 Seconds
Dim AccelX As Float = accel.Accel(LIS2DH12.X_AXIS)
Dim AccelY As Float = accel.Accel(LIS2DH12.Y_AXIS)
Dim AccelZ As Float = accel.Accel(LIS2DH12.Z_AXIS)

Dim pitch As Float = pitchCalc(AccelX, AccelY, AccelZ)
Dim roll As Float = rollCalc(AccelX, AccelY, AccelZ)
'here is where you input your code for sending the pitch and roll values
End Event

Private Function pitchCalc(x As Float, y As Float, z As Float) As Float
Return (((x / (y * y + z * z).Sqrt).ATan) * 180.0) / Float.Pi
End Function

Private Function rollCalc(x As Float, y As Float, z As Float) As Float
Return ((y / (x * x + z * z).Sqrt).ATan* 180.0) / Float.Pi
End Function

End Class

Thanks McThings!
Like x 1 View List