sometimes the function triggers when i trigger the sensor, sometimes it triggers without me triggering it, sometimes it won’t trigger even through multiple attempts.
hi, I am new to this and I have problem with my function. For some reason the software can’t sense when I trigger the sensor or it triggers without me triggering the sensor. The result was so random and I couldn’t find the problem. Thank you for reading this.
function [accelDataArray, accelDataArray2] = collectSamples(serialObject1, sampleNum, serialObject2, sampleNum2)
sampleCounter1 = 1;
accelDataArray = zeros(sampleNum, 1);
sampleCounter2 = 1;
accelDataArray2 = zeros(sampleNum2, 1);
triggered = false; % Initialize the trigger condition
% Wait for the trigger condition (preliminary loop)
disp(‘Waiting for trigger…’);
%%fix 282 to 312
while ~triggered
if serialObject1.BytesAvailable < 20 || serialObject2.BytesAvailable < 20
continue;
end
header1 = fread(serialObject1, 1);
header2 = fread(serialObject2, 1);
if header1 ~= 85 || header2 ~= 85
fread(serialObject1, 1);
fread(serialObject2, 1);
continue;
end
dataBytes1 = fread(serialObject1, 19);
dataBytes2 = fread(serialObject2, 19);
if dataBytes1(1) ~= 97 || dataBytes2(1) ~= 97
continue;
end
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
if az > .1 || az2 > .1 % Trigger condition, adjust the threshold as needed
triggered = true;
disp(‘Triggered, now collecting samples…’);
end
end
% Data collection loop
while sampleCounter1 <= sampleNum || sampleCounter2 <= sampleNum2
% Read data from sensor 1
if serialObject1.BytesAvailable >= 20
header1 = fread(serialObject1, 1);
if header1 == 85
dataBytes1 = fread(serialObject1, 19);
if dataBytes1(1) == 97
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
accelDataArray(sampleCounter1) = az;
sampleCounter1 = sampleCounter1 + 1;
end
end
end
% Read data from sensor 2
if serialObject2.BytesAvailable >= 20
header2 = fread(serialObject2, 1);
if header2 == 85
dataBytes2 = fread(serialObject2, 19);
if dataBytes2(1) == 97
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
accelDataArray2(sampleCounter2) = az2;
sampleCounter2 = sampleCounter2 + 1;
end
end
end
end
endhi, I am new to this and I have problem with my function. For some reason the software can’t sense when I trigger the sensor or it triggers without me triggering the sensor. The result was so random and I couldn’t find the problem. Thank you for reading this.
function [accelDataArray, accelDataArray2] = collectSamples(serialObject1, sampleNum, serialObject2, sampleNum2)
sampleCounter1 = 1;
accelDataArray = zeros(sampleNum, 1);
sampleCounter2 = 1;
accelDataArray2 = zeros(sampleNum2, 1);
triggered = false; % Initialize the trigger condition
% Wait for the trigger condition (preliminary loop)
disp(‘Waiting for trigger…’);
%%fix 282 to 312
while ~triggered
if serialObject1.BytesAvailable < 20 || serialObject2.BytesAvailable < 20
continue;
end
header1 = fread(serialObject1, 1);
header2 = fread(serialObject2, 1);
if header1 ~= 85 || header2 ~= 85
fread(serialObject1, 1);
fread(serialObject2, 1);
continue;
end
dataBytes1 = fread(serialObject1, 19);
dataBytes2 = fread(serialObject2, 19);
if dataBytes1(1) ~= 97 || dataBytes2(1) ~= 97
continue;
end
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
if az > .1 || az2 > .1 % Trigger condition, adjust the threshold as needed
triggered = true;
disp(‘Triggered, now collecting samples…’);
end
end
% Data collection loop
while sampleCounter1 <= sampleNum || sampleCounter2 <= sampleNum2
% Read data from sensor 1
if serialObject1.BytesAvailable >= 20
header1 = fread(serialObject1, 1);
if header1 == 85
dataBytes1 = fread(serialObject1, 19);
if dataBytes1(1) == 97
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
accelDataArray(sampleCounter1) = az;
sampleCounter1 = sampleCounter1 + 1;
end
end
end
% Read data from sensor 2
if serialObject2.BytesAvailable >= 20
header2 = fread(serialObject2, 1);
if header2 == 85
dataBytes2 = fread(serialObject2, 19);
if dataBytes2(1) == 97
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
accelDataArray2(sampleCounter2) = az2;
sampleCounter2 = sampleCounter2 + 1;
end
end
end
end
end hi, I am new to this and I have problem with my function. For some reason the software can’t sense when I trigger the sensor or it triggers without me triggering the sensor. The result was so random and I couldn’t find the problem. Thank you for reading this.
function [accelDataArray, accelDataArray2] = collectSamples(serialObject1, sampleNum, serialObject2, sampleNum2)
sampleCounter1 = 1;
accelDataArray = zeros(sampleNum, 1);
sampleCounter2 = 1;
accelDataArray2 = zeros(sampleNum2, 1);
triggered = false; % Initialize the trigger condition
% Wait for the trigger condition (preliminary loop)
disp(‘Waiting for trigger…’);
%%fix 282 to 312
while ~triggered
if serialObject1.BytesAvailable < 20 || serialObject2.BytesAvailable < 20
continue;
end
header1 = fread(serialObject1, 1);
header2 = fread(serialObject2, 1);
if header1 ~= 85 || header2 ~= 85
fread(serialObject1, 1);
fread(serialObject2, 1);
continue;
end
dataBytes1 = fread(serialObject1, 19);
dataBytes2 = fread(serialObject2, 19);
if dataBytes1(1) ~= 97 || dataBytes2(1) ~= 97
continue;
end
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
if az > .1 || az2 > .1 % Trigger condition, adjust the threshold as needed
triggered = true;
disp(‘Triggered, now collecting samples…’);
end
end
% Data collection loop
while sampleCounter1 <= sampleNum || sampleCounter2 <= sampleNum2
% Read data from sensor 1
if serialObject1.BytesAvailable >= 20
header1 = fread(serialObject1, 1);
if header1 == 85
dataBytes1 = fread(serialObject1, 19);
if dataBytes1(1) == 97
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), ‘int16’)) / 32768 * 16;
accelDataArray(sampleCounter1) = az;
sampleCounter1 = sampleCounter1 + 1;
end
end
end
% Read data from sensor 2
if serialObject2.BytesAvailable >= 20
header2 = fread(serialObject2, 1);
if header2 == 85
dataBytes2 = fread(serialObject2, 19);
if dataBytes2(1) == 97
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), ‘int16’)) / 32768 * 16;
accelDataArray2(sampleCounter2) = az2;
sampleCounter2 = sampleCounter2 + 1;
end
end
end
end
end #sensor MATLAB Answers — New Questions