arduino multiple pin control simultaneously

29 views (last 30 days)
kumar gautam
kumar gautam on 10 Jan 2018
Commented: 煜霄· on 8 Sep 2023
Hi Everyone,
I am struck on this problem from very long time. Please help me with this. I have the Arduino Circuit, and I am getting the dynamically updated text file. What I need to do is :- when the text file is updated. I need to read the file. The text file is updated with numbers. Each number has to be read in the MATLAB. Then the MATLAB will switch ON that particular pin for some specified time and then switch it off. The problem with MATLAB is that "for loop is read sequentially", which switches ON the LED one after the other. In our case I want to control the PINS of Arduino at the same time. To tackle this problem I used the Parfor loop. But the problem with the code
% Reading the text file
file1=fopen('text1.txt','r');
formatSpec='%f';
A=fscanf(file1,formatSpec);
%%arduino
b1=1;b2=3;
a =arduino('COM9','Uno');
[m,n1]=size(A);
m=int16(m);
parfor i=1:m
%voltage=readVoltage(a,'A0');%%this we are going to use for the reading of the photoresistor
%if (b2>b1)%(voltage>200)%%this is when the arduino enters into the sensor
for i1=1:5
%opertion_arduino(n,a);
%opertion_arduino((n+1),a);
switch i1
case {1}
writeDigitalPin(a,'D13',1);
pause(5)
writeDigitalPin(a,'D13',0);
case {2}
writeDigitalPin(a,'D12',1);
pause(5)
writeDigitalPin(a,'D12',0);
case {3}
writeDigitalPin(a,'D11',1);
pause(5)
writeDigitalPin(a,'D11',0);
case{4}
writeDigitalPin(a,'D10',1);
pause(5)
writeDigitalPin(a,'D10',0);
case{5}
writeDigitalPin(a,'D9',1);
pause(5)
writeDigitalPin(a,'D9',0);
end
%elseif(b2<b1) %(voltage<200)%%this is used as the passing statement for not doing anything
end
end
the error which I am getting is the

Answers (1)

Walter Roberson
Walter Roberson on 10 Jan 2018
This is not something that can be done on a single Arduino. Even if you could have multiple processes communicating with the same Arduino (not possible over a single serial port) then you would run into the fundamental problem that the Arduino itself needs to execute I/O instructions at the hardware level to make changes to hardware pins, and those I/O instructions can only affect one device at a time.
In order to do this you would need multiple Arduino, one controlling each device that might have to be activated simultaneously. You would also need to write some C code (or a 'sketch') to permit hardware synchronization of the arduino, with the flow of logic being that you would distribute the instructions about what had to be done to each of the arduino, and then they would pause waiting for an interrupt, and then you would (somehow) have the host trigger the hardware interrupt that was received simultaneously (to within electrical speeds) by all of the Arduino, which would then immediately put the I/O instruction into effect so that the changes happened as simultaneously as was feasible. This is like giving everyone instructions and saying "On your mark, get set, Go!"
  3 Comments
Walter Roberson
Walter Roberson on 5 Sep 2023
Does matlab really cannot control mutiple pin at the same time?
writeDigitalPin(a, 'A0', 1);
Each time you execute a MATLAB command such as this one, MATLAB uses "Serial over USB" to communicate with the Arduino. This involves making some calls to the host USB driver to queue instructions on the USB Controller hardware. The USB Controller does not immediately send the data to the USB bus: the USB Controller has a strict cycle.
About 1000 times per second, the USB Controller asks the USB bus, "Are there any new devices out there?" and waits briefly for a response. If there are none, then the USB Controller sends a message to each known device in turn saying either "I have this kind and size of data available, are you ready to receive it?" or "I do not have anything for you, what kind and size of data do you have for me?". It does not send the data itself at that time, and each device does not yet send whatever data it may have available. The controller gathers all of the "I'm ready to receive data" or "I'm ready to send XXX" responses, and it prioritizes them and deals with the highest priority first, either by sending "Okay device YYYY, here's a burst of data" or "Okay device YYYY, send a burst of data". Only one device at a time can talk on the USB bus, and the Controller is responsible for telling each device in turn when to talk or when to receive data.
The arduino in turn has a USB interface chip that is not as advanced as a USB controller, but which knows how to hold the other half of the conversations -- to tell the controller what kind and size of data is available when it is asked for the information, and to receive or transmit data when the controller tells it.
None of this is instant; it all takes a variable amount of time. More variable when you consider that the host MATLAB program needs to make library calls on a system that is doing other things at the same time, with contention for internal busses, and contention with other devices also using the same USB controller. You can do statistical measurements about "typical" time to get communications over to the arduino... but if it happens to be time for Microsoft Outlook to paint three new advertisements then Microsoft Windows is probably going to give priority to the advertisements.
What MATLAB is going to send to the arduino is going to be a binary (or possibly text) command that requests that the arduino takes some action. There is a program running on the arduino that is listening on the serial port and decoding the commands send by MATLAB, chosing an appropriate branch of the code running on the arduino, and eventually taking appropriate actions. MATLAB is not sending machine instructions. It takes time for the arduino to decode the instruction send by MATLAB, and time to execute the machine instructions.
MATLAB would (probably) have continued executing and finding the
writeDigitalPin(a, 'A8', 1);
and queuing up appropriate commands to the USB controller. Because of the way that USB runs in cycles, and the way that Serial Over USB works, the second command might also get sent in the same communications packet as the previous one -- but this is not at all certain. The time between receiving each writeDigital from MATLAB is going to be variable, so of course the time interval between when the arduino can start executing the different commands is going to be variable.
At the arduino level, even if the arduino received a single communications packet that had all 5 instructions in them, there would be no way that the the arduino could trigger the hardware of different pins at the same time: the CPUs used by the arduino do not have any hardware instructions along the lines of "Turn on lines 0, 3, 8, 11, and 20 now".
Oh, and it's time to update the ads on Bing.
Wait wait -- it's time to update the ads on Minesweeper.
pause(0.35);
It was a variable time between when you started the writeDigital and when you got here. And it turns out that pause() is a lot less consistent than you would expect...
And then you start turning off the lines, with all the overheads and timing uncertainties involved.
No, MATLAB does not offer any way to combine writeDigital -- you cannot, for example, say,
writeDigitalPin(a, {'A0', 'A8', 'D3', 'D8', 'D20'}, 1)
... it probably could, but it doesn't.
And as I indicated, even if it could, it is impossible at the arduino hardware level to trigger multiple pins with the same hardware instruction.
If you are doing this kind of work, I firmly recommand that you bypass the writeDigitalPin() kind of layer, and do your own communications with the arduino, talking to C or C++ code that you have written on the arduino side that does exactly what you want. When you write the code in C/C++ yourself on the arduino side, you can include several direct hardware-library calls in a row, not literally changing multiple pins at the same time, but changing them in sequence as fast as hardware instructions would support, without the overhead of talking to USB controllers for every request.
煜霄·
煜霄· on 8 Sep 2023
Thank you for your answer and suggestion, that help me so much.

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!