Page 1 of 1
Digital outputs automated in a MEGA
Posted: Sat Sep 06, 2025 11:07 pm
by Billduck1302
I need to sample 14 voltages on a Lithium battery pack. I am using a relay board to address individual cells. For example, to get the Cell 1 voltage, I send "0" to a relay board pin, and "0" to another relay board pin. I note that the "output" command icon only allows the selection of a pin number (eg D22) and the value, 0 or 1.
It would be nice if I could have the pin numbers in an array and choose which element of the array to populate the "output" command icon pin #, such as "D22".
Thanks in advance.
Re: Digital outputs automated in a MEGA
Posted: Sat Sep 06, 2025 11:32 pm
by mnfisher
It's certainly possibly, if maybe cheating slightly...
First note that you can define pins in properties (as s single digital pin) and assign values in a calculation block
Pin = 1 to set pin high for example
Take a look at the code generated - i think it is set_pin('D', 2, 1); for d2 set highfor example.
Then use something like - on my phone so typing for is a pain...
Have an array of 2 character strings ("d2”, ”d3”...}
Then port is str[n][0] and pin is str [n][1] - '0'
The in a c block do set_pin(FCL_PORT, FCL_PIN, 1);
i will post on FC tomorrow...
Martin
Re: Digital outputs automated in a MEGA
Posted: Sun Sep 07, 2025 11:04 am
by mnfisher
Hi Bill,
Okay - a simple example - I used an Arduino UNO - but it will work without change on a Mega. You'll need to add more ports to the 'switch' statement in the SetPins macro. It only handles ports B, C and D here.
Here I show toggling a pin set in properties (just once!) and then an array of three members toggled repeatedly.
Note that a 'neater' (ie quicker) way to do this would be to calculate the port address from the name ('B','C' etc) and write directly to the MCU registers. This would be quicker than using the switch statement - I do this here
viewtopic.php?t=259&hilit=pullups - it uses the fact that the difference in address for each port is a constant.
Martin