Use and effectiveness of the F macro

Mod edit,
Below split from If statment confusion - #8 by jremington. Read that to make sense of the first comment.
End of mod edit.

No it doesn't LOL
I Absolutely understand what you were going for, everyone does this as a beginner.

The thing is some don't care about Memory usage and some do.
Now in my opinion you should always care, but if your'e using Arduino you should definitely care but if using ESP32 as it has much more it's easy not to care

Your idea doesn't work because of the this..

int joy_x=A0, joy_y=A1, joy_k=8, joy_center=504;
int bt_a=2,bt_b=3,bt_c=4,bt_d=5,bt_e=6,bt_f=7;


void setup() 
{
  Serial.begin(115200);
  Serial.println(F("Startup")); // it gets stored in Flash Memory here instead of SRAM
}


void loop() 
{
  Serial.print("joy_x=");                   // but then here you stuff it up
  Serial.println(analogRead(joy_x)); ;      // but then here you stuff it up
  if (analogRead(joy_x) != 504)
    {
      Serial.println("offset"); ;           // but then here you stuff it up
      Serial.println(analogRead(joy_x)); ;  // but then here you stuff it up
    }
  if (analogRead(joy_x)==504)
    {
      Serial.println("Joy_Center"); ;       // but then here you stuff it up
    }
  if (analogRead(joy_x)<504)
    {
      Serial.println("Joy Left"); ;             // but then here you stuff it up
    }
  delay(500);
  Serial.println("_________"); ;                  // but then here you stuff it up
}

I'll show you a better way if you like saving memory
because what you're doing here is Taking it out of 1 memory and putting into another.
it ALL gets compiled at run time and chews up your memory in any case.

There is a better way.
it goes like this...
the use of Serial.print() and Serial.println will always cause memory allocation to occur for the text string, this is the Catch-22
and ALL THE STRINGS get loaded into memory during compile.. ok

Now, WHAT IF ...

  1. We didn't load them into memory in the first place

  2. We only used them when we needed them.

But How...?

What you do is.. You use an object that is not Serial or print or println
but then you need to (when required) to change that object to
.. Serial.print() or Serial.println

Here is some test code that i use

#define AltText 1

#if AltText == 1
#define SameLine(x) Serial.print(x)
#define NewLine(x) Serial.println(x)

#else
#define SameLine(x)
#define NewLine(x)
#endif
void setup()
{
  Serial.begin(115200);
  delay(1000);
  SameLine("  1 + 1   = ");
  NewLine("2");
  SameLine("  2 + 2   = ");
  NewLine("4");
  SameLine("  4 + 4   = ");
  NewLine("8");
  SameLine("  8 + 8   = ");
  NewLine("16");
  SameLine(" 16 + 16  = ");
  NewLine("32");
  SameLine(" 32 + 32  = ");
  NewLine("64");
  SameLine(" 64 + 64  = ");
  NewLine("128");
  SameLine("128 + 128 = ");
  NewLine("256");
  SameLine("256 + 256 = ");
  NewLine("512");
  SameLine("512 + 512 = ");
  NewLine("1024");
}
void loop()
{

}

the output is just something i thought up on the spur of the moment , Change it if you want to.

the point is...
i define "AltText" as 1 You can change the name to anything you want.
and 1 just means TRUE
This also gives you the capability of Turning it all off if you wish so you don't have to keep
// doing this
// and this
// and this
which is bloody boring and time consuming
You just need to change the 1 to a 0

so it's saying... If AltText is equal to 1 or True then do this

#define SameLine(x) Serial.print(x)   // This changes SameLine to print
#define NewLine(x) Serial.println(x)  // This changes NewLine to println

it's literally a swap of one thing for another, but the first thing doesn't chew up memory and it only swaps it out WHEN IT'S NEEDED
the (x) is a place holder so whatever you have in the parenthesis will get directly swapped out

then this part

#else
#define SameLine(x)
#define NewLine(x)

is basically saying, when you've selected 0 , Swap them for.... NOTHING.

This sketch was originally designed for debugging where you can turn the debug feature on or off
You can certainly use it to reduce memory you just don't turn it off.
In your case you wouldn't turn it off

so in your code it would look like this...

// This Section Reduces the memory drain of Text Strings
#define AltText 1

#if AltText == 1
#define SameLine(x) Serial.print(x)
#define NewLine(x) Serial.println(x)

#else
#define SameLine(x)
#define NewLine(x)
#endif
// This is the end of the section

int joy_x=A0, joy_y=A1, joy_k=8, joy_center=504;
int bt_a=2,bt_b=3,bt_c=4,bt_d=5,bt_e=6,bt_f=7;


void setup() 
{
  Serial.begin(115200);
  NewLine("Startup");
}

void loop() 
{
  SameLine("joy_x=");
  NewLine(analogRead(joy_x));
  if (analogRead(joy_x) != 504)
    {
      NewLine("offset");
      NewLine(analogRead(joy_x));
    }
  if (analogRead(joy_x)==504)
    {
      NewLine("Joy_Center");
    }
  if (analogRead(joy_x)<504)
    {
      NewLine("Joy Left");
    }
  delay(500);
  NewLine("_________");
}

See how that works, try that out and have a look at how much memory you just saved.
You're Welcome

I'm going to test this with over 100 lines instead of 3 lines when i get a bit more time< just a bit busy now, thanks though for the info.

The F macro isn't applicable to ESP32.

On an ESP32: Is using the F()Macro as pointless as using PROGMEM? - ESP32 Forum

1 Like

Somebody else brought it up (inexplicably).

3 Likes

And if it doesn't fit, then it has to be made to fit.

It works well in ESP32 Dev Module.

  Serial.begin(115200);
  Serial.println(F("F(ESP32)"));

Output:

13:44:39.012 -> F(ESP32)

It seems to 'work', it doesn't kill anything, but not 'well'. It doesn't accomplish the F_macro goal.
Anyway, reading the article linked, it actually burns more memory - in an ESP32.
Someone brought it up and maybe I should have ignored that.

Let's stick with the OP's topic: "If statement confusion".

1 Like

Q: How does Serial.print("Forum"); show Forum on OutputBox of the Serial Monitor?
A: It is my understanding that the given Serial.print("Forum"); command is broken into the following frames which are written one-after-another onto the Transmit Buffer and then onto the Transmit Register of the UART Port by interrupt process. It is an immediate addressing mode; so, there is no need to store the string "Forum" into memory during compile time.

==> Serial.write('F');   // Serial.write(0x46);
==> Serial.write('o');
==> Serial.write('r');
==> Serial.write('u');
==> Serial.write('m');

Would be glad to hear others' opinions.

The F macro saves on dynamic memory in an AVR-based application.

1 Like

No it's all good, thank you mate
I've been speaking to @Delta_G who has cleared up a few things for me
and i'm going to be doing a few experiments when i get the chance, so as to get comfortable with all this.
Appreciate all this
I believe i do remember you saying before that the F macro is useful on Arduino boards and what not but i think you said... it doesn't apply to ESP32 or something along those lines.
In any case, i'm gonna make some time and test all this out for my own knowledge and progression
thanks again

Mod edit,
Some bits removed.

That's exactly right, I did - and it doesn't.

1 Like

And... Duly Noted LOL

Now i have to ask, to be honest it's been bugging me a little
why Runaway Pancake ?? I need to know LOL

It's all I could think of at the time.

1 Like

Fair enough,
I'm a big Beatles fan and i was racking my brain, and i'm like.. Nup, there is no song
that comes remotely close so i was stumped

Spilt from unrelated topic. Some bits removed.

Feel free to carry on discussing the F macro politely.

Attacks on other members and other nonsense will result in posting bans for a while. You all know how this works.

I thank you all.

2 Likes

Sorry, not sorry, we Germans don't sugarcoat.

is not as bad as

but still wrong.

Your understanding of memory usage is flawed.

I don't have the patience or will to change that,

1 Like

Understood
I probably won't as it wasn't that important to me
I'll bow out.

ok, and as a result , i thinks it's best i don't respond to you in the future
Be cool

EDIT : I've left the discussion

There is one Master and many diciples, and the tradition still exists!! If experience is the treasure of many years of exploration, then it should be readily available for distibution -- at least in the open Arduino Forum?

In AVR, there is "Static RAM (SRAM)" -- why are you saying "dynamic memory"?

1 Like