Little help combining sketches

Hi all..

i just started with Arduino about a month ago and learned a lot about it since then.
I know how to set up a simple sketch and how to modify sketches found online. Now i am implenting Arduino into my car to monitor some temperatures with several DS18B20. Assigned on a single bus and hardcoded the addresses to its functions. All good.. read out through bluetooth with MIT created app. That is one sketch.

Now i made another app with MIT to control things like door lock and unlock, controlling hood latch and trunk an so forth.. also through bluetooth with it's own sketch.

Now the problem lies in combining both sketches to one and hoping someone would take a look at it:

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

SoftwareSerial mySerial(11,12);

// 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
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 6 DS18B20s
uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };
void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  mySerial.begin(9600);
 
}

void loop(void)



{
int i;

if (mySerial.available())
{
  i=mySerial.read();
  Serial.println("DATA RECEIVED:");
  
  if(i=='1')
  {
  Serial.println("Running Aux 3");
  digitalWrite(3, HIGH);
  delay(500);                       
  digitalWrite(3, LOW);
  delay(100);
  }
  
  if(i=='2')
  {
  Serial.println("Running Aux 4");
  digitalWrite(4, HIGH);
  delay(500);                       
  digitalWrite(4, LOW);
  delay(100);
  }

  if(i=='3')
  {
  Serial.println("Running Aux 5");
  digitalWrite(5, HIGH);
  delay(500);                       
  digitalWrite(5, LOW);
  delay(100);
  }


  if(i=='4')
  {
  Serial.println("Running Aux 6");
  digitalWrite(6, HIGH);
  delay(500);                       
  digitalWrite(6, LOW);
  delay(100);
  }

  if(i=='5')
  {
  Serial.println("Running Aux 7");
  digitalWrite(7, HIGH);
  delay(500);                      
  digitalWrite(7, LOW);
  delay(100);
  }

  if(i=='6')
  {
  Serial.println("Running Aux 8");
  digitalWrite(8, HIGH);
  delay(500);                    
  digitalWrite(8, LOW);
  delay(100);
  }

  if(i=='7')
  {
  Serial.println("Running Aux 9");
  digitalWrite(9, HIGH);
  delay(500);         
  digitalWrite(9, LOW);
  delay(100);
  }

  if(i=='8')
  {
  Serial.println("Running Aux 10");
  digitalWrite(10, HIGH);
  delay(500);                     
  digitalWrite(10, LOW);
  delay(100);
  }
}


{
  sensors.requestTemperatures();
  
  
  printTemperature(sensor1);
  

  printTemperature(sensor2);
  

  printTemperature(sensor3);

 
  printTemperature(sensor4);

  
  printTemperature(sensor5);

  
  printTemperature(sensor6);
  
  Serial.println();
  delay(500);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC, 1);
     
  Serial.print("|");
}


}

keep getting: 'printTemperature' was not declared in this scope

Can you see the problem now?

No... thats the thing.. the sketch on it's own works fine
How could it differ when combined?

#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
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 6 DS18B20s
uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };
void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{
  sensors.requestTemperatures();
  
  
  printTemperature(sensor1);
  

  printTemperature(sensor2);
  

  printTemperature(sensor3);

 
  printTemperature(sensor4);

  
  printTemperature(sensor5);

  
  printTemperature(sensor6);
  
  Serial.println();
  delay(500);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC, 1);
     
  Serial.print("|");
}

Looks at the very bottom of the code I quoted.
What's that '}' doing, all on its own?

something i also have been over and over.. removed it just now but does not make any difference..

E:\Users\Werkplaats\Downloads\multiple_DS18B20_V3\multiple_DS18B20_V3.ino: In function 'void loop()':

multiple_DS18B20_V3:122:3: error: 'printTemperature' was not declared in this scope

printTemperature(sensor1);

^~~~~~~~~~~~~~~~

E:\Users\Werkplaats\Downloads\multiple_DS18B20_V3\multiple_DS18B20_V3.ino:122:3: note: suggested alternative: 'DallasTemperature'

printTemperature(sensor1);

^~~~~~~~~~~~~~~~

DallasTemperature

multiple_DS18B20_V3:144:1: error: a function-definition is not allowed here before '{' token

{

^

multiple_DS18B20_V3:149:1: error: expected '}' at end of input

}

^

exit status 1

'printTemperature' was not declared in this scope

I can see error messages, but not the code that generated them.

Big clue there

Thats why i am having a hard time finding the error.
I have been over the accolades many times now.

Could you copy the code in IDE and see if you spot something?

thanks in advance

I can't, because I'm not near a computer, but your IDE should help you match all the '{' to their partner '}'.

Thats just it... they all match

The compiler disagrees.

C++ does not allow the definition of a function inside another function

Compliled a new combined sketch, wnet over every single accolade.. no function inside a function
I am not seeing it

I can't see your code.
I can't see your error messages
(I don't see any "accolades")

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

#define ONE_WIRE_BUS 2

SoftwareSerial mySerial(11, 12);
OneWire oneWire(ONE_WIRE_BUS);


DallasTemperature sensors(&oneWire);


uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };



void setup(void)
{

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  mySerial.begin(9600);
  Serial.begin(9600);
  sensors.begin();
}






void loop(void)

{
  int i;

  if (mySerial.available())

  {
    i = mySerial.read();
    Serial.println("DATA RECEIVED:");
    if (i == '1')
    {

      Serial.println("Running Aux 3");
      digitalWrite(3, HIGH);
      delay(500);
      digitalWrite(3, LOW);
      delay(100);
    }
    if (i == '2')
    {
      Serial.println("Running Aux 4");
      digitalWrite(4, HIGH);
      delay(500);
      digitalWrite(4, LOW);
      delay(100);
    }

    if (i == '3')
    {
      Serial.println("Running Aux 5");
      digitalWrite(5, HIGH);
      delay(500);
      digitalWrite(5, LOW);
      delay(100);
    }


    if (i == '4')
    {
      Serial.println("Running Aux 6");
      digitalWrite(6, HIGH);
      delay(500);
      digitalWrite(6, LOW);
      delay(100);
    }

    if (i == '5')
    {
      Serial.println("Running Aux 7");
      digitalWrite(7, HIGH);
      delay(500);
      digitalWrite(7, LOW);
      delay(100);
    }

    if (i == '6')
    {
      Serial.println("Running Aux 8");
      digitalWrite(8, HIGH);
      delay(500);
      digitalWrite(8, LOW);
      delay(100);
    }

    if (i == '7')
    {
      Serial.println("Running Aux 9");
      digitalWrite(9, HIGH);
      delay(500);
      digitalWrite(9, LOW);
      delay(100);
    }

    if (i == '8')
    {
      Serial.println("Running Aux 10");
      digitalWrite(10, HIGH);
      delay(500);
      digitalWrite(10, LOW);
      delay(100);
    }
  }

  {
    sensors.requestTemperatures();


    printTemperature(sensor1);


    printTemperature(sensor2);


    printTemperature(sensor3);


    printTemperature(sensor4);


    printTemperature(sensor5);


    printTemperature(sensor6);

    Serial.println();
    delay(500);
  }

  void printTemperature(DeviceAddress deviceAddress)
  {
    float tempC = sensors.getTempC(deviceAddress);
    Serial.print(tempC, 1);

    Serial.print("|");
  }
}






  

still get:

E:\Users\Werkplaats\Downloads\multiple_DS18B20_V4\multiple_DS18B20_V4.ino: In function 'void loop()':
multiple_DS18B20_V4:133:5: error: 'printTemperature' was not declared in this scope
printTemperature(sensor1);
^~~~~~~~~~~~~~~~
E:\Users\Werkplaats\Downloads\multiple_DS18B20_V4\multiple_DS18B20_V4.ino:133:5: note: suggested alternative: 'DallasTemperature'
printTemperature(sensor1);
^~~~~~~~~~~~~~~~
DallasTemperature
multiple_DS18B20_V4:155:3: error: a function-definition is not allowed here before '{' token
{
^
exit status 1
'printTemperature' was not declared in this scope

If you can't see that this function is defined inside another function, it's going to be quite hard to help.

There should not, and cannot, be a } after the closing brace of printTemperature, yet in your code, there is.

You can place the cursor on a brace { opening or } closing, and the IDE will show you the matching brace…

Autoformat in the IDE will line them up making it even easier to spot a closing brace placed a full function too far down, thereby enclosing the function in the one above.

a7

Dammit.... you can only see it until you actually see it.
Can not thank you enough

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

#define ONE_WIRE_BUS 2

SoftwareSerial mySerial(11, 12);
OneWire oneWire(ONE_WIRE_BUS);


DallasTemperature sensors(&oneWire);


uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };



void setup(void)
{

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  mySerial.begin(9600);
  Serial.begin(9600);
  sensors.begin();
}






void loop(void)

{
  int i;

  if (mySerial.available())

  {
    i = mySerial.read();
    Serial.println("DATA RECEIVED:");
    if (i == '1')
    {

      Serial.println("Running Aux 3");
      digitalWrite(3, HIGH);
      delay(500);
      digitalWrite(3, LOW);
      delay(100);
    }
    if (i == '2')
    {
      Serial.println("Running Aux 4");
      digitalWrite(4, HIGH);
      delay(500);
      digitalWrite(4, LOW);
      delay(100);
    }

    if (i == '3')
    {
      Serial.println("Running Aux 5");
      digitalWrite(5, HIGH);
      delay(500);
      digitalWrite(5, LOW);
      delay(100);
    }


    if (i == '4')
    {
      Serial.println("Running Aux 6");
      digitalWrite(6, HIGH);
      delay(500);
      digitalWrite(6, LOW);
      delay(100);
    }

    if (i == '5')
    {
      Serial.println("Running Aux 7");
      digitalWrite(7, HIGH);
      delay(500);
      digitalWrite(7, LOW);
      delay(100);
    }

    if (i == '6')
    {
      Serial.println("Running Aux 8");
      digitalWrite(8, HIGH);
      delay(500);
      digitalWrite(8, LOW);
      delay(100);
    }

    if (i == '7')
    {
      Serial.println("Running Aux 9");
      digitalWrite(9, HIGH);
      delay(500);
      digitalWrite(9, LOW);
      delay(100);
    }

    if (i == '8')
    {
      Serial.println("Running Aux 10");
      digitalWrite(10, HIGH);
      delay(500);
      digitalWrite(10, LOW);
      delay(100);
    }
  }

  {
    sensors.requestTemperatures();


    printTemperature(sensor1);


    printTemperature(sensor2);


    printTemperature(sensor3);


    printTemperature(sensor4);


    printTemperature(sensor5);


    printTemperature(sensor6);

    Serial.println();
    delay(500);
  }

  
}

void printTemperature(DeviceAddress deviceAddress)
  {
    float tempC = sensors.getTempC(deviceAddress);
    Serial.print(tempC, 1);

    Serial.print("|");
  }




  

Hear that. Get accustomed to it. I have stared at things to nearly going blind… get up, stretch, go to the beach, take a shower, sleep on it and BAM, how was I not seeing that?

And oh yeah, that error message actually does make sense. Now. :expressionless:

a7

I trimmed it a bit, but I'm on my phone, so not compiled or tested

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

#define ONE_WIRE_BUS 2

SoftwareSerial mySerial(11, 12);
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };

const byte auxPin [] {3, 4, 5, 6, 7, 8, 9, 10}; 

void setup(void)
{
  for (auto& pin : auxPin) {
    pinMode(pin, OUTPUT);
  }
  mySerial.begin(9600);
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{
  if (mySerial.available()) {
    int i = mySerial.read();
    Serial.println("DATA RECEIVED:");
 
    if (i >= '1' && i <= '8') {
      int index = i - '1';
      Serial.print("Running Aux ");
      Serial.println ((char)i);
      digitalWrite(auxPin [index], HIGH);
      delay(500);
      digitalWrite(auxPin [index], LOW);
      delay(100);
    }
  }

  sensors.requestTemperatures();
  printTemperature(sensor1);
  printTemperature(sensor2);
  printTemperature(sensor3);
  printTemperature(sensor4);
  printTemperature(sensor5);
  printTemperature(sensor6);

  Serial.println();
  delay(500);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC, 1);

  Serial.print("|");
}

@alto777 indeed very satisfying solving the issue but couldn't have done it without @anon73444976

would have taking me ages and several headaches :slight_smile:

@anon73444976 again thank you for the trimmed sketch. Tested fine,
will be using that one