Variable not declared in this scope - still confused

I’m new to this- I’ve read up on declaring variables, but I’m using some code from some examples and I’m getting an error. I’m not sure how to correctly declare a particular variable to not get the “not declared in this scope” error.

I’m working with the DS18B20 temperature sensor. I’m including the OneWire.h and DallasTemperature.h libraries.

Here’s a code snippet:

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

void setup() {
sensors.begin(); //start the library and sensors
}

void loop() {
sensors.requestTemperature(); //send the command to get temps from all sensors
.
.
.
}

And that’s where it chokes giving me the “‘sensors’ was not declared in this scope” error.

I get that "sensors.begin() and sensors.requestTemperature() are not declared in the same scope- but if I move the ‘sensors.begin()’ to void loop() I get the exact same error.

So I’m not sure where to declare ‘sensors’ or what to declare it as? Floating?

Any help or direction appreciated.

Also- using an ATMega 2560 if that matters, but I don’t think it does.

Post your code.
In code tags

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


const int voltagePin = A0; //power input sensor - resistor network with sense input on A0
// const int tempsensePin = A3; //connect analog TMP36 temp probe here
const int tempsensePin = 4; //connect the DB18B20 here
const int doorStatePin = 2; // connect the door relay here
const int motionStatePin = 3; // connect the motion sensor here
const float templow = 65.0; //store low temperature here
const float temphigh = 75.0; //store high temp here
int doorState = 0; //door closed
int powerState = 1; //power is on
int motionState = 0; //nobody is home
int templowState = 0; // set the temp in range
int temphighState = 0; // set the temp in range


void setup() {
  Serial.begin(9600); // open a serial port
  pinMode(2, INPUT); // door input
  pinMode(3, INPUT); // motion sensor input
  pinMode(4, INPUT); // temp sensor input
  sensors.begin(); //start the library and sensors
}

void loop() {

  // let's find out if the door is open or closed
  doorState = digitalRead(doorStatePin);
  if (doorState == 0) {
    Serial.println("Door is closed.");
  }
  else {
    Serial.println("Door is open.");
  }  // end of door routine

  // Let's find out if anyone is in the freezer
  motionState = digitalRead(motionStatePin);
  if (motionState == 0) {
    Serial.println("No one in freezer.");
  }
  else {
    Serial.println("Someone in freezer!");
  }  // end of motion routine

  // Now let's find out if the power is on - should be 12VDC nominal
  int powersenseVal = analogRead(voltagePin); // read the power/voltage input on A0 - 0-1023
  Serial.print("Power input is: "); Serial.println(powersenseVal);

  // convert the ADC reading to voltage
  float powerinput = (powersenseVal * 5000.0 / 1024.0);
  Serial.print("Power input voltage: "); Serial.print(powerinput); Serial.println("mVDC");

  // now let's decide the power input state
  if (powerinput <= 4) {
    Serial.println("No power to freezer motor!");
    powerState = 0;
  }
  else if (powerinput > 4) {
    Serial.println("Power OK");
    powerState = 1;
  }  // end of power routine

  // let's read the temperature sensor, calculate temp, and decide if it's in range
 /*  Comment out the analog stuff for now 
  *   
  *   
  int tempsenseVal = analogRead(tempsensePin); //read the temp sensor- reading 0-1023
  Serial.print("Temp Sensor Value: "); Serial.println(tempsenseVal);

  //convert the ADC reading to voltage
  float voltage = (tempsenseVal * 5000.0 / 1024.0);
  Serial.print("Temp Sensor Voltage Out: "); Serial.print(voltage); Serial.println("mV");

  //let's start working on the temperature
  float temperatureC = ((voltage - 500) / 10); //500 offset to handle below 0 temps
  */
  
// Let's get the digital temperature
sensors.requestTemperatures(); //send the command to get temps from all sensors  
float tempF = sensors.getTempFByIndex(0); //Index 0 gets temp from 1st sensor on bus
// float tempF = ((tempC * 9 / 5) + 32); //calculate F from C

  // Serial.println(temperatureC);
//  Serial.print("Temp-C: "); Serial.println(tempC);
  Serial.print("Temp-F: "); Serial.println(tempF);

  if (tempF <= templow) {
    Serial.println("Temp Low Alarm");
    templowState = 1;
    temphighState = 0;
  }
  else if (tempF >= temphigh) {
    Serial.println("Temp High Alarm");
    templowState = 0;
    temphighState = 1;
  }
  else {
    Serial.println("Temp in Range");
    templowState = 0;
    temphighState = 0;
  }  // end of temperature routine

  // based on all the collected data, let's output the system state:
  Serial.println("System State:");
  Serial.print("Temp Low State: "); Serial.println(templowState);
  Serial.print("Temp High State: "); Serial.println(temphighState);
  Serial.print("Door State: "); Serial.println(doorState);
  Serial.print("Motion State: "); Serial.println(motionState);
  Serial.print("Power State: "); Serial.println(powerState);

  // and now let's enter our state table to send data to the R-Pi
  

  delay(5000);  // delay so we don't get too many readings
  Serial.println();

}

Your code is missing some object declarations. See the examples that come with the DallasTemperature library.

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 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);

Perfect. Got it working. Thanks for the help!