compiling example "Dallas Temperature/Multiple" leads to error .

Hey Community

I tried to use the given example code of Dallas Temperature / Multiple for an own project.
I got the error: Exitstatus 1; variable or field ‘printAddress’ declared void. The line:

void printAddress(DeviceAddress deviceAddress) {

Is marked.
So I tried to debug it and run into something I can´t deal with.

When I copy the code in one Tab and Verify it, everything is fine. If I put the (unmodified!) code into two tabs, it depends where I put the cut. Sometimes I get the error. Espacially If I don´t expect it.

This one leads to an error.
Tab A_Dallas:

/*
   Rui Santos
   Complete Project Details https://randomnerdtutorials.com
*/

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

// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// 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);

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address



void setup(void)

Tab: A_Setup

{
// start serial port
Serial.begin(9600);

// Start up the library
sensors.begin();

// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();



// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");

// Loop through each device, print out address
for (int i = 0; i < numberOfDevices; i++) {
  // Search the wire for address
  if (sensors.getAddress(tempDeviceAddress, i)) {
    Serial.print("Found device ");
    Serial.print(i, DEC);
    Serial.print(" with address: ");
    printAddress(tempDeviceAddress);
    Serial.println();
  } else {
    Serial.print("Found ghost device at ");
    Serial.print(i, DEC);
    Serial.print(" but could not detect address. Check power and cabling");
  }
}
}


void loop(void) {
  sensors.requestTemperatures(); // Send the command to get temperatures

  // Loop through each device, print out temperature data
  for (int i = 0; i < numberOfDevices; i++) {
    // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i)) {

      // Output the device ID
      Serial.print("Temperature for device: ");
      Serial.println(i, DEC);

      // Print the data
      float tempC = sensors.getTempC(tempDeviceAddress);
      Serial.print("Temp C: ");
      Serial.print(tempC);
      Serial.print(" Temp F: ");
      Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
    }
  }
  delay(5000);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

And the same error occours when the last line

void Setup (void)

is moved to the second tab which is the intention of tabbing the sourcecode.
But If I pull only the first line in fact one char, the “{” from the second tab to the end of the first tab, everything is fine, and there is no error.

This is not what I expect, and I don´t understand the pattern behind it. Of Course my project is somehow different but it condenses to that occurance.

I use the 1.8.13 IDE on windows 10.

Someone can give me a hint? I could not find one by myself.

thanks
Kurt

komischerschwabe:
When I copy the code in one Tab and Verify it, everything is fine.

So everything is fine :smiley: that's good.

The first tab on the left has the name of your project. You should put global variables and the setup() and the loop() function in it.
You can put other functions in other tabs.

The compiler gets confused if you put a part of a function in one tab and the rest of that function in another tab. You can not blame the computer for that.

There is more to it. Arduino has a preprocessor which makes function prototypes for you. The *.ino files are combined into one *.cpp file and the *.cpp file is given to the compiler. That means you can do all kind of silly things trying to fool the preprocessor, but please don't do that.

It looks like splitting a function declaration between the name and the ‘{’ was causing the Arduino processor to get confused about where to put the prototypes (forward declarations) it generated. It put them at the top of the file, which was before #include <DallasTemperature.h> so the “DeviceAddress” type used in the declaration of printAddress() was not defined.

success, where “setup() {” was in the main sketch:

#include <Arduino.h>
#line 1 "/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino"
/*
   Rui Santos
   Complete Project Details https://randomnerdtutorials.com
*/

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

// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4

// 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);

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

#line 22 "/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino"
void setup(void);
#line 4 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
void loop(void);
#line 31 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
void printAddress(DeviceAddress deviceAddress);
#line 22 "/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino"
void setup(void)
{

fail, where “setup()” was in the main sketch and “{” was in the tab.

#include <Arduino.h>
#line 22 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
void setup(void);
#line 40 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
void loop(void);
#line 67 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
void printAddress(DeviceAddress deviceAddress);
#line 0 "/Users/john/Documents/Arduino/sketch_feb28a/zzzfoo.ino"
#line 1 "/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino"
/*
   Rui Santos
   Complete Project Details https://randomnerdtutorials.com
*/

#include <OneWire.h>

#include <DallasTemperature.h>
.
.
.

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