CCCPaste

Scanner rev_117

/********* 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: Scanner
	- Source Code compiled for: Arduino Mega
	- Source Code created on: 2023-11-07 08:11:03
	- Source Code generated by: AlexWind

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

/****** SYSTEM REQUIREMENT 1 *****/
/* Display the data read from sensor and pot */

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateInputs(void);
float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
float map_f(float x, float in_min, float in_max, float out_min, float out_max);
void convertInputsFromRawToPhyData(void);

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t sensore_PIN_A0 = A0;
const uint8_t pot_Potentiometer_Vout_PIN_A1 = A1;

/***** DEFINITION OF I2C PINS *****/
const uint8_t display_LCD1602I2C_I2C_PIN_SDA_D20 = 20;
const uint8_t display_LCD1602I2C_I2C_PIN_SCL_D21 = 21;
const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;

/****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_A0 = 3;
const float voltage_Temperature_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_A0] PROGMEM =
{
    {0.0, 2.0, 3.0}, // Voltage [V]
    {20.0, 60.0, 100.0} // Temperature [°C]
};

const uint8_t SEGMENT_POINTS_voltage_rotazione_PIN_A1 = 2;
const float voltage_rotazione_PIN_A1_lookup[2][SEGMENT_POINTS_voltage_rotazione_PIN_A1] PROGMEM =
{
    {0.0, 5.0}, // Voltage [V]
    {0.0, 360.0} // rotazione [gradi]
};

/***** DEFINITION OF INPUT RAW VARIABLES *****/
/***** used to store raw data *****/
unsigned int sensore_PIN_A0_rawData = 0; // Analog Input
unsigned int pot_Potentiometer_Vout_PIN_A1_rawData = 0; // Analog Input

/***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float sensore_PIN_A0_phyData = 0.0; // Temperature [°C]
float pot_Potentiometer_Vout_PIN_A1_phyData = 0.0; // rotazione [gradi]

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // Initialize the LCD object

void setup(void)
{
    // put your setup code here, to run once:

    pinMode(sensore_PIN_A0, INPUT);
    pinMode(pot_Potentiometer_Vout_PIN_A1, INPUT);

    lcd.init(); // Initialize the LCD
    lcd.backlight(); // Turn on the backlight

    lcd.print("Hello, Arduino!"); // Example LCD display

}

void loop(void)
{
    // put your main code here, to run repeatedly:

    updateInputs(); // Refresh input data

    convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve

}

void updateInputs()
{
    sensore_PIN_A0_rawData = analogRead(sensore_PIN_A0);
    pot_Potentiometer_Vout_PIN_A1_rawData = analogRead(pot_Potentiometer_Vout_PIN_A1);
}

/* BLOCK lookup_phyData_from_voltage */
float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
{
	// Search table for appropriate value.
	uint8_t index = 0;

	const float *voltagePointer = &voltage_phyData_lookup[0];
	const float *phyDataPointer = &voltage_phyData_lookup[segment_points];

	// Perform minimum and maximum voltage saturation based on characteristic curve
	voltage = min(voltage, voltagePointer[segment_points-1]);
	voltage = max(voltage, voltagePointer[0]);

	while( pgm_read_float(voltagePointer[index]) <= voltage && index < segment_points )
		index++;

	// If index is zero, physical value is smaller than our table range
	if( index==0 )
	{
		return map_f( voltage,
			pgm_read_float(voltagePointer[0]),   // X1
			pgm_read_float(voltagePointer[1]),   // X2
			pgm_read_float(phyDataPointer[0]),   // Y1
			pgm_read_float(phyDataPointer[1]) ); // Y2
	}
	// If index is maxed out, phyisical value is larger than our range.
	else if( index==segment_points )
	{
		return map_f( voltage,
			pgm_read_float(voltagePointer[segment_points-2]),   // X1
			pgm_read_float(voltagePointer[segment_points-1]),   // X2
			pgm_read_float(phyDataPointer[segment_points-2]),   // Y1
			pgm_read_float(phyDataPointer[segment_points-1]) ); // Y2
	}
	// index is between 0 and max, just right
	else
	{
		return map_f( voltage,
			pgm_read_float(voltagePointer[index-1]), // X1
			pgm_read_float(voltagePointer[index]),    // X2
			pgm_read_float(phyDataPointer[index-1]), // Y1
			pgm_read_float(phyDataPointer[index]) );  // Y2
	}
}
/* END BLOCK lookup_phyData_from_voltage */

/* BLOCK map_f */
float map_f(float x, float in_min, float in_max, float out_min, float out_max)
{
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/* END BLOCK map_f */

/* BLOCK convertInputsFromRawToPhyData */
void convertInputsFromRawToPhyData()
{
	float voltage = 0.0;

	voltage = sensore_PIN_A0_rawData * (5.0 / 1023.0);
	sensore_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_A0, &(voltage_Temperature_PIN_A0_lookup[0][0]));

	voltage = pot_Potentiometer_Vout_PIN_A1_rawData * (5.0 / 1023.0);
	pot_Potentiometer_Vout_PIN_A1_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_rotazione_PIN_A1, &(voltage_rotazione_PIN_A1_lookup[0][0]));

}
/* END BLOCK convertInputsFromRawToPhyData */