Hi guys,
Had an Arduino (Uno) for a while now, but never used these forums. I've got a simple sketch reading the output of an LM35 temp sensor to define the PWM duty cycle of an output to control a fan. I'm also outputting the temperature via serial to the serial monitor. I've got a delay(200) at the end of the loop so that the temp is only sent once every 1/4s, however - I've just noticed, this is holding up the entire loop for that time too.
Now - I'm well aware temp sensors take a while to respond, and so waiting such a time isn't actually a problem here at all, the system responds perfectly well... but, for future reference - how do I send the temp via serial at set intervals, yet not hold up the rest of the loop from, well - looping again!
Furthermore, as you can see in the code - I've got the temp, and "deg C" on two different prints, followed by a blank println - how can I get these two little snippets to appear on the same line in the serial monitor, with one line of code. I thought maybe Serial.println(temp, "deg C"); may work, but not so.
Many thanks in advance!
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
int fanPin = 9; // defines the fan output connected to digital pin 5
int onboardLED = 13;
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
int 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("");
delay(200); // updates serial monitor every 200ms (+ miniscule execution time of the rest of the loop)
}