Dipswitch configuration

I have a strange question maybe…

Is there a way to define a const variable by reading 4 ports (from a dip switch) on a Mega (outside of setup() and loop())

What I would like to do is make the size of an array variable depending on the state of 4 dip switches…

No. A constant is constant at compile time.

I don't see any value in setting the size of the array at run time. If you want to be able to set it to the largest size then there must be sufficient unused memory, and if there is sufficient there is no advantage in a smaller sized array.

I suggest you create the array big enough for the largest usage and use the dip switches to set a variable that defines the max number of elements to be used.

...R

Thanks for the quick answer!

I kind of hoped I could change the value of a constant at boot, not just at compile time...

The reason why is simple... I use the same home brew library to control my I/Os on several Arduino's. Now I have to compile to each arduino depending my specific needs.

I use about 95% of program storage space, and 70% of dynamic memory.

Input configurations and output configurations are stored in two big global structs. Of roughly the same size. (in is two bytes bigger per instance).

I can't set them to max because then I run out of memory and the program becomes unstable. (I have noticed this starts to happen if I exceed 70% of dynamic memory)

Sometimes I need more Inputs, sometimes more Outputs and sometimes more PWM outputs... If I there were a way to change the size of the array of structs at boot, I could have loaded my script on a few mega's and just set them to the same config (with the dipswitches) and 'hot' swap them...

But thanks anyway!

Actually, you can do the equivalent to what you want using malloc() and free(). BUT all use of that memory is then up to you. Probably using pointers.

Paul

In most programs coded on embedded processors, 95% is critically high use of RAM. If you (or the Arduino core) call many or nested functions simultaneously - that remaining 5% will collapse with the stack & heap writing over your variables - or each other!

The actual RAM usage is variable at runtime.

lastchancename: In most programs coded on embedded processors, 95% is critically high use of RAM. If you (Arduino core) call many or nested functions simultaneously - that remaining 5% will collapse with the stack & heap writing over your variables - or each other!

The actual RAM usage is variable at runtime.

Is there a way to check this at runtime I don't think there is a debugger for Arduino is there?

Paul_KD7HB: Actually, you can do the equivalent to what you want using malloc() and free(). BUT all use of that memory is then up to you. Probably using pointers.

Paul

For some reason I am always afraid to try this out... Maybe I should just experiment a bit with it. Thanks to this Forum I do use memset quite often now..

Can you point me to (understandable and well explained) source that explains the usage of malloc and free? what I find in google is pretty confusing at best...

Sorry am a violinist not a hard core programmer...

Kevin77: For some reason I am always afraid to try this out... Maybe I should just experiment a bit with it. Thanks to this Forum I do use memset quite often now..

Can you point me to (understandable and well explained) source that explains the usage of malloc and free? what I find in google is pretty confusing at best...

Sorry am a violinist not a hard core programmer...

Well, you can't be an innocent and be a hard core programmer at the same time. Spend the same amount of time for each.

Paul

"you can't be an innocent and be a hard core programmer at the same time" The new motto for the forum!

In most programs coded on embedded processors, 95% is critically high use of RAM.

True, but the OP said "I use about 95% of program storage space". He said his ram usage is about 70% which isn't too bad as long as he doesn't call any functions that use a lot of local variable (stack) space.

Pete

Kevin77:
The reason why is simple…
I use the same home brew library to control my I/Os on several Arduino’s.
Now I have to compile to each arduino depending my specific needs.

Thinking some more about this …

If you are using different Arduino boards (with different amounts of SRAM) then you have to select the appropriate board before compiling the program. You could include macros in your code to select different sizes of array according to whichever board is being used.

The Arduino source code uses that style very widely.

…R

Paul_KD7HB: Well, you can't be an innocent and be a hard core programmer at the same time. Spend the same amount of time for each.

Paul

HAHAHAHA I love that...

Robin2: Thinking some more about this ...

If you are using different Arduino boards (with different amounts of SRAM) then you have to select the appropriate board before compiling the program. You could include macros in your code to select different sizes of array according to whichever board is being used.

The Arduino source code uses that style very widely.

...R

Thanks for the advice, I already do that for some specific code that uses Serial1.

I will check out the malloc and free options and spent some more time reading for the sole aim of becoming less and less innocent.

Thanks for the feedback, guys!