Pages: [1]   Go Down
Author Topic: Help to move 2 arrays into FLASH-Mem with PROGMEM  (Read 962 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I modified a sketch for triggering a camera.
This sketch contains 2 arrays that I need to move into FLASH mwmory in order for the menu on the LCD to operate correctly.
The problem is that when flipping thorugh the pages more than 10 times, ARDUINO resets itself
The code and the project description is here:
http://www.halo-photographs.com/remote-shutter-release-timer/the-arduino-code-test.html

I hope someone can help me on the syntax to move the 2 arrays into FLASH memory. I've gone through the PROGMEM Reference but was not able to achieve a useable result.
Thanks for your cooperation, suggestions and feedback.

Logged

Dig-IT-all

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be helpful if you posted what you tried.

Did you do something like this...

Code:
unsigned long exptime[] PROGMEM = {602, 603, 604, 606, 608, 611, 617, 622, 633, 650, 667, 700, 728, 767, 850, 933, 1100, 1267, 1600, 2100, 2600, 3600, 4600, 6600, 8600, 10600, 15600, 20600, 30600, 40600, 60600, 90600, 120600, 180600, 240600, 360600, 480600, 600600, 900600, 1200600, 1800600, 2400600, 3600600, 4800600, 7200600, 14401200};

unsigned long GetExpTime( uint8_t index )
{
  return( pgm_read_dword( &exptime[index] ) );
}


- Brian
Logged

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for reply.
Here is what I did so far.

There are 2 arrays to be put into Flash-mem. For now I have only been working on the one for 'char* expstr[]'.
The effect is that since the array is in Flash-mem the code no longer crashes. (so the initial problem solved).

But I am having trouble to display the content of the variable 'expstr' for value '' (b = base exposure).
The code is here: http://www.halo-photographs.com/remote-shutter-release-timer/the-arduino-code-Flashmem1.html

The array 'char* expstr[]' starts at line 78 till 162.
The variable is defined on line 187.
The content of this variable is used for display at line 287 for the first time like this:

Code:
strcpy_P(expstr, (char*)pgm_read_word(&(expstr_table[b])))
The problem is when value > 10 then it displays instead of the corresponding string in the array, the array srting that has a value +10.

This can be easily confirmed by entering the value for manually, see line 288. eg. for value '11' the array spits out the string for value '21'.

So the array is placed into Flash-mem, it returns strings as anticipated, but if the arrays register value is > 10 it adds another 10.

I have no idea what might cause this behaviour.
Seems to be an issue of proper syntax - don't konw.

Suggestions are most welcome.
Logged

Dig-IT-all

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Recap:

The problem with 'expstr' (described in the previous post) has been resolved. Some lines were missing in the table for this array.
Code:
PROGMEM const char *expstr_table[] = {expstr_00, expstr_01, ..., ..., expstr_46};

Meanwhile I have moved the other array for 'exptime' into FLASH-mem with the following command:
Code:
PROGMEM prog_uint32_t exptime[] = {601, 602, 603, 604, 606, 608, ..., ..., 14400600};
This seems to work allright.

But for some reason I cannot access the values this array contains.
I was playing with some cocmmands like below, but did not succeed.
Using this command for example:

Code:
lcd.cLCD_String(strcpy_P(exptime, pgm_read_word_near(exptime[b])), MN_X+75, MN_Y+80, RED, WHITE);

produces the following error:
Code:
error: cannot convert 'prog_uint32_t*' to 'char*' for argument '1' to 'char*' strcpy_P(char*, const prog_char*)'
Hhm, I'd think I am having a syntax problem.

Unfortunately the sample code for reading and writing unsigned chars (bytes) and ints (2 bytes) to PROGMEM provided at the PROGMEM reference page does not work.
http://www.arduino.cc/en/Reference/PROGMEM

It would be a great help for me to be able to retrieve the unsigned long values from the array 'exptime'.

... or to find some sample code to see how unsigned long values can be properly retrieved and used for further processing.

Thanks for some feedback on this topic.


Logged

Dig-IT-all

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This works for me...

http://arduino.pastebin.com/m1b20b5a7


Good luck,
Brian
Logged

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great, thanks a lot BRIAN.
(your coding doesn't seem to be so 'badly' - in contrary).

The adjusted code is updated (at the url provided in previous post).

There is one last issue I'd like to get solved:
The array 'exptime[]' is now in FLASH-mem. So the command on line 569 must be changed as well as 't' referred to the current exposure time when this array was in the default memory space.

Code:
void fire(unsigned long t){
What is the proper syntax for this function when it's values are stored in PROGMEM?

I am almost certain there is an easy answer, but for me it's not so easy. Also I must confess this is my first big project I've done in C++, so please forgive my mostly 'basic' questions.

Thanks a ton, Hans.
Logged

Dig-IT-all

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe I should add a bit more information.

This is the part of the code I need to adapt.
Only the lines marked with an asteriks * are of importance here.
What is the first line so that it reads the value for 't' from array 'exptime[]'?

Code:
void fire(unsigned long t){            // * this is the sequence to release the shutter

pinMode(FOCUS, OUTPUT);                  // outpin 6 gives output
pinMode(SHUTTERPIN, OUTPUT);            // outpin 4 gives output
digitalWrite(FOCUS, HIGH);            // initiate focus and camera ready ('r6' for the Nikon D300)
delay(INITfocus);                  // give time till camera 'beeps' the ready signal
digitalWrite(SHUTTERPIN, HIGH);            // initiate exposure, activate mirror-up / on Nikon D300 set d9=on, results in a 600ms delay till the actual exposure starts
delay(INITfire);                  // there must be a short delay so the circuit has just enough time to trigger the shutter release
digitalWrite(SHUTTERPIN, LOW);            // mirror is up and shutter has been released, the Shutterpin has no more effect can be set to LOW
delay(pgm_read_dword( &exptime[t]));      // * this is the actual exposure. Should be something like the following line
// delay(t);                         // * 't' should be known form the first line
digitalWrite(FOCUS, LOW);            // end the exposure delayed by 't'

}
« Last Edit: May 11, 2009, 06:28:48 pm by snahl » Logged

Dig-IT-all

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Issue resolved.

Below the code for those interested:

old code before using PROGMEM:
Code:
void fire(unsigned long t){                // open shutter for 't' milliseconds

new code using PROGMEM:
Code:
void fire(prog_uint32_t t){                // open shutter for 't' milliseconds

Well, basically quite simple, for those who learned this stuff properly.

Thread is closed.
Logged

Dig-IT-all

Pages: [1]   Go Up
Jump to: