jraskell:
You should make them unsigned, not signed. millis() and micros() will never return a negative value. Look at the reference of a function to see it's return type. 9 times out of 10, that's the data type you want to store the value into.
millis() and micros() return an unsigned long, so you should store the value in an unsigned long. Otherwise you will eventually run into problems.
Got it, I think! So, can I do the following; use a short for the interval, as the interval is constant at 500?
unsigned long previousMillis = 0;
const unsigned short interval = 500; // creates 500ms interval in which to update serial
So as for defining pins as ints, can I not define pins as chars, as they are between 0 and 255 - to save space? For example;
const short tempPin = 1; // defines the LM35 input into analogue pin 1
short valFan = 0; // creates an integer used for temperature mapping, this being mapped fan speed - default to 0
const short fanPin = 9; // defines the fan output connected to digital pin 5
Or is this not seen as best practice, if not - why?
EDIT: Changed my entire code to the following, and tested - works exactly the same, but the program size is exactly the same replacing the ints with shorts. I thought a short was 1 byte, and int was 2 bytes?
/*
Uses an LM35 to sense temperature, and drive a cooling fan accordingly.
by Julian West http://uk.linkedin.com/pub/julian-west/26/a42/a02
*/
const short tempPin = 1; // defines the LM35 input into analogue pin 1
short valFan = 0; // creates an integer used for temperature mapping, this being mapped fan speed - default to 0
const short fanPin = 9; // defines the fan output connected to digital pin 5
const short onboardLED = 13; // defines onboard LED as pin 13
unsigned long previousMillis = 0;
const unsigned short interval = 500; // creates 500ms interval in which to update serial
void setup()
{
analogReference(INTERNAL); // sets ADC ref voltage to internal 1.1v to increase resolution
pinMode(fanPin, OUTPUT); // defines fanPin as an output
pinMode(onboardLED, OUTPUT); // defines onboardLED as an output
digitalWrite(onboardLED, LOW); // turns off onboard pin 13 LED
Serial.begin(9600); // initialises serial comms at 9600 baud rate
digitalWrite(fanPin, HIGH); // pulses fan output to max
delay(250); // waits 250ms
digitalWrite(fanPin, LOW); // then switches output to fan off before commencing loop, helps to kickstart sticky fans into motion at low PWM duty cycles
}
void loop()
{
valFan = map(analogRead(tempPin), 0, 500, 0, 255); // used to map valFan between 0 and 255 for PWM, between 2 and 55 degrees C (LM35 output = 0v @ 2deg C, + 10mV/degC)
valFan = constrain(valFan, 0, 255); // sets limits of valFan
analogWrite(fanPin, valFan); // write PWM to fanPin
unsigned long currentMillis = millis(); // count milliseconds and assign to currentMillis
if(currentMillis - previousMillis > interval) // decides if current millis is greater than interval
{
previousMillis = currentMillis;
short temp = (1.1 * analogRead(tempPin) * 100 / 1024); // converts analog reading into degrees C
Serial.print(temp); // prints to serial monitor
Serial.print(" deg C");
Serial.println("");
}
}