Digital outputs automated in a MEGA

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
Post Reply
Billduck1302
Posts: 18
http://meble-kuchenne.info.pl
Joined: Fri Dec 06, 2024 4:41 pm
Has thanked: 1 time

Digital outputs automated in a MEGA

Post 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.

mnfisher
Valued Contributor
Posts: 1655
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 764 times

Re: Digital outputs automated in a MEGA

Post 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

mnfisher
Valued Contributor
Posts: 1655
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 764 times

Re: Digital outputs automated in a MEGA

Post 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
Attachments
pins.fcfx
(10.49 KiB) Downloaded 16 times

Billduck1302
Posts: 18
Joined: Fri Dec 06, 2024 4:41 pm
Has thanked: 1 time

Re: Digital outputs automated in a MEGA

Post by Billduck1302 »

I can not say that I follow your suggestions. It is a little advanced for me.
But I will attach what I have come up with. One program samples 7 cells.
And places the float voltages into an array. I have not tested it yet. The second
program - sorting - sorts the values in the array.

mnfisher
Valued Contributor
Posts: 1655
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 764 times

Re: Digital outputs automated in a MEGA

Post by mnfisher »

Will take a look.....

Billduck1302
Posts: 18
Joined: Fri Dec 06, 2024 4:41 pm
Has thanked: 1 time

Re: Digital outputs automated in a MEGA

Post by Billduck1302 »

This is a sorting algorithm. It works for 8 elements in the array, but not 8+.
I need 14 elements.
Attachments
Sorting_V2.fcfx
(17.42 KiB) Downloaded 13 times

mnfisher
Valued Contributor
Posts: 1655
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 764 times

Re: Digital outputs automated in a MEGA

Post by mnfisher »

I posted a bubble sort many moons ago (https://www.flowcode.co.uk/mmforums/vie ... ino#p88632) - this sorted 'ints' but would be easy to change to floats
I've also posted a quicksort - though it is probably overkill for 14 numbers (https://www.flowcode.co.uk/mmforums/vie ... rt#p104653)

Your algorithm probably fails because it goes through the loop a set number of times (5 x length + 4) - what it should really do is repeat until there are no swaps made (have a flag (Boolean) 'sorted' - initially true and set it to false if you make a swap and loop until 'sorted'....

In pseudocode...

Code: Select all

loop 
    sorted = true
    for (.i = 0 to  length - 1)
        if swap needed then
            sorted = false
            swap(array[.i], array[.i + 1])
        end if
    end  for
until sorted


The sample data you have - the sort is attempting to reverse the data - which is the worst case for bubble sort. I haven't tested it - but it probably needs more passes (it's not the most efficient sorting algorithm)

Martin

Post Reply