Hi there as many people I am new to Arduino programming and have been doing research but to no avail. I have a project where I need to connect and Arduino Uno and an Arduino Nano to 1 ADXL 345 accelerometer. I need to be able to get the same x y z coordinates displayed in visual basic using 1 form and 2 graphs, I also need to be a be able to export these values for further analyzing. Since I am using two Arduino Boards I am also force to utilizing 2 com ports. The way I am accomplishing this goal is by running 2 Arduino IDE program simultaneously, however there is a big issue with lag. Is there a way to be able to read the data simultaneously and send it to visual studio without any lag?
The second issue that I'm also experiencing seems to be with VB or Arduino I'm not sure but just in case I can also get help on that I will post the question as well. I notice that there are times when Visual studio seem to not grab the X1 at the right time. For example my X1 Value should always be the first value sent to VB by Arduino but there are times where the first value sent is the Y1 or at times the Z1 therefore I had to implement a loop to check all 3 lines to be able to locate the X1 value. There has been instances where the Data given is as shown "X1=0.03 Y1=0.05 Z1=3.02" which is perfect displayed but then on the second loop it would probably show as "=0.03 Y1=0.05 Z1=3.02" it would erase the remaining values or even erase the X1 so in this instance none of my loops would locate and X1 value and it would just use the last recorded value stored in X1 which is inaccurate, I've found that if I delay by 100 in Arduino and collect the data in VB at 10 interval this issue doesn't occur. However my professor has informed me that no delay can be used in vibration. Can someone determine if this a coding problem in Arduino? is it a code issue with Arduino or VB? Note to save space I've deleted the VB codes i deemed unnecessary, the VB code given shows in what variable the data is stored and how it is displayed in the TEXTBOX for both COM PORT 5 and 6.
Code 1
/*
Arduino and ADXL345 Accelerometer Tutorial
*/
#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
float X1, Y1, Z1;
void setup() {
Serial.begin(9600); // 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();
}
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
X1 = X_out / 256; //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
Y1 = Y_out / 256;
Z_out = ( Wire.read() | Wire.read() << 8); // Z-axis value
Z1 = Z_out / 256;
Serial.print("X1");
Serial.println(X1);
Serial.print("Y1");
Serial.println(Y1);
Serial.print("Z1");
Serial.println(Z1);
}
Code 2
/*
Arduino and ADXL345 Accelerometer Tutorial
*/
#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
float X2,Y2,Z2;
void setup() {
Serial.begin(9600); // 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();
}
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
X2 = X_out/256; //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
Y2 = Y_out/256;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z2 = Z_out/256;
Serial.print("X2");
Serial.println(X2);
Serial.print("Y2");
Serial.println(Y2);
Serial.print("Z2");
Serial.println(Z2);
}
VB CODE
Public Class Form1
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 StrSerialIn2 As String = SerialPort2.ReadExisting
Dim StrSerialInRam2 As String
Dim TB As New TextBox
TB.Multiline = True
TB.Text = StrSerialIn
Dim TB2 As New TextBox
TB2.Multiline = True
TB2.Text = StrSerialIn2
'--------------------------COM 5
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "X1" Then
X_1 = TB.Lines(0)
X_1L = X_1.Length
Else
StrSerialInRam = TB.Lines(1).Substring(0, 2)
If StrSerialInRam = "X1" Then
X_1 = TB.Lines(1)
X_1L = X_1.Length
Else
StrSerialInRam = TB.Lines(2).Substring(0, 2)
If StrSerialInRam = "X1" Then
X_1 = TB.Lines(2)
X_1L = X_1.Length
End If
End If
End If
'-------------------
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "Y1" Then
Y_1 = TB.Lines(0)
Y_1L = Y_1.Length
Else
StrSerialInRam = TB.Lines(1).Substring(0, 2)
If StrSerialInRam = "Y1" Then
Y_1 = TB.Lines(1)
Y_1L = Y_1.Length
Else
StrSerialInRam = TB.Lines(2).Substring(0, 2)
If StrSerialInRam = "Y1" Then
Y_1 = TB.Lines(2)
Y_1L = Y_1.Length
End If
End If
End If
'----------------
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "Z1" Then
Z = TB.Lines(0)
ZL = Z.Length
Else
StrSerialInRam = TB.Lines(1).Substring(0, 2)
If StrSerialInRam = "Z1" Then
Z = TB.Lines(1)
ZL = Z.Length
Else
StrSerialInRam = TB.Lines(2).Substring(0, 2)
If StrSerialInRam = "Z1" Then
Z = TB.Lines(2)
ZL = Z.Length
End If
End If
End If
LabelX.Text = "X1 = " & Mid(X_1, 3, X_1L)
LabelY.Text = "Y1 = " & Mid(Y_1, 3, Y_1L)
LabelZ.Text = "Z1 = " & Mid(Z, 3, ZL)
'--------------------------------------------------------------------------------------------- COM 6
StrSerialInRam2 = TB2.Lines(0).Substring(0, 2)
If StrSerialInRam2 = "X2" Then
X_2 = TB2.Lines(0)
X_2L = X_2.Length
Else
StrSerialInRam2 = TB2.Lines(1).Substring(0, 2)
If StrSerialInRam2 = "X2" Then
X_2 = TB2.Lines(1)
X_2L = X_2.Length
Else
StrSerialInRam2 = TB2.Lines(2).Substring(0, 2)
If StrSerialInRam2 = "X2" Then
X_2 = TB2.Lines(2)
X_2L = X_2.Length
End If
End If
End If