NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Arrays for calculus and manipulation

May 11, 2010
by Frozenlock
Frozenlock's Avatar

There is abundant information about string arrays on this forum, but so far I didn't find anything about (what I consider) ordinary arrays. (int, short, long...)

My problem is quite simple; I want to fill an array with data acquired by the MCU. Unfortunately, I'm unable to even start my program, my array never works.

As a first step I want to initialize an array of 10, then insert the number "5" in one of the coordinates.

This is a part of my code:

int sample_results_matrix[10];
sample_results_matrix[3] = 5;

My compiler says " data definition has no type or storage class ". I've tinkered around a bit, but I've yet to succeed.

I would greatly appreciate any advice!

Frozenlock

May 11, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Frozenlock,

I see no reason why what your are doing should not work. To be more precise it is a good idea to declare the exact width you need for the integers, so I would change it to uint8_t or something that fits the data you will be putting in the int.

Are you sure those two lines are the problem, and not something else in the program. Also make sure you are including inttypes.h

Humberto

May 11, 2010
by Frozenlock
Frozenlock's Avatar

Thanks Humberto, With your answer I determined my syntax was correct and examined other variables in my problem. I think I've found the cause.

I have the habit of placing most of my global variables outside the main, near the function where they're needed. It usually works well, but apparently the arrays can't be initialize outside the main. Could someone confirm?

At least now it works!

May 12, 2010
by bretm
bretm's Avatar

See this thread on a related issue. It explains why your arrays don't get initialized and how to fix it.

May 17, 2010
by Frozenlock
Frozenlock's Avatar

Thanks bretm, I have now added .data in all of my makefiles!

I'm still having some trouble with some arrays. It acts as if I was modifying the data inside when I only want to retrieve and use it.

For instance, I have an array:

long int Array[20]={0};

In the program, the zeros are replaced by some values. Then I retrieve the values and use them in another equation:

A=(Array[i]*thick*2*y);

(With thick a float and everything else an integer.) When I look back in my array, the values have changed!

I suspect an overflow.... but this is getting a little too advanced for me. I don't know which part might be causing this: The simple integer 'A' trying to handle the long integer Array? The float multiplied by a long integer creating some kind of long float, which in turn is supposed to fit in the simple integer 'A'?

By the way, is there a way to monitor the RAM usage?

May 18, 2010
by bretm
bretm's Avatar

First, I would ask how you know that the array values have changed. How are you verifying this? Do you have a small code sample that reproduces the problem? The code you showed obviously should not modify the array values.

The expression (Array[i]thick2*y), because it contains a mix of integer a floating point terms, will be evaluated as a floating point expression and produce a floating point value. When you assign the result to an integer variable 'A' the value will be truncated to an integer value, e.g. 3.1 will become 3. If the value overflows the range of int, which is -32678 to 32767, I think it will just take the least-significant 16 bits of the result and put those in the variable, but the result probably wouldn't be useful in that case.

May 20, 2010
by Frozenlock
Frozenlock's Avatar

Thanks bretm, your questions helped me quite a bit!

It turns out the faulty element wasn't the array, but rather the fact that I tried to print a long integer out on my LCD, with a command valid only for normal integer.

It went like this... "First, I would ask how you know that the array values have changed. How are you verifying this? " -Well it's easy, I print them on my LCD..... D'oh!

Post a Reply

Please log in to post a reply.

Did you know that one NerdKits customer discovered that his hamster ran several miles in a night using his kit and a Hall-effect sensor? Learn more...