/********* 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: Potentiometer Oscillator
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2025-10-20 22:32:12

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* create a pulse generator of one millisecond */
	/* duration with a variable frequency of 10Hz to */
	/* 100Hz , adjustable via the potentiometer */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** DEFINITION OF LIBRARIES *****/

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

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t frequency_adjust_Potentiometer_Vout_PIN_D4 = D4;

// Pulse output pin
const uint8_t pulse_output_PIN = 25; // Output pulse

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

	pinMode(frequency_adjust_Potentiometer_Vout_PIN_D4, INPUT);
	pinMode(pulse_output_PIN, OUTPUT);
	digitalWrite(pulse_output_PIN, LOW);

	// Optional: initialize ADC characteristics if supported
	// (commented out to preserve compatibility across boards)
	// analogReadResolution(12);
}


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

	// Read potentiometer and map to frequency range [10,100] Hz
	int potValue = analogRead(frequency_adjust_Potentiometer_Vout_PIN_D4);
	const int MIN_FREQ = 10;
	const int MAX_FREQ = 100;
	int freqHz = map(potValue, 0, 4095, MIN_FREQ, MAX_FREQ);
	if (freqHz < MIN_FREQ) freqHz = MIN_FREQ;
	if (freqHz > MAX_FREQ) freqHz = MAX_FREQ;

	// Period in microseconds for the selected frequency
	static int currentFreqHz = -1;
	static unsigned long period_us = 0;
	static unsigned long lastChangeTime = 0;
	static bool pulseHigh = false;

	// Sync frequency state on first run or when changed
	if (currentFreqHz != freqHz) {
		currentFreqHz = freqHz;
		period_us = 1000000UL / (unsigned long)freqHz; // total period
		// Reset timing to align with new frequency
		lastChangeTime = micros();
		// Start with a HIGH pulse
		digitalWrite(pulse_output_PIN, HIGH);
		pulseHigh = true;
		return;
	}

	const unsigned long PULSE_WIDTH_US = 1000; // 1 ms HIGH pulse

	unsigned long now = micros();

	if (pulseHigh) {
		// Keep high for 1 ms
		if (now - lastChangeTime >= PULSE_WIDTH_US) {
			digitalWrite(pulse_output_PIN, LOW);
			lastChangeTime = now;
			pulseHigh = false;
		}
	} else {
		// Low phase: rest of the period
		unsigned long lowDuration = period_us > PULSE_WIDTH_US ? (period_us - PULSE_WIDTH_US) : 0;
		if (lowDuration > 0 && (now - lastChangeTime >= lowDuration)) {
			digitalWrite(pulse_output_PIN, HIGH);
			lastChangeTime = now;
			pulseHigh = true;
		}
	}
}

/* END CODE */
