Go Down

Topic: Serial print refresh time (Read 2 times) previous topic - next topic

majenko

Also, in reply to how to join your strings together into one print - you can use sprintf to build up a string before printing to serial:

Code: [Select]

char buffer[80];

// ...

sprintf(buffer,"The temperature is %f degrees C", temperature);

Serial.println(buffer);


where %f is a placeholder for a "float" or "double" variable - there are others for other variable types - plenty of references online.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

jtw11


Quote
So why, 'const int tempPin = 1;' instead of the old 'int tempPin = 1;'?


The "const" tells the compiler the value will never change.  Therefore, instead of allocating a variable and taking up valuable RAM space, and slowing the system down reading and writing from RAM, it just uses the number "1".

It's functionally the same as

Code: [Select]

#define tempPin 1


but has the advantage that it is typecast to an int.


Aha, that's the explanation I was looking for! (thanks to everybody else too!) On that subject, are there any other ways to reduce the program size, as this sketch actually forms part of a complete automated engine dyno control program.

majenko

Quote
are there any other ways to reduce the program size


Yes.  Write it in assembly instead of C++.

But seriously, some tips:

1. Avoid float / double - the floating point library adds loads of bulk.
2. Don't use variables bigger than you need.  The processor is 8-bit.  Anything bigger than a BYTE or CHAR uses extra instructions to emulate a larger value.
3. Think about the flow of your program.  Write it tight, and the resultant program will be tight.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

jtw11

Having taken snippets and applied them for use in my code from the 'blink without delay' example, could somebody please explain to me the use of 'unsigned long' in place of 'int'? It seems 'int' takes up just a few bytes more, but I'm not sure what 'unsigned long' really is, or indeed why the example uses it. English please?  :D

Quote
const int tempPin = 1; // defines the LM35 input into analogue pin 1
int valFan = 0; // creates an integer used for temperature mapping, this being mapped fan speed - default to 0
const int fanPin = 9; // defines the fan output connected to digital pin 5
const int onboardLED = 13;
long previousMillis = 0;
const unsigned long interval = 500;

majenko

The "const unsigned long" is just seen as (and takes up the room of) 500.

However, when it's being used in comparisons, it's being combined with / compared against other values that are unsigned long.  This just helps the compiler to know what it's doing so it doesn't get confused between different length variables.

For example, if you subtract an int from a long, should the result be an int or a long?  If you're putting it in a variable, then it's whatever the destination variable is cast as.  If you're just using it as a comparison, what then?  By telling the compiler it's an unsigned long, it's telling it that the value is of the same type as that returned by the millis() function.

Unsigned long is twice as big (RAM wise) as an int.  As it's a "const" though, it doesn't use RAM.

Byte, char - 8 bits.
Int, short - 16 bits.
Long - 32 bits.
Long long - 64 bits.

(the "unsigned" doesn't affect the size, just the range of the content)

Oh, and
Code: [Select]

long previousMillis = 0;


should really be
Code: [Select]

unsigned long previousMillis = 0;


to keep the signs the same.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up