pleasedontcode

ESP32 Pulses rev_01

Oct 19th, 2025
1,254
0
Never
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: ESP32 Pulses
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-10-19 08:03:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* provide five pulse outputs simultaneously of one */
  21.     /* millisecond  at individual frequencies . The */
  22.     /* frequencies required are as follows : 10 Hz , 25 */
  23.     /* Hz , 50 Hz , 75 Hz , 100 Hz . */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // ESP32 Five-Channel Pulse Generator
  36. // Generates five simultaneous 1 ms pulses at frequencies:
  37. // 10 Hz, 25 Hz, 50 Hz, 75 Hz, 100 Hz on five separate GPIO pins.
  38.  
  39. // Pin assignments for ESP32 DevKit V1 (adjust if needed):
  40. const uint8_t PULSE_PINS[5] = {16, 17, 18, 19, 21};
  41.  
  42. // Target frequencies in Hz
  43. const uint32_t PULSE_FREQS[5] = {10, 25, 50, 75, 100};
  44.  
  45. // Internal channel representation
  46. typedef struct {
  47.     uint8_t pin;          // GPIO pin for the channel
  48.     uint64_t period_us;     // Pulse period in microseconds
  49.     uint64_t next_time;     // Next scheduled pulse start time (microseconds)
  50.     bool in_pulse;          // Is the 1 ms pulse currently high?
  51.     uint64_t pulse_end;     // Time when current 1 ms pulse ends
  52. } PulseChannel;
  53.  
  54. PulseChannel channels[5];
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize serial for debugging (optional)
  59.     // Serial.begin(115200);
  60.  
  61.     // Configure pins as outputs and reset state
  62.     for (int i = 0; i < 5; i++) {
  63.         pinMode(PULSE_PINS[i], OUTPUT);
  64.         digitalWrite(PULSE_PINS[i], LOW);
  65.     }
  66.  
  67.     // Initialize channel parameters
  68.     uint64_t now = (uint64_t)micros();
  69.     for (int i = 0; i < 5; i++) {
  70.         channels[i].pin = PULSE_PINS[i];
  71.         // Use integer division for period; fractional remainder ignored per cycle
  72.         channels[i].period_us = (uint64_t)(1000000ull / PULSE_FREQS[i]);
  73.         channels[i].next_time = now;  // start pulses immediately
  74.         channels[i].in_pulse = false;
  75.         channels[i].pulse_end = 0;
  76.     }
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     uint64_t now = (uint64_t)micros();
  82.  
  83.     for (int i = 0; i < 5; i++) {
  84.         // If not currently pulsing and it's time for the next pulse, start a 1 ms pulse
  85.         if (!channels[i].in_pulse && now >= channels[i].next_time) {
  86.             digitalWrite(channels[i].pin, HIGH);
  87.             channels[i].in_pulse = true;
  88.             channels[i].pulse_end = now + 1000ull; // 1 ms pulse width
  89.         }
  90.         // If currently pulsing and the 1 ms width has elapsed, end pulse and schedule next
  91.         else if (channels[i].in_pulse && now >= channels[i].pulse_end) {
  92.             digitalWrite(channels[i].pin, LOW);
  93.             channels[i].in_pulse = false;
  94.             // Schedule next pulse start time based on period
  95.             channels[i].next_time += channels[i].period_us;
  96.             // If we somehow fell behind, realign to now
  97.             if (channels[i].next_time < now) {
  98.                 channels[i].next_time = now + channels[i].period_us;
  99.             }
  100.         }
  101.     }
  102.  
  103.     // Small delay to prevent excessive CPU usage; keeps loop responsive
  104.     delayMicroseconds(50);
  105. }
  106.  
  107. /* END CODE */
  108.  
Advertisement
Add Comment
Please, Sign In to add comment