How to save voltage data as a array in Matlab, using a Arduino MEGA 2560 board?

조회 수: 11 (최근 30일)
Hello everyone
I appreciate if you can help me with this issue.
I'm working with Arduino MEGA 2560 and I'm using the followings commands:
clc
clear all
a = arduino;
data = readVoltage(a,'A9');
So far, there is no problem (I'm using the analogic input A9). But I need some help with, How can I get an array with the measurement of the reading voltage in Matlab, without using Simulink and Arduino software, only Matlab?
I'm using a battery of 1.7 volts approx. as an example, next I need to apply this commands to a motor servo DC (Analogic Control System ACS-1000 from K and H fabricants.
I'm looking forward for your comments and help.
Thanks very much in advance.

채택된 답변

Madhu Govindarajan
Madhu Govindarajan 2019년 1월 15일
Let's say you know how long of a vector you need, then use indexing.
Example -
for i = 1:100
data(i) = readVoltage(a,'A9');
pause(0.1);
end
Note the pause just pauses MATLAB and you might notice that the whole thing takes more than 10 seconds.
If you want to collect data till you press a pushbutton on the Arduino.
buttonPressed = false;
data = readVoltage(a,'A9');
while ~buttonPressed
data(end+1) = readVoltage(a,'A9');
buttonPressed = readDigitalPin(a,'D13'); % Here it is assumed that you have a push button connected to Pin 13.
pause(0.1);
end
  댓글 수: 3
Madhu Govindarajan
Madhu Govindarajan 2019년 1월 16일
for loops are generally used when you know you have to loop through and repeat an activity a fixed number of times. In this case, repeat and create a vector of size 100. It could be used for anything.
The pause is to just ensure that you are not reading too quickly, you can do without it too.
While loops are used when you want something to repeat as long as a condition is true. So without the pushbutton you will have to come up with someother way to break your condition and make it false. Depends on how you set up your problem.
This page might help understand about different loops - https://www.mathworks.com/help/matlab/matlab_prog/loop-control-statements.html
Jonathan Bijman
Jonathan Bijman 2019년 1월 16일
Thank you very much. You really help me.
Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by