Integer Array?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Integer Array?

Post by Mark »

Hello,

I would like to implement an integer array using Flowcode. Initially I provided a byte array by defining a string variable Data[250] and this worked fine. To simply accommodate large numbers I can obviously use two byte arrays in parallel, one for the high byte, one for the low byte. however, the reason I wish to use an integer is to accommodate negative numbers.

Unless there is a simple solution to the above then let me illustrate the problem I'm trying to solve, as more experienced programmers than I may have an alternative suggestion as to how to solve the problem.

I have a line follower robot, each iteration of the 'sense line-move' iterative line following process has the outcome stored in an array.
I then carry out trend analysis on the array to move the program into a predictive level of operation.
The above works reasonably well using a byte array but any level of analysis became unduly complicated.

I therefore had in mind storing a value of -X for move to the left, 0 for straight ahead and X for move to the right. Simply adding up the preceding set of N values from the array would immediately tell me if there was a net change in direction and conveniently screen out the randomness associated with line detection. The problem being that the byte based method simply looked for blocks of the same movement, which was easily upset by the odd 'random' deviation from course.

A first thought was to use two's complement numbers in the byte array. But I don't understand the underlying maths enough to know if using (1 for 1, 128 for -1 and 0 for 0 would allow me to add up all the bytes into an integer variable and still get a nett result, presumably not. Or should I convert each signed byte to an integer and then do the maths? If so is there a neat way of doing this?

Thanks for any input in advance. Whilst the question is mostly related to Formula Flowcode the underlying issue is a programming one, hence I have put it in this part of the forum.
Go with the Flow.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Integer Array?

Post by Benj »

Hello,

You can create an array of integers but the maximum array size on a pic is 256 bytes so your maximum array size for integers would be 128.

You could instead use the method to convert values to integer before doing the final calculation.

if (byte > 127)

Yes:
int = byte
int = !int
int = int + 1

No:
Int = byte

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Re: Integer Array?

Post by Mark »

Ben,

Thanks, helpful.

When you say an integer array can be created did you mean:
In Flowcode - at least not directly (or in dsPIC flowcode?)?
In C - If so a reference, so I can look up the syntax please?
An effective array from using a byte array in sets of two bytes?

Thanks again.
Go with the Flow.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Integer Array?

Post by JonnyW »

Hi Mark. You should be able to create an array of integers in any Flowcode version by selecting the integer radio icon and typing in the size of the array you want:
vars.png
Variable example
(19.62 KiB) Downloaded 5359 times
You will not be able to change existing variables that are arrays from byte to int, but should be able to create one from scratch.

Note that where as a two byte high/low array is unsigned, an int array is signed.

Cheers,

Jonny

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Integer Array?

Post by JonnyW »

Hi Mark. Sorry, Ive just re-read your post - still bleary eyed this morning.

Another possibility if you are struggling for size (using an int array) and your 'X' value is always the same is to store a value of 0-2 in a byte array then do:
X = (array[index] - 1) * speed

Where 'array' is a byte array and 'X' and 'speed' are integers. This gives a value of:
0 = Move -X
1 = Stay still
2 = Move +X

If you want more granularity just increase the max size of a value in your array, so allowing values 0-4 you would do:
X = ((array[index] - 2) * speed) / 2

Where 'array' is a byte array and 'X' and 'speed' are integers. This gives a value of:
0 = Move -X
1 = Move -X/2
2 = Stay still
3 = Move +X/2
4 = Move +X

I hope this makes sense and isnt too garbled.

Jonny

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Re: Integer Array?

Post by Mark »

Jonny,

Many thanks. Very helpful. I never knew that you could define arrays that way. I had been defining the arrays by creating a string and populating it with numercical values.

If I may, it would be great to have a programming manual for flowcode. The 'help' is dare I say helpful but it is just not the same as being able to sit down and read a systematic reference. Personally I would be prepared to pay for a proper manual as a seperate product, I have manuals enough on most things.

I will use your idea of an array of offset values, adjusted back when retrieved.

Best regards,

Mark
Go with the Flow.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Integer Array?

Post by JonnyW »

No problems. As far as a technical manual goes I don't think we have any plans for such at the moment but it would certainly be nice to have - I'm developing Flowcode v5 at the moment and it would certainly be helpful for me!

Good luck with your program,

Jonny

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Integer Array?

Post by Benj »

I pitched a technical manual on C, Flowcode and microcontrollers in general to Elektor and they were not overly interested which put me off a bit. I am still writing in my free time but it is not currently at the top of the list. Does anyone know of a publisher who may potentially be interested?

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: Integer Array?

Post by Jay Dee »

Only just found this thread but just thought i'd jot a comment regarding a tech manual or indeed any other further flowcode documentation which is always desirable. The forum is really great for both help and reference but i often find myself trawling many different old threads just to find the answers to my general queries and problem solving, ( i.e. how to create an array )this trawling works but its not the same as a manual.
I bought Bert van Dams book which is a great 'Projects Book' and gives me a starting point for similar projects but again its not a manual and parts are now a little outdated with current flowcode. (i'd still recommend it though :-) )

Regarding a publisher, I would also be happy to pay for a well written manual and whilst we all still love paper, I've recently gone to using a largish Ebook to store all of my PDF datasheets and use this as a reference along side my laptop/second screen setup. Its not the same as paper but it works and I can carry all my data sheets around the world.
Others may also be happy with electronic (pdf/epub) copies, so I guess an author could simply release ebook chapters, people could buy the chapters as required or the whole lot and as flowcode grows so would the chapters etc thus keeping the reference up to date. A big job though and having been involved in writing a few end user manuals and technical guides, this is all very easy to say and much harder to collect info, consolidate and produce the actual product.
J.

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Integer Array?

Post by Steve »

There has been another book published recently based on Flowcode that you might be interested in:
https://www.google.com/search?q=%22john ... rojects%22

Bert's book is a great resource because of its quantity and breadth of examples and I'd recommend it to anyone who is moderately familiar with Flowcode and wants help and ideas for specific projects.

Although John's book is entitled "PIC Projects for Non-programmers", I'd say it serves as more of a tutorial/reference book. It does contain projects, but they are generally pitched at a beginner level and are often explained in great detail. Plus there are reference sections for aspects of Flowcode. It's definitely aimed more at the novice user.

I believe these two books do not overlap too much and compliment each other quite well.

Post Reply