Input Pin assignment

Hi,

Got a little condition that appears to work ok, but the code does not look right, from my beginers eyes ?

I have two functions, one to read a ntc sensor , one to read a ds18b20 sensor.

On the circuit board there is a jumper to select which device is needed and in the main loop one or the other function must be commented out.

But its the pin assigment that looks wrong, I specify the ntc pin with a Const Int but have to specify the Dallas by means of its #Define in that sequence only , as shown below.

So its seems I am assigning two different functions to the same pin, so seemingly that would confuse things, yet it all seems to work ok when I run the code for either of the sensors ?

Should I be defining the NTC pin assignments and Dallas #Define and parameters within their respective functions rather than at the beginning of the code ?

const int ntcPIN    = A1; 

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS A1

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire)


void loop() {
	
    //ds18b20_temperature_control();   
	
	ntc_temperature_control();

The DS18B20 is usually assigned to a digital pin like:

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

Need to see [u]all[/u] your code.

Programmers get into arguments about whether const int is better than #define. I prefer const int but they are both valid.

const int for a value that is never above 255 is wasting a byte. const byte would be better. I think the argument for #define is that the smaller choice gets made "automatically"?

The OP asked about #define versus const int. I didn't want to confuse the issue with const byte, const char, etc.

As you probably know, const int (and const byte and const char) won't use any Arduino data bytes unless there is a need (such as taking the address of the item).

I can do

const int x 6 ;

and the compiler will check that I have done the right thing.

I can also do

#define x 6

but without the checking, so

#define x six

also is allowed but will lead to some very strange error messages.

I can do

No, you can't. Try that exact code in an otherwise empty sketch.

vaj4088: I can do

const int x 6 ;

I can not, I have to use const int x = 6;;)

Yes, I omitted the equal sign. So sorry! Thanks for the correction.

ricky101:
Hi,

Got a little condition that appears to work ok, but the code does not look right, from my beginers eyes ?

I have two functions, one to read a ntc sensor , one to read a ds18b20 sensor.

On the circuit board there is a jumper to select which device is needed and in the main loop one or the other function must be commented out.

But its the pin assigment that looks wrong, I specify the ntc pin with a Const Int but have to specify the Dallas by means of its #Define in that sequence only , as shown below.

So its seems I am assigning two different functions to the same pin, so seemingly that would confuse things, yet it all seems to work ok when I run the code for either of the sensors ?

Should I be defining the NTC pin assignments and Dallas #Define and parameters within their respective functions rather than at the beginning of the code ?

const int ntcPIN    = A1; 

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS A1

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire)

void loop() {

//ds18b20_temperature_control();

ntc_temperature_control();

Just be consistent. So either use

const int ntcPIN    = A1;
const int ONE_WIRE_BUS = A1;

OR

#define ntcPIN A1
#define ONE_WIRE_BUS A1

And as both refer to the same pin, you can use one definition / declaration like

#define DATA_PIN A1

And use that for both the NTC and the Dallas.

And as both refer to the same pin

I think it should be obvious that they shouldn't.

PaulS: And as both refer to the same pin

I think it should be obvious that they shouldn't.

From the opening post I understand that OP has a 'module' with two functionalities and he/she can select one by using a jumper; my interpretation is that data for both functionalities is on the same pin.

my interpretation is that data for both functionalities is on the same pin.

Should they be?

PaulS: Should they be?

Ask the designer of the board ;)