/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Motor Control
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-01-29 22:53:05

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Activate the L298N Dual Full-Bridge Motor Driver */
	/* connected to pins D4, D5, and D3. Set IN1 and IN2 */
	/* to HIGH, EN1 to 50% duty cycle, for 3 seconds. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Refine the user description requirement by */
	/* including the functionality to stop the motor */
	/* connected to the L298N Dual Full-Bridge Motor */
	/* Driver after a duration of 6 seconds. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <L298N.h>    // Include L298N library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t IN1_PIN = 4;
const uint8_t IN2_PIN = 5;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t EN_PIN = 3;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool IN1_rawData = 0;
bool IN2_rawData = 0;
uint8_t EN_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float IN1_phyData = 0.0;
float IN2_phyData = 0.0;
float EN_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
L298N motor(EN_PIN, IN1_PIN, IN2_PIN);    // Create L298N motor instance

unsigned long systemStartTime = 0;  // Variable to store the system start time
bool motorStarted = false;          // Flag to indicate if motor has started
bool motorStopped = false;          // Flag to indicate if motor has stopped

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(IN1_PIN, OUTPUT);
    pinMode(IN2_PIN, OUTPUT);
    pinMode(EN_PIN, OUTPUT);
    
    // Activate the L298N Dual Full-Bridge Motor Driver
    // Set IN1 and IN2 to HIGH, EN1 to 50% duty cycle, for 3 seconds
    IN1_rawData = HIGH;
    IN2_rawData = HIGH;
    EN_rawData = 128; // 50% duty cycle
    
    // Store the system start time
    systemStartTime = millis();
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    if (!motorStarted && millis() - systemStartTime >= 3000) {
        // Start the motor
        motorStarted = true;
        EN_rawData = 255; // 100% duty cycle
    }
    
    if (motorStarted && !motorStopped && millis() - systemStartTime >= 6000) {
        // Stop the motor after 6 seconds
        motorStopped = true;
        EN_rawData = 0; // 0% duty cycle
    }
    
    updateOutputs(); // Refresh output data
}

void updateOutputs()
{
    // Set output pins according to the raw data
    digitalWrite(IN1_PIN, IN1_rawData);
    digitalWrite(IN2_PIN, IN2_rawData);
    analogWrite(EN_PIN, EN_rawData);
}
