hey guys
im doing a project in which i scan a 15x15 grid using arduino. im trying to send the data in a line of 225 numbers than read that in using matlab, turn it into a matrix and make a 3d plot of the grid.
most of the time it will display fine but if i manipulate the sensor to change the data on too many points of the grid it will fail and not get enough points at the fscanf function in matlab (it always has 225 data points when i view the serial monitor through arduino).
I think its just a problem with how I am reading the data in matlab (maybe a syncing issue?)
any help would be appreciated
thanks!
arduino code:
int Signal[] = {
A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14};
int output[] = {
24,26,28,30,32,34,36,38,40,42,44,46,48,50,52};
int SenselReading[14]; //set an empty 15 point array
void setup()
{
Serial.begin(9600);
for (int i=0; i<=14 ;i++)
{
pinMode(Signal[i],INPUT); // set the analog input pins A0~A5 as the inputs
pinMode(output[i], OUTPUT); // set the digital pins 8,9,10 as the outputs
}
}
void loop()
{
for (int i = 0; i <=14; i++)
{
digitalWrite(output[i], HIGH);
// delay(10);
for (int j = 0; j<=14; j++)
{
if (analogRead(Signal[j]) <= 10)
{
SenselReading[j] = 1;
}
else{
SenselReading[j] = analogRead(Signal[j]);
}
if (i==14 && j==14)
{
Serial.println(SenselReading[j],DEC);
}
else{
Serial.print(SenselReading[j],DEC);
Serial.print("\t");
}
delay(3);
}
digitalWrite(output[i], LOW);
}
}
matlab code:
clc;
clear all;
delete(instrfindall)
s = serial('/dev/tty.usbmodem1421'); % define the 's' as the Arduino port
set(s,'BaudRate',9600); % set the frequence of the signal
set(s,'DataBits',8); % set the data bits
set(s,'Parity','none');
out=zeros(1,225);
fopen(s); % open the Arduino port and make it accessable to get data from Arduino to Matlab
% i = 0;
pause(1);
while 1 % keep moving all the way and get data in real-time
try
line = fscanf(s) % get data if there exists data in the next line
length(line)
catch
fclose(s); % close it when accident happens
end
try
out = strread(line); % read the data from the 'line'
length(out)
catch
fclose(s);
end
%this block turns it into a matrix and interpolates, then plots the data
hold on
if length(out) == 225
A = [out(1:15); out(16:30); out(31:45);out(46:60);out(61:75);...
out(76:90);out(91:105);out(106:120);out(121:135);out(136:150);...
out(151:165);out(166:180);out(181:195);out(196:210);out(211:225)];
[X,Y] = meshgrid(1:1:15,1:1:15);
[Xq,Yq] = meshgrid(1:.25:15);
Vq = interp2(X,Y,A,Xq,Yq,'cubic');
surf(Xq,Yq,Vq)
end
hold on
drawnow
box on
grid on
axis on
caxis([-50 500])
colorbar;
axis([1 15 1 15 -10 500]);
cla
end