Combining code troubles

Hello. As far too new in arduino but getting really keen on it with some small project for now. Later on it will be expanded I hope. I'm making some basic aquarium controller but have some troubles combining the code. For now I have Temp sensor, and RTC. Waiting for Ph sensor and relay modules to be delivered. So the code for the Temp and for the Time as far as they are stand alone are working. Afetr combining them still have some error. Any help would be great and also provide me some basic combining skills. So the two codes:

RTC.txt (1.44 KB)

Temp.txt (1.72 KB)

It would hugely increase your chances of getting decent advice if you posted the code in which you attempted to combine them, along with the errors.

This thread of member Robin2 may help.

What is the error?

What's the code that is generating the error?

Please include the code and the error. It's more convenient for people helping you if you include the code inline with code tags, so we don't have to download anything.

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 



LiquidCrystal_I2C lcd(0x3F,20,4);  


int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup(void) {
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
//  lcd.setCursor(0, 1); 
//  lcd.print("Second Line");



}

void loop(void) {
  float temperature = getTemp();
  Serial.println(temperature);
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(5,0); 
  lcd.print(temperature);
  lcd.setCursor(10, 0);
  lcd.print((char)223);
  lcd.setCursor(11, 0);
  lcd.print("C");
  
  delay(1000);
}


float getTemp(){
  

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}

{
void setup()  {
  lcd.begin(20, 4);
  lcd.backlight();
 

  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");
    
}

void loop()
{
   digitalClockDisplay(); 
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
 
lcd.setCursor(0, 0);                                            // Set LCD cursor position (column, row)
lcd.print(hour());
lcd.print(":");
lcd.print (minute());
lcd.print(":");
lcd.print(second());


// Print text to LCD
                                     // Delay to read text
                               // Clear the display

}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

And the error is:
try.ino:93:1: error: expected unqualified-id before '{' token
Thank you very much in advance :slight_smile:
And I know the errors for the cursor location of the LCD print. Fixing them up. But still cannot fix the above error.

It looks like you have two different functions with the name setup. Same for the name loop. You can't do that. If you want to combine the codes, then you have to combine the setup and loop portions of each into a single code.

Look at the link in reply 1. It should be very helpful.

Try this, may need some tweaking in setup() still for the LCD :

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 



LiquidCrystal_I2C lcd(0x3F,20,4);  


int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup(void) {
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
//  lcd.setCursor(0, 1); 
//  lcd.print("Second Line");

  lcd.begin(20, 4); // <<<< probably want to do something with these?
  lcd.backlight();  // <<<< probably want to do something with these?


  // Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");

}

void loop(void) {
  float temperature = getTemp();
  Serial.println(temperature);
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(5,0); 
  lcd.print(temperature);
  lcd.setCursor(10, 0);
  lcd.print((char)223);
  lcd.setCursor(11, 0);
  lcd.print("C");
  
  delay(1000);

   digitalClockDisplay(); 
   delay(1000);  // <<<< need both delays now? or at all?
}


float getTemp(){
  

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}

{
/*  setup() code moved above
void setup()  {

    
}
*/
/* loop() code moved above
void loop()
{

}
*/

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
 
lcd.setCursor(0, 0);                                            // Set LCD cursor position (column, row)
lcd.print(hour());
lcd.print(":");
lcd.print (minute());
lcd.print(":");
lcd.print(second());


// Print text to LCD
                                     // Delay to read text
                               // Clear the display

}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 


int DS18S20_Pin = 2;
OneWire ds(DS18S20_Pin);
LiquidCrystal_I2C lcd(0x3F, 20, 4);


void setup(void)  {
  lcd.begin(20, 4);
  lcd.backlight();
  lcd.init();

  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");
    
}

void loop(void)
{
   digitalClockDisplay(); 
   delay(1000);
}
{
  float temperature = getTemp();
  Serial.println(temperature);
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(5,0); 
  lcd.print(temperature);
  lcd.setCursor(10, 0);
  lcd.print((char)223);
  lcd.setCursor(11, 0);
  lcd.print("C");
  
  delay(1000);
}


float getTemp(){
  

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}


void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
 
lcd.setCursor(0, 0);                                            // Set LCD cursor position (column, row)
lcd.print(hour());
lcd.print(":");
lcd.print (minute());
lcd.print(":");
lcd.print(second());


// Print text to LCD
                                     // Delay to read text
                               // Clear the display

}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Nice. The link from the first answer helped me, but still receiving the same error on line 32. :slight_smile:

void loop(void)
{
   digitalClockDisplay(); 
   delay(1000);
}   //here's the
{   //problem
  float temperature = getTemp();
  Serial.println(temperature);
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(5,0); 
  lcd.print(temperature);
  lcd.setCursor(10, 0);
  lcd.print((char)223);
  lcd.setCursor(11, 0);
  lcd.print("C");
  
  delay(1000);
}

Get rid of those (I assume all that's supposed to be in loop() ) - the first close curly brace is closing the function, so the second block is just a block of code floating in the middle of the file, which is not valid.

Yep those were the problem. Thank you so much for helping with this. Now I need to figure out why the clock is refreshing every two seconds :smiley:

GanevGa:
Yep those were the problem. Thank you so much for helping with this. Now I need to figure out why the clock is refreshing every two seconds :smiley:

Because you're refreshing the clock in loop() and loop runs every 2 seconds (the two delay(1000)'s each delay it 1 second)?

Ok removed the delay in the loop() and now its running normally. But now when it reaches 59sec, i starts to count09,19,29,39,49,59,11 sec and then it runs normally. What wrong with it :slight_smile:

it is printing a single digi5 number and the 9 from 59 is still sitting there. You could test if the number is below 10 and put a 0 before you print it.