Software > Software

C language programming problem, AVR

(1/3) > >>

Admin:
Ok this is really confusing so Im 100% sure Im doing something dumb . . .

I call Move() in my main code, this is Move():

--- Code: ---void Move(int direction)
{
int time_position=0;//goes up to 8

direction=1;

//if(direction==1)
//{
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_2_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_4_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_5_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Bulk_Left[8]={33,33,33,33,33,33,33,33};
//}

//sort through kinematics for one cycle
while(time_position<8)
{
RIB_1_Fin_Left(Rib_1_Left[time_position]);
RIB_2_Fin_Left(Rib_2_Left[time_position]);
RIB_4_Fin_Left(Rib_4_Left[time_position]);
RIB_5_Fin_Left(Rib_5_Left[time_position]);
BULK_Fin_Left(Bulk_Left[time_position]);
time_position++;
}
}

--- End code ---

the above code properly compiles with no problem . . . however, when I remove the commenting out for the IF statement, the code no longer works:


--- Code: ---void Move(int direction)
{
int time_position=0;//goes up to 8

direction=1;

if(direction==1)
{
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_2_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_4_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_5_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Bulk_Left[8]={33,33,33,33,33,33,33,33};
}

//sort through kinematics for one cycle
while(time_position<8)
{
RIB_1_Fin_Left(Rib_1_Left[time_position]);
RIB_2_Fin_Left(Rib_2_Left[time_position]);
RIB_4_Fin_Left(Rib_4_Left[time_position]);
RIB_5_Fin_Left(Rib_5_Left[time_position]);
BULK_Fin_Left(Bulk_Left[time_position]);
time_position++;
}
}

--- End code ---

when compiling that code, i get these errors that make no sense to me:
(see attached image)

what dumb mistake am i making?!?

JonHylands:
You're declaring your variable inside an if clause, which is a no-no if you reference it outside the if clause.

You need to declare all the variables at the beginning of the function (or context), and then assign them inside the if clause.

- Jon

Admin:
wow it only took you 3 minutes to fix that . . .

ok so my problem runs a little deeper now . . .

so lets say i initialize an array like this:
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};

but then later i want the array to have some other random set of numbers stored:
unsigned int Rib_1_Left[8]={133,23,33,43,23,83,93,133};

without reinitiating it, how would i write that above line?

if I just do:
Rib_1_Left[8]={133,23,33,43,23,83,93,133};
it gives me an error that says "expected expression before '{' token"

i could of course do one liners like this that will work:
Rib_1_Left[0]=133;
Rib_1_Left[1]=23;
Rib_1_Left[2]=33;
but since i must have 16 arrays each of size 8 this would get extremely messy very fast

my google search failed to find answers . . .

JonHylands:
Okay, so its complicated...

The {1,2,3} syntax can only be used during the initialization of the variable, on the declaration line.

What you really need to do is to have an array in memory with each setup, and copy the contents of the template arrays into your working arrays. Of course, given the numbers you're giving me, you're probably going to run out of RAM really quick, unless you're using one of the huge AVR chips.

If that is the case (low RAM), you can store the arrays in FLASH, and then copy them into to the RAM array from there.

Read the following two pages:

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_rom_array

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#g53ee9e2dec1d5f685d78aa8dc444dccb

Hopefully that will point the way...

All this is courtesy of my brother Dave, BTW.

- Jon

JonHylands:
My brother says to try something like this:

const unsigned int array1[ 8 ] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 8 };
unsigned int array2[8];
memcpy_P( array2, array1, sizeof( array2 ));

You may also want to check the size of your values - he says 'int' is 16 bits typically on AVRs...

- Jon

Navigation

[0] Message Index

[#] Next page

Go to full version