Getting Functions to work

I am working on a sketch that utilizes “Functions”. The sketch works great until I add the functions to it. I get a “SaveDataToPi” was not declared in this scope.

I am using seconds to test the code and will be changing seconds to minutes in the actual running code.

I highlighted the line of code that is causing the error.

Thanks

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

void setup() 
{
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);  
}

void loop() 
{
   tmElements_t tm;

  if (RTC.read(tm))
   
    if ((tm.Second) == 01)  // using Second for testing
    
    {
    Serial.println("Second 01 code");
    Serial.println();
    delay(1000);
  //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv  
    SaveDataToPi(); // This function will save the hourly sensor data to the SQLdB on the Raspberry Pi
  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
    }
    
    if ((tm.Second) == 55)  // using Second for testing
  
    {
    Serial.println("Second 55 code"); 
    Serial.println();
    delay(1000);
    ReadSensors(); // This function will read the sensors for current hourly data 
    } 
       
   else 
   
    { 
     Serial.println("else part of  code,  ");
     Serial.println();    
     if ((tm.Second) != 01)
     delay(1000);
    }
   
void ReadSensors() //Read sensor function
 {
  Serial.print("Reading_Sensors");
  // This function will start reading the sensors for current hourly data 
 }
   
 void SaveDataToPi() // Save sensor data to Pi dB function
 {
  Serial.print("Saving_Sensor_Data to the Pi");
  // This function will save the hourly sensor data to the SQLdB on the Raspberry Pi 
 }     
}

You cannot define one function inside another. Both SaveDataToPi() and ReadSensors() definitions are inside the loop() function. Move the final } to above void ReadSensors().

Steve

Thank you ,
I overlooked that at least 20 times
Thanks again

if ((tm.Second) == 01)

One will always be one, whether expressed in octal, binary, decimal or hex.

These two errors should have pointed you in the right direction. If your function “was not declared in this scope” it sometimes means “The declaration of this function failed. See the error below.”

sketch_aug13a:50:3: error: a function-definition is not allowed here before '{' token
   {
   ^
sketch_aug13a:56:3: error: a function-definition is not allowed here before '{' token
   {
   ^

Just using Ctrl-T (Auto Format) in the IDE is also a big help. All function definitions should start at the beginning of the line, with no leading spaces. If they don't you have a problem.

Steve

I just looked at the single error line at the bottom of the screen. I did not think of copying the error data to notebook and reviewing it for more detail. Will do from now on
Thanks

ElectronicsNut:
I just looked at the single error line at the bottom of the screen. I did not think of copying the error data to notebook and reviewing it for more detail. Will do from now on
Thanks

No need to copy and paste. The text area below your sketch (containing the messages) will scroll, just like the text area containing your sketch. You can drag the separator between the two text areas to make one smaller and the other bigger.

Thank you!!