Getting a return value from Arduino function

Goal is a 3D printer Heater. Parts: Mega, 2.8 " TFT, 12V 4 wire fan, SSR relay, 120VAC PTC heaters, DS18B20 temperature sensors.
The problem is getting a returned temperature value tempC from function printTemperature() . tempC is then set to interiorTemp. Both tempC and interiorTemp = 0, and not the correct temperture. How to correctly code this ? InteriorTemp will be used to compare to the set temperture, and then turn on / off the heater. The rest of the code works.

// 2.8 " TFT Display adafruit, Arduino Mega, DS18B20+, 12V fan(4 wire), 
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h> 
#include <Adafruit_GFX.h>                      
#include <Adafruit_ILI9341.h>
#include <FanController.h>

//***** DS18B20 Section (Temperature Sensor)**
#define ONE_WIRE_BUS 22 // pin 22 for DS18B20 
// Setup a oneWire instance to communicate with Maxim/Dallas temperature ICs
OneWire oneWire(ONE_WIRE_BUS); // pin 22, ONE_WIRE_BUS is the object name
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire); // sensors  is the object name
// arrays to hold device addresses
byte InteriorSensor0 [8]= { 0x28, 0x95, 0xEE, 0x2A, 0x0D, 0x00, 0x00, 0x47 };
byte CaseSensor1 [8]= { 0x28, 0xB7, 0x25, 0x2A, 0x0D, 0x00, 0x00, 0xCD };
byte OutputSensor2 [8]= { 0x28, 0x61, 0x8F, 0x2B, 0x0D, 0x00, 0x00, 0xBA };

//***** Set Temperature Section **
int potPinSetTemp = A9; //Analog pin 9,potentiometer pin number for "Set Temp"
int potValSetTemp; // for "Set Temperature", potentiometer value
int SetTempValCelsius; //"Set Temperature" value in Celsius
int SetTempValFah; //"Set Temperature" value in Fahrenheit

//***** Heater Fan Section (Fan PWM, Fan Speed, Fan Frequency,Fan Speed Potentiomter )***************************
// please refer to:
#define SENSOR_PIN 18 //for fan tach. works at pin 2 & 3 & 18
// Choose a threshold in milliseconds between readings.
// A smaller value will give more updated results,
// while a higher value will give more accurate and smooth readings 
#define SENSOR_THRESHOLD 1000   //For FanController.h
// Initialize library
int potValFan;// for fan speed, potentiometer value
int potPinFan = 8; // Analog pin 8, potentiometer pin number for fan speed
int fanVal; //fanVal = duty cycle for fan

const byte OC1A_PIN = 11;//fan pwm pin number, probably not changeable                            
const byte OC1B_PIN = 12;                         
const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency 
const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ); //variable: Timer/Counter register for timer 1 = 16000000 divided by (2 * 20000)

void setPwmDuty(byte duty) {             
OCR1A = (word)(duty * TCNT1_TOP) / 100;  

//***** TFT Section ***************************
// 2.8 " TFT adafruit using SPI wiring
// TFT
// These are 'flexible' lines that can be changed for tft screen
#define TFT_CS 53 //tft screen pin CS,to arduino pin 10
#define TFT_DC 6  //tft screen D/C, to arduino pin 9
#define TFT_RST 8 //not used, RST can be set to -1 if you tie it to Arduino's reset
//Arduino Mega SPI digital I/O pins: MISO-pin 50, MOSI-pin 51, SCK (CLK)- Pin 52
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

//***** Fan and Heater Relay turn On Section ***************************
const int FanTurnOnPin = 24; //  turn on 12V postive for fan 
const int HeaterTurnOnPin = 25; // turn on 12V postive for heater relay 
float tempC;     
float inteiorTemp;

//***** Error Section ***************************
const int ErrorPin = 26; // turns on postive 12V for red LED, //Uses 2 transistors to output 12V positive.
void setup() {
  sensors.begin();// start DallasTemperature sensors
  sensors.setResolution(9); // lower resolution is that the temperature-to-digital conversion takes a lot less time
  fan.begin();  // start FanController.h
  pinMode(OC1A_PIN, OUTPUT); // pin 11, fan pwm  
  // turns on Fan 12V positive, and PTC relay 12V positive.
  //Uses 2 transistors to output 12V positive.
  pinMode(24, OUTPUT); // turns on postive for fan
  pinMode(25, OUTPUT); // turns on postive for heater relay
  pinMode (ErrorPin, OUTPUT); // turn on red LED for Error
  // Clear Timer1 control and count registers 
  TCCR1A = 0;  // timer/counter register 1a
  TCCR1B = 0;  // timer/counter register 1b
  TCNT1  = 0;  //timer/counter register. The actual timer value is stored here  
  TCCR1A |= (1 << COM1A1) | (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << CS10);

  tft.setRotation (1), tft.fillScreen(ILI9341_BLACK), tft.setTextSize(2);  
  tft.setCursor(0, 0), tft.setTextColor(ILI9341_WHITE);tft.print("HEATER IS --------");
  tft.setCursor(0,20),tft.setTextColor(ILI9341_BLUE);tft.print("SET TEMP. -------- ");
  tft.setCursor(0,40),tft.setTextColor(ILI9341_WHITE);tft.print("INTERIOR TEMP. --- ");
  tft.setCursor(0,60),tft.setTextColor(ILI9341_BLUE);tft.print("CASE TEMP. ------- ");
  tft.setCursor(0,80),tft.setTextColor(ILI9341_WHITE);tft.print("OUTPUT TEMP. ----- ");
  tft.setCursor(0,100),tft.setTextColor(ILI9341_BLUE);tft.print("FAN SPEED (RPM)--- ");
  tft.setCursor(0,120),tft.setTextColor(ILI9341_WHITE);tft.print("FAN DUTY CYCLE % - ");
  tft.setCursor(0,140),tft.setTextColor(ILI9341_RED);tft.print("----- ERROR MESSAGE ----- ");  

void loop() {
  sensors.requestTemperatures(); // must be in loop to update temp
  digitalWrite(FanTurnOnPin, HIGH); // turn on 12V postive for fan 
  digitalWrite(HeaterTurnOnPin, HIGH); // turn on 12V postive for heater relay.
  //digitalWrite (ErrorPin, HIGH);
  tft.fillRect(220,0,125,134, ILI9341_BLACK);// write over (black out)old values
  tft.fillRect(0,157,320,80, ILI9341_BLACK);// write over (black out)old "error messages"
  tempC = inteiorTemp;
  // test to see if inteiorTemp is valid   
  potValFan = analogRead(potPinFan);//read potentiomter center tap, for fan
  potValSetTemp = analogRead(potPinSetTemp);//read potentiomter center tap, for "Set Temp"
  fanVal = map(potValFan, 0, 1023, 0, 100);//set fan duty cycle value 0 to 100
  SetTempValCelsius = map(potValSetTemp, 0, 1023, 12, 80);//"Set Temp" (degrees) 12(53 Fah) to 80(176 Fah)
  SetTempValFah = (SetTempValCelsius * 1.8) + 32;
  setPwmDuty(fanVal);// set fan duty cycle    
  // Call fan.getSpeed() to get fan RPM.
  unsigned int rpms = fan.getSpeed(); // Send the command to get RPM  
  if (digitalRead(25) == HIGH){
    tft.setCursor(240,0),tft.setTextColor(ILI9341_GREEN),tft.print ("ON");}
    tft.setCursor(240,0),tft.setTextColor(ILI9341_RED),tft.print ("OFF");}
float printTemperature(DeviceAddress deviceAddress) {
  float tempC = sensors.getTempC(deviceAddress);
  float tempF = sensors.getTempF(deviceAddress);
  tft.print(tempC,0), tft.print("C,"), tft.print(tempF,0), tft.print("F");
  return  tempC;

You have a function, printTemperature() which returns the value, but your code never calls it. Inside loop(), you need to do something like

inteiorTemp = printTemperature();

so it hold the current value and then compare it to your setpoint and turn the heater on/off

Also, what's going on here:

  tft.setCursor(220, 40), tft.setTextColor(ILI9341_WHITE), printTemperature(InteriorSensor0);

Why commas instead of semicolons to separate statements?

The function gets called in the line that wildbill posted/wrote about.

Thank you, will use semicolons. My experience level is very low.

It gets called sure, but you don't use the return value as @blh64 showed you how to do.

Recommend a method to get a return value, I am still learning. Thanks

Split this

  tft.setCursor(220, 40), tft.setTextColor(ILI9341_WHITE), printTemperature(InteriorSensor0);

into three lines so it is readable.

tft.setCursor(220, 40);

Next apply to the last line what @blh64 showed in post #2.

tft.setCursor(220, 40);
inteiorTemp = printTemperature(InteriorSensor0);

Repeat for other temperature readings that you're interested in.

In a good design you separate (in my opinion) the UI from the IO and the logic. So write one or more dedicated functions for reading sensors and one or more for displaying the information.

It does, but you throw the return value away. You have to assign it like I said

Thank you everyone for your time. I think I got it. To return a value it's: Variable = Function. Made a function to print to the screen, no commas.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.