Adxl 345 Visual Basic real time Vibration plotting

I have a project that requires me to grab the xyz values from arduino and plot them in Visual studio. I have manage to get it working but not real time. a delay is required to capture and plot the values in Vb this causes an issue because it reduces the samples to 12 samples per second which is unacceptable

#include <Wire.h>  // Wire library - used for I2C communication
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;  // Outputs
void setup() {
  Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
  Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10);
}
void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out/32; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out/32;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/32;
  Serial.print("X1 ");
  Serial.print(X_out);
  Serial.print("Y1");
  Serial.print(Y_out);
  Serial.print("Z1");
  Serial.println(Z_out);
}

When I use a delay the values are sent in the right order XYZ but without a delay the values seems to be randomly sent to VB such as
VB Value Example 1
Y:05, Z:10 and X:10
VB Value Example 2
:10, Y:05 and Z1:10

This is a problem since my VB code tries to find XYZ then grabs the value but since there is no X in the second example the program would completely skip the x value. I tried adjusting the frequency and sensitivity and baud rate but it hasn't fixed the random issue. Is there a way to plot these values real time without delay and fix the random value issues?

Vb Code that grabs XYZ to be plotted

 Private Sub TimerSerial_Tick(sender As Object, e As EventArgs) Handles TimerSerial.Tick
        Try
            Dim StrSerialIn As String = SerialPort1.ReadExisting
            Dim StrSerialInRam As String

            Dim TB As New TextBox
            TB.Multiline = True
            TB.Text = StrSerialIn

            StrSerialInRam = TB.Lines(0).Substring(0, 2)
            If StrSerialInRam = "X1" Then
                X_1 = TB.Lines(0)
                X_1L = X_1.Length
            Else
                X_1 = X_1
            End If

            StrSerialInRam = TB.Lines(1).Substring(0, 2)
            If StrSerialInRam = "Y1" Then
                Y_1 = TB.Lines(1)
                Y_1L = Y_1.Length
            Else
                Y_1 = Y_1
            End If

            StrSerialInRam = TB.Lines(2).Substring(0, 2)
            If StrSerialInRam = "Z1" Then
                Z = TB.Lines(2)
                ZL = Z.Length
            Else
                Z = Z
            End If

            If PictureBoxConnectionInd.Visible = True Then
                PictureBoxConnectionInd.Visible = False
            ElseIf PictureBoxConnectionInd.Visible = False Then
                PictureBoxConnectionInd.Visible = True
            End If

            LabelX.Text = "x = " & Mid(X_1, 3, X_1L)
            LabelY.Text = "y = " & Mid(Y_1, 3, Y_1L)
            LabelZ.Text = "z = " & Mid(Z, 3, ZL)
        Catch ex As Exception

        End Try
    End Sub

Perhaps you're sending it faster than VB can process and a buffer somewhere is being overflowed.

Try adding a substantial delay on the Arduino side and if it works, reduce it until it only just works :wink:

Another recourse is to get the excel spreadsheet, open the macros and see how the Author did his code

Thanks for the reply. I've observed the excel exporting code and it is just a basic for loop grabbing the values obtained from vb code I posted. I don't think the problem exist there but I'll surely look again.

Is there a way for me to for example take a maximum set of samples in one second then clear the buffer once that second has elapsed and then restore a new set of samples ? Would this help with the overflowing buffer issue ?

If the issue is that the Arduino serial output buffer is overflowing you can use Serial.flush() after your print statements to wait for the buffer to empty.

What is the timer interval?

In Vb I have it set to 1 interval.

I have it set to one interval in VB

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.