Pages: [1]   Go Down
Author Topic: progmem help  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, hope you can help. What is the correct way to read "HELLO2" from the code below?
Code:
prog_char hello[] PROGMEM =
{
  "HELLO1"
  "HELLO2"
  "HELLO3"
};

Many thanks.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does that snippet even compile?
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't get any errors apart from the line that says
Code:
sprintf_P(str1, hello[2]);
my full code below.
Code:
#include <avr/pgmspace.h>

prog_char hello[] PROGMEM =
{
  "HELLO1"
  "HELLO2"
  "HELLO3"
};
void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  char str1;
 
  sprintf_P(str1, hello[2]);
 
  Serial.print(str1);
 
  delay(5000);
}

Am wanting to have an array of strings and read them when i want. Just trying to learn about progmem.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
prog_char hello[] PROGMEM =
{
  "HELLO1"
  "HELLO2"
  "HELLO3"
};

This is what "hello" contains: "HELLO1HELLO2HELLO3" (a single C string)
If that's what you want, then it is easy to locate "HELLO2" - it starts at offset 6, and is 6 characters long.
It might be easier for you to store them as separate strings.
« Last Edit: November 16, 2012, 08:57:51 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have just noticed AWOL that my strings are not separate lol and thats what i want so i gues my code needs to be something like below
Code:
prog_char hello[] PROGMEM =
{
  "HELLO1",
  "HELLO2",
  "HELLO3"
};

so how would i read the second string?
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have worked it out now with the code below.
Code:
#include <avr/pgmspace.h>

prog_char hello[3][7] PROGMEM =
{
  "HELLO1",
  "HELLO2",
  "HELLO3"
};

void setup()
{
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }
}

void loop()
{
  char str1[16];
 
  sprintf_P(str1, hello[2]);
 
  Serial.print(str1);
 
  delay(5000);
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have worked it out now with the code below.
If that code is printing "HELLO2", something is wrong. That should be printing "HELLO3".
Logged

Pages: [1]   Go Up
Jump to: