pleasedontcode

Simultaneous Pulses rev_02

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