Esteemed Forum Participants and Lurkers:
Warning: I'm NOT a C++ programmer ... I barely get along in C
I'm trying to write a general application for multiple DS18x20 thermometers on multiple pins. I have loaded the OneWire library and demo program from the PJRC website OneWire Library and it runs a single wire just fine.
I then started on my program and first wrote a loop to discover all devices on the specified pins, and that runs fine - it loops finding devices on 2 pins (1 device on pin 9, and 6 devices on pin 10). Note that it iterates through the "OneWire" definition for each pin:
// DS18x20 Library
#include <OneWire.h>
/**************** USER DEFINITIONS ****************/
// Define the wired pins
uint8_t PINS[] = {9, 10}; // I AM USING ARDUINO MEGA 2650
// Define the maximum number of Thermo devices
uint8_t DEVS = 8;
/*************************************************************
**** setup: Initialization section ****
*************************************************************/
void setup(void) {
// Initialize and start the default serial port
Serial.begin(9600); // 9600 baud
}
/*************************************************************
**** FUNCTIONS: ****
*************************************************************/
// Function to convert a byte into a 2 character string.
// pdec2: prefix a leading space for a decimal output
// 0 - 99 only
void pdec2(uint8_t dnum) {
if (dnum < 10) { Serial.print(" "); }
Serial.print(dnum);
}
// phex2: prefix a leading 0 for a hex output
void phex2(uint8_t hnum) {
if (hnum < 16) { Serial.print("0"); }
Serial.print(hnum, HEX);
}
/*************************************************************
**** loop: RUN section VARIABLES ****
*************************************************************/
// loop section variables:
// Device parameter storage: Pin #, Address[8 bytes]
// Since structs are not fully supported, just use arrays
// This setup will support up to 8 devices
uint8_t pin[8]; // Pin # driving each device
uint8_t par[8]; // Whether or not this device is on a Parasitic Power pin
uint8_t addr[8][8]; // Array of address for the devices
byte data[12]; // Raw data from read of Scratch Pad
byte i; // loop iterator - pin index
byte j; // device iterator - index
byte k; // byte iterator - index
int n; // repeat counter
// "Continue" flag
boolean flag;
/*************************************************************
**** loop: RUN section CODE ****
*************************************************************/
void loop(void) {
// Create a list of devices
j = 0; // Initialize the Device counter
// Initialize the device table to "NO DEVICES" by writing "0" into "device type" bytes
for (i = 0; i < DEVS; i++) { addr[i][0] = 0; }
// Iterate through the pins creating a list of devices
for (i = 0; i < sizeof(PINS); i++) {
flag = TRUE; // Initialize the "Continue" flag
// DS18x20 Temperature chip i/o assignment to pin
OneWire ds(PINS[i]);
// find all of the devices on this pin
// If search is successful, the address is loaded into the array
while ( flag ) {
// Print the pin number as a line prefix
Serial.print("Pin "); pdec2(PINS[i]);
// Look for any devices on this wire - NOTE: addr[j] = *addr[j][0]
if ( ds.search(addr[j])) {
// Log the pin number for this device address
pin[j] = PINS[i];
// Display the device ROM information
Serial.print(" ID "); phex2(addr[j][0]);
// Display the 6 Byte address:
for (k = 6; k > 0; k--) { phex2(addr[j][k]); }
// Display the CRC just for luck ???
Serial.print(" CRC "); phex2(addr[j][7]);
Serial.print("\n");
// Increment the device counter
j += 1;
if (j >= DEVS) { flag = FALSE; }
}
else { // no more devices were found ... end of search on this pin
Serial.print(" No more devices on this pin.\n\n");
ds.reset_search();
// Force the next pin to be selected
flag = FALSE;
delay(250); // ????
}
}
}
// READ CODE GOES HERE ...
}
If I insert the following code at the end of the previous code:
// Take 100 Temp Readings from all attached thermometers
for (n = 0; n < 100; n++) {
// Reset the pin selector
i = 0;
// Scan the entire address table for devices to read
for (j = 0; j < DEVS; j++) {
// Check for a change in the driver pin number
if (i != pin[j]) {
// The pin has changed ... set up to use the new pin
i = pin[j];
// DS18x20 Temperature chip i/o assignment to pin
OneWire ds(PINS[i]); // Set the pin at each change
// Start a convert operation on ALL devices on this pin/wire
// Do a reset to clear out all devices
ds.reset();
// Issue SKIP ROM to direct next command to ALL devices on the pin
ds.write(0xCC,0); // SKIP ROM, with parasite power OFF
ds.write(0x44,0); // ALL DEVICES: CONVERT T, with parasite power OFF
delay(775); // Wait at least 750 milliseconds for the conversions to complete
}
// All devices are done ... Read each device
// ds has dropped out of the scope of OneWire here ???
ds.reset();
ds.select(addr[j]);
ds.write(0xBE); // Read Scratchpad
// Read 9 bytes of Scratch Pad Data
for ( k = 0; k < 9; k++) { // we need 9 bytes
data[k] = ds.read();
// This is as far as I got before getting the compile error
}
}
}
I get an error:
MultiTherm.ino: In function ‘void loop()’:
MultiTherm.ino:211:7: error: ‘ds’ was not declared in this scope
I have beaten my head against a brick wall trying to figure out how to properly change the pin assignment WITHOUT having to have ds1, ds2, ds3, etc. The "OneWire ds(PINS*);"[/b] does change the pin and it does work in the first part of the program, but it has this strange "scope" problem in the bottom part.*
I even tried to insert a "ds.apin" function into the library, but I don't really know how to do that. I put it in OneWire.h, OneWire.cpp, and in keywords.txt, but it was never recognized in the program as a function of OneWire. It is frustrating, because all the ds(PINS);[/b] does is to set up the pin mode, mask, and basereg and then do a reset_search. Nothing at all exotic. I just can not figure out how to access or implement the function to allow the pin number to be changed reliably.
HELP!!! Please. Thank you for any and all comments, suggestions, and assistance in this perplexing C++ puzzle.
Blessings in abundance,
Art in Carlisle, PA USA