Modifying Code for Multiple Input Pins

I’ve started a project to monitor multiple temperatures at a remote location. I’m using the DS18B20 and a Diamondback. Even though I can bus multiple DS18B20 using 1 pin, I still would like the option of being able to do that on every pin from 3 to 13. I understand how int pinmode, digitalread and serial work but I can’t seem to change the code in the existing program. I’m trying to display the temperatures as int 3 = blue, 4 = red, 5 = yellow etc etc up to pin 13 and print out the temperature for each colour. I would also like to know how the serial indexing works for multiple DS18B20’s on 1 pin where you can actually name each index as 0 = blue, 1 = red, 2 = yellow etc etc.
I just would like the option to name each pin in the sketch or each index on the pin and see the temperature information for each colour on the serial monitor.

The code that I’m using is just standard =

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

// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2 

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 

// Pass our oneWire reference to Dallas Temperature.  
DallasTemperature sensors(&oneWire); 

void setup(void) 
{   
// start serial port 
Serial.begin(9600); 
Serial.println("Dallas Temperature IC Control Library Demo"); 

// Start up the library 
sensors.begin(); 
} 
 
 
void loop(void) 
{    
// call sensors.requestTemperatures() to issue a global temperature  
// request to all devices on the bus 
Serial.print("Requesting temperatures..."); 
sensors.requestTemperatures(); // Send the command to get temperatures 
Serial.println("DONE"); 

Serial.print("Temperature for Device 1 is: "); 
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire 

}

The onewire.h that I’m using is onewire 2.0 but I had to rename it so include creference it correctly

If you mean you have all the sensors on pin 1 and want to cycle through them, i have a “skeleton” sketch that you could use as an example.
It uses arrays to cycle through all the sensors an display the values to in my case , serial displays.
You can quite easilly modify it to do it what you want, eg. display the temps on ports you define.

If you have questions about it, just ask.

/************************************************************************************************
* Display temperature on serial display
* Requires an Arduino, DS18B20 & Serial LED display
* 
* Running on Arduino Uno & compatible
* 13/8/2011 arduino at esdivad.com
************************************************************************************************/

/***********************************************************************************************
* Resources
* http://www.milesburton.com/Dallas_Temperature_Control_Library
* http://bildr.org/2011/07/ds18b20-arduino/ code did not work!!!
* Serial LED display:
* http://www.sparkfun.com/products/9765
* http://www.sparkfun.com/datasheets/Components/LED/7-Segment/SFE-0012-DS-7segmentSerial-v41.pdf
* http://www.arunet.co.uk/tkboyd/ec/ec1led4x7ser.htm
* C++:
* http://www.cplusplus.com/doc/tutorial/arrays/
************************************************************************************************/

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

// Port serial display is connected to 
int serialToDisplay0  	= 4; // serial LED display
int serialFromDisplay0 	= 5; // not used declared for clarity
int serialToDisplay1  	= 6; // serial LED display
int serialFromDisplay1 	= 7; // not used declared for clarity

// array declared global to modify by function and used in loop()
int temperature[2]; // 0 = round value, 1 = decimal value

// if we need serial debugging, just make this true
#define DEBUG false

// Setup serial LED displays
NewSoftSerial LedDisplay[2] = 	{ NewSoftSerial(serialFromDisplay0, serialToDisplay0),
				  NewSoftSerial(serialFromDisplay1, serialToDisplay1) };
				  
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

/************************************************************************************************
* void setup
************************************************************************************************/

void setup(void)
{
  pinMode(serialToDisplay0, OUTPUT);
  pinMode(serialToDisplay1, OUTPUT);
  
  int i;
  for ( i = 0; i < 2; i++)
  {
    // initialise serial LED displays
    LedDisplay[i].begin(9600);
    LedDisplay[i].print("v"); // reset display 
  }
    
  // Start up the sensors
  sensors.begin();
}

/************************************************************************************************
* void loop
************************************************************************************************/

void loop(void)
{ 

  int i;
  for ( i = 0; i < 2; i++)
  {
    // point position 1 & 5 on
    LedDisplay[i].print("w");
    LedDisplay[i].print(B00100010,BYTE);
    
    
    getTemperature(i);
   
    // print value to the display
    LedDisplay[i].print( temperature[0] );
    LedDisplay[i].print( temperature[1] );    
    
    // print celcius sign
    LedDisplay[i].print("C");
  } 
}

/************************************************************************************************
* getTemperature
************************************************************************************************/

void getTemperature( int sensorIndex )
{
   
  sensors.requestTemperatures(); // Send the command to get temperatures
  
  // get temperature from sensors and store the value in an array
  temperature[0] = sensors.getTempCByIndex(sensorIndex);
  temperature[1] = ( sensors.getTempCByIndex(sensorIndex) - temperature[0] ) * 10;
}
[/code}

For multple one wire busses you just need an array of OneWire and an array of DallasTemperature objects and set them up in a for loop I think...

Yes I've seen the array & for loop command and I have a general idea of how it works so I will try it and let you know!

If you mean you have all the sensors on pin 1 and want to cycle through them, i have a "skeleton" sketch that you could use as an example. It uses arrays to cycle through all the sensors an display the values to in my case , serial displays. You can quite easilly modify it to do it what you want, eg. display the temps on ports you define.

If you have questions about it, just ask.

I can see that your program outputs the bus on pin 2 to 2 serial output displays. I can modify any output but what I'm trying to do is beable to array the output pins from 3 to 13 and also have multiple DS18B20's on each of those pins and index each one with a name for reference on the serial monitor. I can see how your program indexes the serial bus and it is useful but I need to set up each pin first then work from there.

I know what I want to do with it and I have an idea how to do it but I don't know how the program is structured so that I can modify it.

Do you 2 guys need help with adding multiple pins because I've nearly got it....! I don't mind helping you either!

:astonished:

I am also trying to connect 4 DS18B20 to 4 different pins. This way it will be easy to replace any potentially broken thermometer and the code will be universal: anyone can connect their thermometers to the given pins and the code will read the addresses of devices and use them correctly (pin1 temperature inside, pin2 temp. outside, pin3 temp. garage, pin4 temp. attic).

Can you help me with programming 4 DS18B20 thermometers, each connected to a different pin?

seanlangford: Do you 2 guys need help with adding multiple pins because I've nearly got it....! I don't mind helping you either!

:astonished:

That must be the most difficult Arduino question... Nobody answered your question for 5 years...

I've got the same problem and I wonder whether I will ever solve it :-)