Joining two codes

Hi everyone,
I'm hoping to get some help putting these two codes together. I have tried both codes by themselves and both work great. I also tested all components. I have tried putting the charger code first and second. Obviously, it's not enough. Nothing works the way it is. Any help will be appreciated.

/* Alarm At Night Code.
    In daylight a solar panel will charge the battery.  The program, along with some external components,
    will turn the charger on and off until the battery is at 14 volts.  When the battery reaches
    14 volts it will stop charging.
    The alarm portion checks for darkness using a LDR.  If it is dark and the PIR is triggered the two relays will activate an alarm and blinking lights for five seconds.
*/
//-----Alarm Constants------------------------------
unsigned long startTime = 0;   // time of trigger
unsigned long previousBlinkTime = 0;
const int ldrPin = A1;
const int relay1Pin = 2;
const int relay2Pin = 3;
const int pirPin = 7;
//-----Alarm Variables------------------------------
const unsigned long triggerDelay = 5000;    //Set up Intervals
const unsigned long blinkDelay = 150;
byte ldrState = 0;
byte pirState = LOW;
bool isTriggered = false;
//-----Charger Constants-----------------------------
int chargerLight = 10;
int cEnable = 13;
int vinPin = A0;
//-----Charger Variables-----------------------------
int voltVal;
unsigned long chargeOn = 4000;
unsigned long chargeOff = 4000;
unsigned long cVariable = 600;
//----Setup-------------------------------------------
void setup () {
  pinMode(pirPin, INPUT);
  pinMode(ldrPin, INPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);

  pinMode( chargerLight, OUTPUT);
  pinMode(cEnable, OUTPUT);
  pinMode(vinPin, INPUT);

  Serial.begin(9600);
  digitalWrite(cEnable, LOW);
}
void loop()  {

  //-----------Alarm---------------------------------------
  ldrState = analogRead(ldrPin);  //set up LDR
  Serial.print(ldrState);

  if (ldrState <= 300) {
    pirState = digitalRead(pirPin);
    unsigned long currentMillis = millis();


    if ( pirState == HIGH && isTriggered == false) {
      isTriggered = true;                          // initial trigger so start timing
      startTime = currentMillis;
      previousBlinkTime = currentMillis;
      digitalWrite(relay1Pin, HIGH );
      digitalWrite(relay2Pin, HIGH );
    }
    if ( isTriggered == true ) {                 // trigger has already happened so check timing

      if ( currentMillis - previousBlinkTime >= blinkDelay ) {                  // first check if time to blink led2
        digitalWrite(relay2Pin, !digitalRead(relay2Pin) );  // toggle the led
        previousBlinkTime = currentMillis;
      }
    }
    if ( currentMillis - startTime >= triggerDelay ) {                              // check if it is time to finish
      // all done so turn everything off and reset trigger so it can happen again
      digitalWrite(relay1Pin, LOW );
      digitalWrite(relay2Pin, LOW );
      isTriggered = false;

      //--------Charger Controller-----------------------------
      //-------------------------------------------------------

      voltVal = analogRead(vinPin);
      while (voltVal > cVariable) {
        voltVal = analogRead(vinPin);
        Serial.print("Charge off val = ");
        Serial.println(voltVal);
        Serial.print("Vbat = ");
        Serial.println(voltVal * .0048 + 10.0);
        delay(2000);
      }
      digitalWrite(cEnable, HIGH);
      digitalWrite(chargerLight, HIGH);
      delay(chargeOn);
      digitalWrite(cEnable, LOW);
      digitalWrite(chargerLight, LOW);
      delay(chargeOff);
    }
  }
}

I see some serial debug prints... can you please post the output?

It looks like everything including the charging part is inside the check for it being dark (assuming ldrState <= 300 means it's dark - a few more comments might help). That doesn't make much sense if it's a solar charger.

When exactly are things supposed to happen? Maybe the charger routine should be in an ''else' clause after the end of the "if (ldrState <= 300) alarm routine is complete?

Steve

aarg:
I see some serial debug prints... can you please post the output?

Sorry, I can't get Serial Moniter or Putty or Kitty to work properly. The LDR does change voltage though.
Thank

slipstick:
It looks like everything including the charging part is inside the check for it being dark (assuming ldrState <= 300 means it's dark - a few more comments might help). That doesn't make much sense if it's a solar charger.

When exactly are things supposed to happen? Maybe the charger routine should be in an ''else' clause after the end of the "if (ldrState <= 300) alarm routine is complete?

Steve

Code with more comments

/* Alarm At Night Code.
    In daylight a solar panel will charge the battery.  The program, along with some external components,
    will turn the charger on and off until the battery is at 14 volts.  When the battery reaches
    14 volts it will stop charging.
    The alarm portion checks for darkness using a LDR.  If it is dark and the PIR is triggered the two relays will
    activate an alarm and blinking lights for five seconds.
*/
//-----Alarm Constants------------------------------
unsigned long startTime = 0;   // time of trigger
unsigned long previousBlinkTime = 0;
const int ldrPin = A1;
const int relay1Pin = 2;
const int relay2Pin = 3;
const int pirPin = 7;
//-----Alarm Variables------------------------------
const unsigned long triggerDelay = 5000;    //Set up Intervals
const unsigned long blinkDelay = 150;
byte ldrState = 0;
byte pirState = LOW;
bool isTriggered = false;
//-----Charger Constants-----------------------------
int chargerLight = 10;
int cEnable = 13;
int vinPin = A0;
//-----Charger Variables-----------------------------
int voltVal;
unsigned long chargeOn = 4000;
unsigned long chargeOff = 4000;
unsigned long cVariable = 600;
//----Setup-------------------------------------------
void setup () {
  pinMode(pirPin, INPUT);
  pinMode(ldrPin, INPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);

  pinMode( chargerLight, OUTPUT);
  pinMode(cEnable, OUTPUT);
  pinMode(vinPin, INPUT);

  Serial.begin(9600);
  digitalWrite(cEnable, LOW);
}
void loop()  {

  //-----------Alarm---------------------------------------
  ldrState = analogRead(ldrPin);  //set up LDR
  Serial.print(ldrState);

  if (ldrState <= 300) {                                  
    pirState = digitalRead(pirPin);
    unsigned long currentMillis = millis();


    if ( pirState == HIGH && isTriggered == false) {     //If it is dark and the PIR is get triggered, the alarm comes on
      isTriggered = true;                                   // initial trigger so start timing
      startTime = currentMillis;
      previousBlinkTime = currentMillis;
      digitalWrite(relay1Pin, HIGH );          //  Alarm on
      digitalWrite(relay2Pin, HIGH );
    }
    if ( isTriggered == true ) {                 // trigger has already happened so check timing

      if ( currentMillis - previousBlinkTime >= blinkDelay ) {  // first check if time to blink led2
        digitalWrite(relay2Pin, !digitalRead(relay2Pin) );  // toggle the led
        previousBlinkTime = currentMillis;
      }
    }
    if ( currentMillis - startTime >= triggerDelay ) {       // check if it is time to finish
      // all done so turn everything off and reset trigger so it can happen again
      digitalWrite(relay1Pin, LOW );
      digitalWrite(relay2Pin, LOW );            //Alarm off
      isTriggered = false;

      //--------Charger Controller-----------------------------
      //-------------------------------------------------------

      voltVal = analogRead(vinPin);   //read voltage pin ( through a 10V zener diode so range is 0vV  to 5V
      
      while (voltVal > cVariable) {
        
        Serial.print("Charge off val = ");  //Print the value
        Serial.println(voltVal);
        Serial.print("Vbat = ");                   //print the voltage
        Serial.println(voltVal * .0048 + 10.0);    // convert the reading
        delay(2000); 
      }
      digitalWrite(cEnable, HIGH);       //Allow the battery to charge 4 seconde
      digitalWrite(chargerLight, HIGH);  //show that the charger is working 
      digitalWrite(cEnable, LOW);        //Stop 4 seconds
      digitalWrite(chargerLight, LOW);
      delay(chargeOff);   //should continue until battery voltage is 14V
    }
  }
}

Would this be a better approach?

Void loop () {

charger ()
alarm()

If (LDR >= 300) {

alarm()

else{

charger()
}

void alarm() {

code
}
void charger(){
code
}

That looks better except you don't need the first two calls to alarm() and charger(). Just start with the if.

Steve

slipstick:
That looks better except you don't need the first two calls to alarm() and charger(). Just start with the if.

Steve

Thanks