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 ...
-
We didn't load them into memory in the first place
-
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