Go Down

Topic: progmem help (Read 762 times) previous topic - next topic

Pavilion1984

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


Many thanks.

PaulS

Does that snippet even compile?

Pavilion1984

I don't get any errors apart from the line that says
Code: [Select]
sprintf_P(str1, hello[2]);
my full code below.
Code: [Select]
#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.

AWOL

#3
Nov 16, 2012, 02:31 pm Last Edit: Nov 16, 2012, 02:57 pm by AWOL Reason: 1
Code: [Select]

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.
"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.

Pavilion1984

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: [Select]
prog_char hello[] PROGMEM =
{
 "HELLO1",
 "HELLO2",
 "HELLO3"
};


so how would i read the second string?

Pavilion1984

I have worked it out now with the code below.
Code: [Select]
#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);
}

PaulS

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".

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy