I'm trying to send the voltage i'm sensing with arduino to matlab, i have tried and read many different ways, but i have to use the serial comunication. So i have to send the numbers to the serial port and then read them from matlab. Can anyone give me any clue of how to do it?
what i tried for the moment is the following:
matlab code:
comPort=('COM4');
[s, flag]=setupSerial(comPort);
v=fread(s,1,'uint8');
where the setupSerial function is:
function [s, flag] = setupSerial( comPort )
%SETUPSERIALPORT Summary of this function goes here
% Detailed explanation goes here
flag=1;
s=serial(comPort);
set (s,'DataBits',8);
set (s,'StopBits',1);
set (s,'BaudRate',9600);
set (s,'Parity','none');
fopen(s);
% a='b';
% while (a~='a')
% a=fread(s,1,'uchar');
% end
% if (a=='a')
% disp('serial read');
% end
% fprintf(s,'%c','a');
% fscanf(s,'%u');
end
and my code in arduino is:
/*
- Read Voltage Program
*/
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
int sensorValue = analogRead(A1); // Switch connected to digital pin 2
float analogVoltageNumber = sensorValue*(5.0/1023.0); //converts the sensor value to voltage
string analogVoltage;
Serial.println(analogVoltage); // Read the pin and display the value
}
void loop() // run over and over again
{
}
I have been googleing trying to find out a way, but i think there's something about how the comunication works that i haven't understood.