Reading two voltages from arduino.

조회 수: 4 (최근 30일)
Tomas
Tomas 2012년 6월 5일
Hello, I'm trying to read two voltage using the fscanf function to read two voltages in arduino which display like the following using the arduino monitor serial I get: 1022.00,1022.00 1023.00,1022.00 1023.00,1022.00 etc.. Yet when I try to read the two values I use the following code: a=fscanf(s,'%d,%d'); v1(i)=a(1)*5/1024; % 5/1024 is a constant v2(i)=a(2)*5/1024; % 5/1024 is a constant
but for a(2) in the v2(i) line It mentions me that: ??? Attempted to access a(2); index out of bounds because numel(a)=1. giving me the Idea that a is only taking one of the two voltages, so I was wondering how to make the a take the two values?

채택된 답변

Geoff
Geoff 2012년 6월 5일
Probably because you're asking it to read integers (%d), not floats (%f). Make that change.
Also, beware of MatLab's fscanf behaviour. If you only want to read one pair of numbers, you have to do:
fscanf(s, '%f,%f', 1);
  댓글 수: 1
Tomas
Tomas 2012년 6월 5일
oh you are right about the float situation now it seems to work fine and it graphs well! thanks a lot!

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

추가 답변 (1개)

Edmond
Edmond 2013년 2월 1일
Hi, I have the same problem now. It's the first time I am using arduino and Matlab together. I change the integer for floats and there is no response. Sometimes the program is running but giving the wrong values and something not.
--->Attempted to access a(2); index out of bounds because numel(a)=1.
Error in Morduino (line 41) v2(i)=a(2)*5/1024;

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by