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