Compile error '<variable>' does not name a type

I am using an Arduino Mega 2560 and have a breadboard with a 4-digit, 7 segment display. Code is developed in the 1.6.11 IDE.

I am attempting to create a simple program:

read the thermistor (KY-013)
convert to deg. F,
compare to predefined value
turns on a relay (LED - pin 13 in this sketch)

I am using the sevseg library for the display

I had it all working fine, and then tried to add an audible alert (KY-006) when the LED turned on or off and that messed up the 7-segment display (I got carried away and didn't save my functioning code before adding the buzzer - my bad... lesson learned).

I have since tried to eliminate everything that is not involved in just reading the analog pin and displaying it on the 7 segment and I keep getting the error:

'varTemp1' was not declared in this scope

If I comment out line 126 and change 114 to int varTemp=1234 , it compiles fine and displays 1234 on the display. I'm pretty sure defining varTemp1 as an integer at the top of the loop() is the wrong thing to do. I've tried moving the definition to the setup() section - but that doesn't help with the error on line 126.

All my searching points to issues with libraries - but... I don't think this is a library issue since I am including the sevseg library and it displays accurately.

I guess I could start redeveloping again and this time being more careful to archive the code whenever I get to a functioning point - but I've asked questions here before and the gang is really helpful.

Disclaimer - I am NOT a programmer by training or experience. I'm working with the Arduino to teach myself C++ coding. I have worked in many different worlds of code & scripting - enough to understand, probably never good enough to be considered 'good'. I'm not trying to substitute asking a question for research - I'm just out of ideas of what to look at and where to search.

Here is my code...

</>
/*

  • Development of a utility to read an analog pin (temperature)
  • compare to a preset static number (temperature) and turn
  • a relay on or off depending on comparison to the static value.
  • As part of the development process, I included a 4 digit, 7 segment
  • LED display to display my calculated temperature and visually compare
  • with an independent thermometer used as a standard.
  • The utility utilizes code provided for the KY-013 thermister module
  • (37 sensor selection) but mathematically modified to present the
  • displayed temperature to closer match my external, independent thermometer.
  • Most of the code has been commented out to try and get it down to the
  • smallest bit of working code
    */

#include <SevSeg.h>

SevSeg sevseg; //Instantiate a seven segment controller object

//int tempSensor = A0; //select analog input pin
//int buzzOP = 12; //select buzzer output pin
//int relayState = 13; //select digital driver pin for relay
//int varRelayState = 0; //variable to store current relay state

//temperature capture variables

//int varTemp2=0;
//float varTemp1f = 0.0;
//float varTemp2f = 0.0;

//control variables
//float varTempSetPoint = 74.0;
//int varCycleCount = 0;

//this code is taken from the website
//TkkrLab
//and modified the base resistance value from 10K to 10K150 to display the temperature
//to match my external thermometer

float Thermistor(int RawADC) {
float Temp;
Temp = log(10150.0*((1024.0/RawADC-1))); //modified from '...log(10000...
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}

void setup(){

//Serial.begin(9600); //setup the serial port

// assign digital output pins to segments
int segA = 2;
int segB = 0;
int segC = 3;
int segD = 5;
int segE = 6;
int segF = 4;
int segG = 1;
int segDP = 7;
int Dig1 = 8;
int Dig2 = 9;
int Dig3 = 10;
int Dig4 = 11;

int buzzOP = 12; //select buzzer output pin
int relayState = 13; //select digital driver pin for relay
int tempSensor = A0; //select analog input pin

// set all output pins (segments/buzzer/relay)
pinMode(segA,OUTPUT);
pinMode(segB,OUTPUT);
pinMode(segC,OUTPUT);
pinMode(segD,OUTPUT);
pinMode(segE,OUTPUT);
pinMode(segF,OUTPUT);
pinMode(segG,OUTPUT);
pinMode(segDP,OUTPUT);
pinMode(Dig1,OUTPUT);
pinMode(Dig2,OUTPUT);
pinMode(Dig3,OUTPUT);
pinMode(Dig4,OUTPUT);
pinMode(buzzOP,OUTPUT);
pinMode(relayState,OUTPUT);

pinMode(relayState,OUTPUT); //set the relay control pin as output
pinMode(buzzOP,OUTPUT); //set buzzer ping to output (analog)

// for the sevseg function - need to assign varibles needed in function
byte numDigits = 4;
byte digitPins[] = {Dig1, Dig2, Dig3, Dig4}; //Digits: 1,2,3,4 <--put one resistor (ex: 220 Ohms, or 330 Ohms, etc, on each digit pin)
byte segmentPins[] = {segA, segB, segC, segD, segE, segF, segG, segDP}; //Segments: A,B,C,D,E,F,G,Period

sevseg.begin(N_TRANSISTORS, numDigits, digitPins, segmentPins);
sevseg.setBrightness(25); //Note: 100 brightness simply corresponds to a delay of 2000us after lighting each segment. A brightness of 0
//is a delay of 1us; it doesn't really affect brightness as much as it affects update rate (frequency).
//Therefore, for a 4-digit 7-segment + pd, COMMON_ANODE display, the max update rate for a "brightness" of 100 is 1/(2000us8) = 62.5Hz.
//I am choosing a "brightness" of 10 because it increases the max update rate to approx. 1/(200us
8) = 625Hz.
//This is preferable, as it decreases aliasing when recording the display with a video camera....I think.

/*
varTemp1 = analogRead(tempSensor); //collect raw results from ADC
varTemp1f = Thermistor(varTemp1); //convert to °C
digitalWrite(relayState,HIGH);
delay(500);
digitalWrite(relayState,LOW);
/
/

analogWrite(buzzOP,255);
delay(500);
analogWrite(buzzOP,0);
*/
}

void loop() {
int varTemp1;
static byte decPlace = 0;
//Serial.print("varTemp1 ");
//Serial.println(varTemp1);
//Serial.print("varTemp1f ");
//Serial.println(varTemp1f);
//for (int j=1; j<1000; j++) {
sevseg.setNumber(varTemp1 ,decPlace);
//sevseg.setNumber(float (varTemp1f ),decPlace);
sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
}

varTemp1 = analogRead(tempSensor); //capture raw data read from ADC
//varTemp1f = Thermistor(varTemp1); //convert to temp using function (above)

//}
/*
if (varTemp1f > varTempSetPoint) //comparison for on/off trigger
{
varRelayState = 1; //if we are in here - time to turn on the relay
digitalWrite(relayState,HIGH); //set the pin on
varTemp2f = varTemp1f; //capture current temp for now
varCycleCount++; //increment cycle counter
// Serial.println(varCycleCount); //output the variable just for observation
}
else if (varCycleCount > 5) //make sure we have left the fan on for 5 cycles
{
varRelayState = 0; //turn off the fan relay
digitalWrite(relayState,LOW); //set the pin off
varCycleCount = 0; //zero the cycle counter
}
else varCycleCount++; //just increment the counter
}

*/
</>

Where does loop() end? Where are you then attempting to use varTemp1? ALL executable code MUST be in a function.

OK - THAT was a big miss on my part... I 'closed the loop' before line 126. Changed that and moved to the next errors that stated:

'' was not declared in this scope

I don't want to expose too much of my ignorance and this is not the venue for 'teaching' someone to code. But I have a basic question about the 'sections' of code.

I understand that if I declare a variable 'at the top', it is a global variable. But what is the 'setup()' section for? I thought that was where I should declare variables that will be used in the 'loop()' section. I'm pretty sure I'm not understanding that correctly so... can someone perhaps provide either a quick explanation or a link to one?

And as far as this original question is concerned - I believe the answer has been provided and I have gotten through this problem - so thanks so much 'PaulS'