'printTime' was not declared in this scope

I hope someone can help me. I'm relatively new to C++. I am building a hydroponic tower system. Due to my lack of coding knowledge, I have found it easiest to break my operations into separate sketches and them put them together. Having said that I have two sketches attached. The "Timer_CountDown sketch will compile. However, when I put that code into the " Hydroponic_Tower_Assembled" sketch it gets caught up on "'printTime' was not declared in this scope" Can anyone explain why this is?

#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//...................Timer Variables
unsigned long currentTime;
unsigned long previousTime = 0;
unsigned long ON_DURATION = 8000; // ON_DURATION: 8 seconds
unsigned long OFF_DURATION = 20000;//7200000; // OFF_DURATION: 2 hours
unsigned long interval = OFF_DURATION;
bool isRelayOn = false;

void setup() {
  Serial.begin(9600);
  const int Watering = 12;
  pinMode(Watering, OUTPUT);
}

void loop() {
  //............................Timer calculation
  currentTime = millis();
  if (currentTime - previousTime >= interval) {
    if (isRelayOn) {
      interval = OFF_DURATION;
      isRelayOn = false;
      digitalWrite(12, HIGH);

    } else {
      interval = ON_DURATION;
      isRelayOn = true;
      digitalWrite(12, LOW);
    }
    previousTime = currentTime;
  }

  if (!isRelayOn) {
    unsigned long timeRemaining = OFF_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  } else {
    unsigned long timeRemaining = ON_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  }
  delay(1000);
}
//.....................Timer Print Readout
void printTime(unsigned long timeRemaining) {
  unsigned long hours = timeRemaining / 3600000;
  unsigned long mins = (timeRemaining % 3600000) / 60000;
  unsigned long secs = ((timeRemaining % 3600000) % 60000) / 1000;

  Serial.print("Watering In: ");
  Serial.print(hours);
  Serial.print("h ");
  Serial.print(mins);
  Serial.print("m ");
  Serial.print(secs);
  Serial.println("s    ");

lcd.init();
lcd.backlight();
//lcd.begin(20, 4);
lcd.setCursor(0, 2);
  lcd.print("Watering In: ");
  lcd.setCursor(0, 3);
  lcd.print(hours);
  lcd.print("h ");
  lcd.print(mins);
  lcd.print("m ");
  lcd.print(secs);
  lcd.println("s ");
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Arduino.h>
//...................Timer Variables
unsigned long currentTime;
unsigned long previousTime = 0;
unsigned long ON_DURATION = 10000; // ON_DURATION: 10 seconds
unsigned long OFF_DURATION = 40000; //2700000;//45min//7200000; // OFF_DURATION: 2 hours
unsigned long interval = OFF_DURATION;


//............... Sonar definitions
const int trigPin = 6;   // Trigger pin of HC-SR04 connected to Arduino digital pin 2
const int echoPin = 7;   // Echo pin of HC-SR04 connected to Arduino digital pin 3
#define Tankfill 8 //Tank fill relay
//..................Watering Relay


// ......Sonar Variables for duration and distance calculation
long duration;
int distance;
bool isRelayOn = false;
const int Watering = 12;
void setup() {
  Serial.begin(9600);   // Initialize serial communication
//....................Sonar pin Mode............
  pinMode(trigPin, OUTPUT);  // Set the trigger pin as output
  pinMode(echoPin, INPUT);   // Set the echo pin as input
  //..................Tank Fill Relay pin Mode............
  pinMode(Tankfill, OUTPUT);
  pinMode(Tankfill, LOW);
  //.......................Watering Relay......................
  isRelayOn = true;
  
  pinMode(Watering, OUTPUT);
  pinMode(Watering, LOW);
  //........................................................
lcd.init();
lcd.backlight();
lcd.begin(20, 4);
lcd.setCursor(3, 0);
lcd.print("System Starting");
lcd.setCursor(3,2);
lcd.print("THE GERMINATOR");
delay(3000);
lcd.clear();


}

void loop() {
  
  // Clear the trigger pin and wait for a short delay
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigger pin high for 10 microseconds to start the measurement
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse on the echo pin
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  distance = duration * 0.034 / 2;

   //Print the distance to the serial monitor
  Serial.println("Distance  ");
  Serial.print(distance);
  Serial.print("cm  ");

  delay(1000);  // Wait for 1 second before taking the next measurement

  //...................Turning Pumpfill Relay on or off

  if (distance <= 34)
 {
analogWrite(Tankfill, LOW);

lcd.setCursor(0, 0);
lcd.print(distance);
//lcd.setCursor(0,4);
lcd.print("cm");
lcd.setCursor(0, 1);
lcd.print("Tank is Full");
}
else if (distance >= 37)
{
analogWrite(Tankfill, HIGH);
lcd.print(distance);
lcd.setCursor(0,1);
lcd.print(" cm");
lcd.setCursor(0, 1);
lcd.print("Tank Filling to ");
delay(1000);
}
  //............................Timer calculation
 currentTime = millis();
  
  if (currentTime - previousTime >= interval) {
    if (isRelayOn) {
      interval = OFF_DURATION;
      isRelayOn = false;
      digitalWrite(12, HIGH);

    } else {
      interval = ON_DURATION;
      isRelayOn = true;
      digitalWrite(12, LOW);
    }
    previousTime = currentTime;
  }

  if (!isRelayOn) {
    unsigned long timeRemaining = OFF_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  } else {
    unsigned long timeRemaining = ON_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  }
  delay(1000);
}

  unsigned long timeRemaining = interval - (currentTime - previousTime);
  printTime(timeRemaining);
  delay(1000);
}
//................Set HH/MM/SS............
void printTime(unsigned long timeRemaining) {
  unsigned long hours = timeRemaining / 3600000;
  unsigned long mins = (timeRemaining % 3600000) / 60000;
  unsigned long secs = ((timeRemaining % 3600000) % 60000) / 1000;
//...................Serial & lcd Print
  Serial.print("Watering In: ");
  Serial.print(hours);
  Serial.print("h ");
  Serial.print(mins);
  Serial.print("m ");
  Serial.print(secs);
  Serial.println("s");
  lcd.setCursor(0, 2);
  lcd.print("Watering In: ");
  lcd.setCursor(0, 3);
  lcd.print(hours);
  lcd.print("h ");
  lcd.print(mins);
  lcd.print("m ");
  lcd.print(secs);
  lcd.println("s     ");

}

This piece of code is not part of any function. The sketch compiles when it is removed.

This copy/pasted section needs to be removed from the second sketch... and compare that section to your first sketch:

Full second sketch:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Arduino.h>
//...................Timer Variables
unsigned long currentTime;
unsigned long previousTime = 0;
unsigned long ON_DURATION = 10000; // ON_DURATION: 10 seconds
unsigned long OFF_DURATION = 40000; //2700000;//45min//7200000; // OFF_DURATION: 2 hours
unsigned long interval = OFF_DURATION;


//............... Sonar definitions
const int trigPin = 6;   // Trigger pin of HC-SR04 connected to Arduino digital pin 2
const int echoPin = 7;   // Echo pin of HC-SR04 connected to Arduino digital pin 3
#define Tankfill 8 //Tank fill relay
//..................Watering Relay


// ......Sonar Variables for duration and distance calculation
long duration;
int distance;
bool isRelayOn = false;
const int Watering = 12;
void setup() {
  Serial.begin(9600);   // Initialize serial communication
  //....................Sonar pin Mode............
  pinMode(trigPin, OUTPUT);  // Set the trigger pin as output
  pinMode(echoPin, INPUT);   // Set the echo pin as input
  //..................Tank Fill Relay pin Mode............
  pinMode(Tankfill, OUTPUT);
  pinMode(Tankfill, LOW);
  //.......................Watering Relay......................
  isRelayOn = true;

  pinMode(Watering, OUTPUT);
  pinMode(Watering, LOW);
  //........................................................
  lcd.init();
  lcd.backlight();
  lcd.begin(20, 4);
  lcd.setCursor(3, 0);
  lcd.print("System Starting");
  lcd.setCursor(3, 2);
  lcd.print("THE GERMINATOR");
  delay(3000);
  lcd.clear();


}

void loop() {

  // Clear the trigger pin and wait for a short delay
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigger pin high for 10 microseconds to start the measurement
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse on the echo pin
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  distance = duration * 0.034 / 2;

  //Print the distance to the serial monitor
  Serial.println("Distance  ");
  Serial.print(distance);
  Serial.print("cm  ");

  delay(1000);  // Wait for 1 second before taking the next measurement

  //...................Turning Pumpfill Relay on or off

  if (distance <= 34)
  {
    analogWrite(Tankfill, LOW);

    lcd.setCursor(0, 0);
    lcd.print(distance);
    //lcd.setCursor(0,4);
    lcd.print("cm");
    lcd.setCursor(0, 1);
    lcd.print("Tank is Full");
  }
  else if (distance >= 37)
  {
    analogWrite(Tankfill, HIGH);
    lcd.print(distance);
    lcd.setCursor(0, 1);
    lcd.print(" cm");
    lcd.setCursor(0, 1);
    lcd.print("Tank Filling to ");
    delay(1000);
  }
  //............................Timer calculation
  currentTime = millis();

  if (currentTime - previousTime >= interval) {
    if (isRelayOn) {
      interval = OFF_DURATION;
      isRelayOn = false;
      digitalWrite(12, HIGH);

    } else {
      interval = ON_DURATION;
      isRelayOn = true;
      digitalWrite(12, LOW);
    }
    previousTime = currentTime;
  }

  if (!isRelayOn) {
    unsigned long timeRemaining = OFF_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  } else {
    unsigned long timeRemaining = ON_DURATION - (currentTime - previousTime);
    printTime(timeRemaining);
  }
  delay(1000);
}

// unsigned long timeRemaining = interval - (currentTime - previousTime);
// printTime(timeRemaining);
// delay(1000);
// }

//................Set HH/MM/SS............
void printTime(unsigned long timeRemaining) {
  unsigned long hours = timeRemaining / 3600000;
  unsigned long mins = (timeRemaining % 3600000) / 60000;
  unsigned long secs = ((timeRemaining % 3600000) % 60000) / 1000;
  //...................Serial & lcd Print
  Serial.print("Watering In: ");
  Serial.print(hours);
  Serial.print("h ");
  Serial.print(mins);
  Serial.print("m ");
  Serial.print(secs);
  Serial.println("s");
  lcd.setCursor(0, 2);
  lcd.print("Watering In: ");
  lcd.setCursor(0, 3);
  lcd.print(hours);
  lcd.print("h ");
  lcd.print(mins);
  lcd.print("m ");
  lcd.print(secs);
  lcd.println("s     ");

}

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