Cleaning up my Code

Hello Guys , I have a working Sketch and i admit it is a bit of copy and pasted and own coding together but it all works as I wanted to. Now I am wondering if there is any why that I could "clean" the code up or even optimize it a bit better any suggestion would be appreciated!

//Constant Values
const int LDR = A0;
const int Relay = 2;   // The Relay that Controles the Lights
int pinButton = 8;     // The main switch that will be wall mounted (Might be a "bell" Push button
int AlarmRelay = 9 ;  // The Relay that will be triggered when the alarm goes off
int LED = 13;         // The Led that will show that the trigger is recieved ( Else will need to go outside )
int CheckPin = 3;     // This pin is Used to ensure that Timer is only used when the Alarm Relay is activated first
//

//Varialbles
int LDRstate = LOW;
int ALARMstate = LOW ;
int previous2 = LOW;
int stateCheckPin = LOW;

// Values for Alarm trigger
int stateButtonAlarm ;              // to Chek the state of the Alarm Relay
int ledState = LOW;                // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 100;               // milliseconds of on-time
long OffTime = 5000;           // milliseconds of how long before the light time goes off after Alarm has stopped

// VALUES for LDR
int sensorValue = 0;  // variable to store the value coming from the sensor
int previous = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled

// Values for Push Button
unsigned long LastOnTime = 0;
boolean RelayState = LOW;
int stateRelay = LOW;
int stateButton;
long debounce = 2000;

void setup() {
  pinMode (AlarmRelay,INPUT);
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
  pinMode (LED, OUTPUT);
  pinMode (CheckPin,OUTPUT);
  Serial.begin(9600); //sets serial port for communication

}

void loop() {

// Cell Phone Switch Code 

   int LDRValue = analogRead(LDR); // read the value from the LDR
   Serial.println(LDRValue); //prints the values coming from the sensor on the screen

// code for checking the LDR and if the phone is on
  // If the screen is ON...
  if(LDRValue > 400) {

   //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
   if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (LED , LOW);
      digitalWrite (CheckPin , HIGH);
    } else {
       stateRelay = HIGH;
       digitalWrite (LED , HIGH);
       digitalWrite (CheckPin , HIGH);
    }

    }
   
  LastOnTime = millis();
  }

 // Code for house on off switch so that if the cell phone switch was on it can be swithed off inhouse
   
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (CheckPin , HIGH);
     
    } else {
       stateRelay = HIGH;
       digitalWrite (CheckPin , HIGH);
  }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  digitalWrite (LED , stateRelay);
 // digitalWrite (CheckPin , stateRelay);
  
    delay (200) ;
  previous == stateButton;

{

// Alarm Code
}
stateButtonAlarm = digitalRead(AlarmRelay);
stateCheckPin = digitalRead(CheckPin); 
   unsigned long currentMillis = millis();
   if((stateButtonAlarm == HIGH  ) && (currentMillis - previousMillis >= OnTime))
  {
    stateRelay = HIGH;  // Turn it on
    previousMillis = currentMillis;  // Remember the time
    digitalWrite (CheckPin , LOW);   // Chage to low so that timer can be used to switch the Light off
    digitalWrite(LED, stateRelay);  // Update the actual LED
    digitalWrite (Relay,stateRelay);
  }
  else if ((stateButtonAlarm == LOW && stateCheckPin == LOW ) && (currentMillis - previousMillis >= OffTime)) // Only activate it Checkpin is Low 
  {
    stateRelay = LOW;  // turn it off
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(LED, stateRelay);    // Update the actual LED
    digitalWrite (Relay,stateRelay);
   
  }
}
[code]

You could stop using int data type for a bunch of stuff that doesn't need it, esp if you only need boolean, which you used once that I saw, so maybe you were unaware since that was one of the copy/pasted things.

Is it working as intended? Is it fast enough? Then stop optimising. Move on to the next project.

The code looks relatively clean to me. Much better quality than what we usually see here. No magic numbers, well commented. Good use of whitespace. If that was a school assignment (and it does what it's supposed to) then I would give you full marks.

Super thank you for the feed back! This is my first "put together" project as I have compiled a couple of single sketches. I was so relieved when I got every thing running - took me almost two weeks (not full days). The learning experiences is quite fun and I am slowly but surly getting to understand each line and what it does!

As has been said, if it works then why tidy it, but I hate to see repeated code.

So

    //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
      if (stateRelay == HIGH) {
        stateRelay = LOW;
        digitalWrite (LED , LOW);
        digitalWrite (CheckPin , HIGH);
      } else {
        stateRelay = HIGH;
        digitalWrite (LED , HIGH);
        digitalWrite (CheckPin , HIGH);
      }

    }

could be changed to

   //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
      stateRelay = !stateRelay;  //change the relay state
      digitalWrite (LED , stateRelay);
      digitalWrite (CheckPin , HIGH);
    }

UKHeliBob: As has been said, if it works then why tidy it, but I hate to see repeated code.

[/code] could be changed to

   //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
      stateRelay = !stateRelay;  //change the relay state
      digitalWrite (LED , stateRelay);
      digitalWrite (CheckPin , HIGH);
    }

Thank you for the tip will look into this way - and play with it!

One very simple way of tidying-up is to use the auto-format tool in the IDE.