Motion LED with fade

I have a12v LED motion light project I am working on, using the below coding. So far it is working on my breadboard set up. Now, I would like to add fade in fade out to these lights. The fade code is listed further below.

I know I can copy and paste this somehow, but I'm more interested in knowing the logic or reasoning behind this.

I'm fairly new to this technology but find it very intriguing! The more knowledge I gain I see new areas where I could use this!

Thanks for your assistance.
/*

const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to the OUTPUT pin of motion sensor
const int RELAY_PIN = A5; // Arduino pin connected to the IN pin of relay
int motionStateCurrent = LOW; // current state of motion sensor's pin
int motionStatePrevious = LOW; // previous state of motion sensor's pin

void setup() {
Serial.begin(9600); // initialize serial
pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
motionStatePrevious = motionStateCurrent; // store old state
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state

if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
digitalWrite(RELAY_PIN, HIGH); // turn on
}
else
if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
digitalWrite(RELAY_PIN, LOW); // turn off
}
}

Below is the Fading code
/*
Created by ArduinoGetStarted.com

This example code is in the public domain

Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-fade-in-fade-out

This example fade LED:
+ fade in LED in 3 seconds
+ fade out LED in 3 seconds
+ without using delay() function. This is a non-blocking example
*/

#include <ezLED.h> // ezLED library

ezLED led(9); // create a LED object that attach to pin 9
bool isFadedIn = false;

void setup() {
Serial.begin(9600);
}

void loop() {
led.loop(); // MUST call the led1.loop() function in loop()

if (led.getState() == LED_IDLE) {
if (isFadedIn == false) {
Serial.println("FADING IN");
led.fade(0, 255, 3000); // fade in from 0 to 255 in 3000ms, fade immediately
//led.fade(0, 255, 3000, 1000); // fade in from 0 to 255 in 3000ms, fade after 1 second
isFadedIn = true;
} else {
Serial.println("FADING OUT");
led.fade(255, 0, 3000); // fade out from 255 to 0 in 3000ms, fade immediately
//led.fade(255, 0, 3000, 1000); // fade out from 255 to 0 in 3000ms, fade after 1 second
isFadedIn = false;
}
}

// To stop fading immediately, call led.cancel() function
}

Bit of knowledge:


Use code tags (the </> icon above the compose window) to make it easier to read and copy code for examination

Like this?

#include <ezLED.h> // ezLED library

ezLED led(9);  // create a LED object that attach to pin 9
bool isFadedIn = false;

void setup() {
  Serial.begin(9600);
}

void loop() {
  led.loop(); // MUST call the led1.loop() function in loop()

  if (led.getState() == LED_IDLE) {
    if (isFadedIn == false) {
      Serial.println("FADING IN");
      led.fade(0, 255, 3000);         // fade in from 0 to 255 in 3000ms, fade immediately
      //led.fade(0, 255, 3000, 1000); // fade in from 0 to 255 in 3000ms, fade after 1 second
      isFadedIn = true;
    } else {
      Serial.println("FADING OUT");
      led.fade(255, 0, 3000);          // fade out from 255 to 0 in 3000ms, fade immediately
      //led.fade(255, 0, 3000, 1000);  // fade out from 255 to 0 in 3000ms, fade after 1 second
      isFadedIn = false;
    }
  }

  // To stop fading immediately, call led.cancel() function
}