Combining two codes, can't find my error.

Hello, I am trying to combine two codes using the tutorial I found here written by Robin2. Both codes work on their own. I can’t seem to figure out why the compiler says “updatepirPinState is not declared in this scope.” Can I get some help please? Also, keep in mind my username is also a description of me.

// PIR triggers relay__random flashing LEDs when dark

int pirPin = 5;                                   //PIR sensor 
int relayPin = 6;                                      // pin for the relay
int pirState = true;                                     //assume no motion detected
int val = 0;                                             // variable for reading the pin status
int minimummSecsLowForInactive = 3000;                    // If the sensor reports low for
long unsigned int timeLow;                               // more than this time, then assume no activity
boolean takeLowTime;                                     //is it true or fals 
int calibrationTime = 30;
 
int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int led5 = 12;                                                       
int leds[5] = {8,9,10,11,12};
int LDRpin = 0;
int LDRState = 0;


byte pirPinState = LOW;           //   LOW = off
byte LDRpinState = HIGH;


void setup() {
pinMode(relayPin, OUTPUT);                              // declare LED as output
pinMode(pirPin, INPUT);                                // declare sensor as input

 for (int jj; jj<sizeof(leds)/sizeof(int);jj++){
    pinMode(leds[jj],OUTPUT);
    
    pinMode(LDRpin, INPUT);
    delay(10);

 
Serial.begin(9600);
 
Serial.print("calibrating sensor ");                    //give the sensor some time to calibrate
for(int i = 0; i < calibrationTime; i++){                // serial print for testing 
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
}
void loop(){
       
       updateLDRpinState();  
       updatepirPinState();
    
void updatepirPinState(){  
}
  
val = digitalRead(pirPin);                                // read input value
if (val == HIGH) {                                               // check if the input is HIGH
  
digitalWrite(relayPin, HIGH);                                   //turn on Relay
if (pirState) {                                                  //if  PIR is high 
delay (7000);                                                    //keep it on for 7 sec 
pirState = false;

Serial.println("Motion detected!");                               // We only want to print on the output change
                                                                  
delay(50);
}
takeLowTime = true;
} 

else {
digitalWrite(relayPin, LOW);                                      // turn LED OFF
 
if (takeLowTime){
timeLow = millis();
takeLowTime = false;
}
 
if(!pirState && millis() - timeLow > minimummSecsLowForInactive){
pirState = true;
Serial.println("Motion ended!");
delay(50);
}
 
}
}

void updateLDRpinState(){
  
int LDRStatus = analogRead(LDRpin);
    LDRValue = analogRead(LDRpin); // read the value from the LDR
    Serial.println(LDRValue);      // print to the serial port for test
      delay(100);

if (LDRStatus <= 235) {              //it is dark, turn on lights
  
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
  delay(random(1000,1500));
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);

} else {                              //suns up turn light off

                                             
     digitalWrite (leds, LOW);
     digitalWrite (led2, LOW);
     digitalWrite (led3, LOW);
     digitalWrite (led4, LOW);          
     digitalWrite (led5, LOW);

}    
}

  
}

you are pry getting this error because you are missing a closing bracket some where before the updatepirPinState() function.

if there is a mix up in your brackets and a function isnt properly set aside in it's own brackets then the compiler doesnt see it and can result in an error when you are calling it.

go over all your brackets!

If you had some style, you might be able to find mistakes easier.

You had an unmatched curly brace, a function was defined in the wrong place (in loop() where you were trying to call it, and you had an undeclared variable type (LDRvalue).

I corrected your code and verified that it’ll compile.

// PIR triggers relay__random flashing LEDs when dark

int pirPin = 5;                                   //PIR sensor
int relayPin = 6;                                 // pin for the relay
int pirState = true;                             //assume no motion detected
int val = 0;                                     // variable for reading the pin status
int minimummSecsLowForInactive = 3000;            // If the sensor reports low for
long unsigned int timeLow;                       // more than this time, then assume no activity
boolean takeLowTime;                             //is it true or fals
int calibrationTime = 30;


int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int led5 = 12;
int leds[5] = {8, 9, 10, 11, 12};
int LDRpin = 0;
int LDRState = 0;
byte pirPinState = LOW;           //   LOW = off
byte LDRpinState = HIGH;

void setup() {
  pinMode(relayPin, OUTPUT);                          // declare LED as output
  pinMode(pirPin, INPUT);                                // declare sensor as input


  for (int jj; jj < sizeof(leds) / sizeof(int); jj++) {
    pinMode(leds[jj], OUTPUT);


    pinMode(LDRpin, INPUT);
    delay(10);




    Serial.begin(9600);


    Serial.print("calibrating sensor ");                    //give the sensor some time to calibrate
    for (int i = 0; i < calibrationTime; i++) {              // serial print for testing
      Serial.print(".");
      delay(1000);
    }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }
}


void updatepirPinState() {
}


void loop() {
  updateLDRpinState();
  updatepirPinState();
  val = digitalRead(pirPin);              // read input value
  if (val == HIGH) {                      // check if the input is HIGH
    digitalWrite(relayPin, HIGH);         //turn on Relay
    if (pirState) {                       //if  PIR is high
      delay (7000);                       //keep it on for 7 sec
      pirState = false;
      Serial.println("Motion detected!"); // We only want to print on the output change
      delay(50);
    }
    takeLowTime = true;
  }  else {
    digitalWrite(relayPin, LOW);            // turn LED OFF


    if (takeLowTime) {
      timeLow = millis();
      takeLowTime = false;
    }


    if (!pirState && millis() - timeLow > minimummSecsLowForInactive) {
      pirState = true;
      Serial.println("Motion ended!");
      delay(50);
    }
  }
}

void updateLDRpinState() {
  int LDRStatus = analogRead(LDRpin);
  int LDRValue = analogRead(LDRpin); // read the value from the LDR
  Serial.println(LDRValue);      // print to the serial port for test
  delay(100);

  if (LDRStatus <= 235) {        //it is dark, turn on lights
    digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], HIGH);
    delay(random(1000, 1500));
    digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], LOW);
  } else {                       //suns up turn light off
    digitalWrite (leds, LOW);
    digitalWrite (led2, LOW);
    digitalWrite (led3, LOW);
    digitalWrite (led4, LOW);
    digitalWrite (led5, LOW);
  }
}

taterking:
you are pry getting this error because you are missing a closing bracket some where before the updatepirPinState() function.

if there is a mix up in your brackets and a function isnt properly set aside in it's own brackets then the compiler doesnt see it and can result in an error when you are calling it.

go over all your brackets!

Thank you

Thank you dl324

I will go over all of it.
I like to try to understand what is going on.

I think dl324 may have misunderstood what the code is intended to do. Either that or I have.

It seems to me that you wanted loop() to contain only 2 function calls, nothing else. E.g.

void loop(){
       
       updateLDRpinState();  
       updatepirPinState();

// loop should end here with a }
    
void updatepirPinState(){  

} // but you put this } instead. It should be ABOVE the void updatepirPinState() to end the looop

Of course I could be wrong.

You also have a problem with an undefined LDRValue but I'm not sure what that's for...there seems to be no point in reading LDRpin twice into different variables. You also define a global variable LDRState but never use it for anything.

Steve

for (int jj; Oops

 for (int jj; jj<sizeof(leds)/sizeof(int);jj++){

oops++. That loop encompasses all of the the remaining setup function. Not really necessary to call Serial.begin so many times. Check your serial output and you’ll see the unnecessary repetition.
Edit: or you will once you fix the oops that AWOL pointed out.

Also, why jj?

slipstick:
I think dl324 may have misunderstood what the code is intended to do. Either that or I have.

I made no attempt to understand what the code is doing. I just addressed the compile errors.

I will test it and see. I'll let you know if it does what I want/

AWOL:
for (int jj; Oops

Thanks, but this part of code works OK by itself

OldJohn:
Thanks, but this part of code works OK by itself

...right up until the point that it doesn't.

Fix it, and don't think about doing it again.

I'm sorry, what does this: "...right up until the point that it doesn't.

Fix it, and don't think about doing it again." mean?

I mean "the variable jj is not initialised; this construct may work once in setup, but if you were to copy the code and paste it elsewhere in a sketch, it may well not work. You should fix it, and learn from the information I'm passing on to you."

AWOL:
I mean "the variable jj is not initialised; this construct may work once in setup, but if you were to copy the code and paste it elsewhere in a sketch, it may well not work. You should fix it, and learn from the information I'm passing on to you."

Thank you Awol. Your explanation in your next post was most helpful.

You know, learning this on your own is quite difficult. If I use someone else's code I do try to understand it. Most things I can figure out. A few only slightly. Also, I copy and paste a lot due to advance arthritis in my left hand.

I am mostly concerned with the "Fix it, and don't think about doing it again" line. That teaches me nothing. It seems pretty harsh and uncalled for. Especially for a Global Moderator. Also, Initialized is spelled with a "z".
Thanks again for your help and I shall not bother you in the future.

Also, Initialized is spelled with a "z".

Only to Oxford grads and the Atlantically-challenged.

I am mostly concerned with the "Fix it, and don't think about doing it again" line. That teaches me nothing.

I explained that the variable is not initialised.
The fix is to initialise it.

{penny drops} You're @shootout !

never looked at where you are. I'm Canadian. I am not @shootout

Penny drops. Oxford grad. Rich daddy. That’s why you are arrogant

You know, learning this on your own is quite difficult. If I use someone else's code I do try to understand it. Most things I can figure out. A few only slightly.

That, combined with attempting projects that are way above your skill level, is the real problem.

Your approach guarantees endless frustration, and impatient responses from forum members, who know that you have not done your homework.

Start with the simple examples that come with the Arduino and the many fine on line tutorials, in order to learn the programming language, and the special features of the Arduino.

Then you won't be so confused about really basic skills, such as using braces and writing a proper "for loop".

Thanks for being helpful and courteous