/********* 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: "Sensor Control"
	- Source Code compiled for: Arduino Mega
	- Source Code created on: 2024-06-24 04:58:46

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* take input from 4 hall sensor for each wheel */
/****** SYSTEM REQUIREMENT 2 *****/
	/* take input pwm signal and send out to 4 brushless */
	/* esc for each wheel. change the motor speed to */
	/* maintain traction */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t hallSensor1_PIN_A0 = A0;
const uint8_t hallSensor2_PIN_A1 = A1;
const uint8_t hallSensor3_PIN_A2 = A2;
const uint8_t hallSensor4_PIN_A3 = A3;
const uint8_t pwm_PIN_D2 = 2;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t output1_PIN_D3 = 3;
const uint8_t output2_PIN_D4 = 4;
const uint8_t output3_PIN_D5 = 5;
const uint8_t output4_PIN_D6 = 6;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
uint8_t output1_PIN_D3_rawData = 0;
uint8_t output2_PIN_D4_rawData = 0;
uint8_t output3_PIN_D5_rawData = 0;
uint8_t output4_PIN_D6_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float output1_PIN_D3_phyData = 0.0;
float output2_PIN_D4_phyData = 0.0;
float output3_PIN_D5_phyData = 0.0;
float output4_PIN_D6_phyData = 0.0;

/***** DEFINITION OF HALL SENSOR VARIABLES *****/
/***** used to store hall sensor data *****/
int hallSensor1_value = 0;
int hallSensor2_value = 0;
int hallSensor3_value = 0;
int hallSensor4_value = 0;

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(hallSensor1_PIN_A0, INPUT);
    pinMode(hallSensor2_PIN_A1, INPUT);
    pinMode(hallSensor3_PIN_A2, INPUT);
    pinMode(hallSensor4_PIN_A3, INPUT);
    pinMode(pwm_PIN_D2, INPUT);

    pinMode(output1_PIN_D3, OUTPUT);
    pinMode(output2_PIN_D4, OUTPUT);
    pinMode(output3_PIN_D5, OUTPUT);
    pinMode(output4_PIN_D6, OUTPUT);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    readHallSensors(); // Read hall sensor data
    updateOutputs(); // Refresh output data
}

void readHallSensors()
{
    // Read the value from each hall sensor
    hallSensor1_value = analogRead(hallSensor1_PIN_A0);
    hallSensor2_value = analogRead(hallSensor2_PIN_A1);
    hallSensor3_value = analogRead(hallSensor3_PIN_A2);
    hallSensor4_value = analogRead(hallSensor4_PIN_A3);

    // Process the hall sensor data to control motor speed
    // This is a placeholder for the actual logic to maintain traction
    output1_PIN_D3_rawData = map(hallSensor1_value, 0, 1023, 0, 255);
    output2_PIN_D4_rawData = map(hallSensor2_value, 0, 1023, 0, 255);
    output3_PIN_D5_rawData = map(hallSensor3_value, 0, 1023, 0, 255);
    output4_PIN_D6_rawData = map(hallSensor4_value, 0, 1023, 0, 255);
}

void updateOutputs()
{
    analogWrite(output1_PIN_D3, output1_PIN_D3_rawData);
    analogWrite(output2_PIN_D4, output2_PIN_D4_rawData);
    analogWrite(output3_PIN_D5, output3_PIN_D5_rawData);
    analogWrite(output4_PIN_D6, output4_PIN_D6_rawData);
}

/* END CODE */