pleasedontcode

Potentiometer Oscillator rev_04

Oct 20th, 2025
3,479
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Potentiometer Oscillator
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-10-20 22:32:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* create a pulse generator of one millisecond */
  21.     /* duration with a variable frequency of 10Hz to */
  22.     /* 100Hz , adjustable via the potentiometer */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF ANALOG INPUT PINS *****/
  35. const uint8_t frequency_adjust_Potentiometer_Vout_PIN_D4 = D4;
  36.  
  37. // Pulse output pin
  38. const uint8_t pulse_output_PIN = 25; // Output pulse
  39.  
  40. void setup(void)
  41. {
  42.     // put your setup code here, to run once:
  43.  
  44.     pinMode(frequency_adjust_Potentiometer_Vout_PIN_D4, INPUT);
  45.     pinMode(pulse_output_PIN, OUTPUT);
  46.     digitalWrite(pulse_output_PIN, LOW);
  47.  
  48.     // Optional: initialize ADC characteristics if supported
  49.     // (commented out to preserve compatibility across boards)
  50.     // analogReadResolution(12);
  51. }
  52.  
  53.  
  54. void loop(void)
  55. {
  56.     // put your main code here, to run repeatedly:
  57.  
  58.     // Read potentiometer and map to frequency range [10,100] Hz
  59.     int potValue = analogRead(frequency_adjust_Potentiometer_Vout_PIN_D4);
  60.     const int MIN_FREQ = 10;
  61.     const int MAX_FREQ = 100;
  62.     int freqHz = map(potValue, 0, 4095, MIN_FREQ, MAX_FREQ);
  63.     if (freqHz < MIN_FREQ) freqHz = MIN_FREQ;
  64.     if (freqHz > MAX_FREQ) freqHz = MAX_FREQ;
  65.  
  66.     // Period in microseconds for the selected frequency
  67.     static int currentFreqHz = -1;
  68.     static unsigned long period_us = 0;
  69.     static unsigned long lastChangeTime = 0;
  70.     static bool pulseHigh = false;
  71.  
  72.     // Sync frequency state on first run or when changed
  73.     if (currentFreqHz != freqHz) {
  74.         currentFreqHz = freqHz;
  75.         period_us = 1000000UL / (unsigned long)freqHz; // total period
  76.         // Reset timing to align with new frequency
  77.         lastChangeTime = micros();
  78.         // Start with a HIGH pulse
  79.         digitalWrite(pulse_output_PIN, HIGH);
  80.         pulseHigh = true;
  81.         return;
  82.     }
  83.  
  84.     const unsigned long PULSE_WIDTH_US = 1000; // 1 ms HIGH pulse
  85.  
  86.     unsigned long now = micros();
  87.  
  88.     if (pulseHigh) {
  89.         // Keep high for 1 ms
  90.         if (now - lastChangeTime >= PULSE_WIDTH_US) {
  91.             digitalWrite(pulse_output_PIN, LOW);
  92.             lastChangeTime = now;
  93.             pulseHigh = false;
  94.         }
  95.     } else {
  96.         // Low phase: rest of the period
  97.         unsigned long lowDuration = period_us > PULSE_WIDTH_US ? (period_us - PULSE_WIDTH_US) : 0;
  98.         if (lowDuration > 0 && (now - lastChangeTime >= lowDuration)) {
  99.             digitalWrite(pulse_output_PIN, HIGH);
  100.             lastChangeTime = now;
  101.             pulseHigh = true;
  102.         }
  103.     }
  104. }
  105.  
  106. /* END CODE */
  107.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment