pleasedontcode

Pulse Rate rev_03

Oct 19th, 2025
1,812
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: Pulse Rate
  13.     - Source Code NOT compiled for: XIAO ESP32S3
  14.     - Source Code created on: 2025-10-19 23:08:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* count pulses in  1 second period , pulses range */
  21.     /* from 10 per second to 100 per second , read out */
  22.     /* put in pulses per second and display on serial */
  23.     /* monitor */
  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. #define PULSE_PIN 34
  36.  
  37. volatile unsigned long pulseCount = 0;
  38. const unsigned long WINDOW_MS = 1000; // 1 second window for PPS calculation
  39. unsigned long windowStartMillis = 0;
  40.  
  41. // Pulse input interrupt service routine
  42. void IRAM_ATTR pulseISR()
  43. {
  44.     // Increment pulse counter on each rising edge detected
  45.     pulseCount++;
  46. }
  47.  
  48. void setup(void)
  49. {
  50.     // put your setup code here, to run once:
  51.     Serial.begin(115200);
  52.     while (!Serial) {
  53.         // wait for serial port to connect. Needed for native USB (ESP32-S3)
  54.         yield();
  55.     }
  56.     // Configure the pulse input pin. Adjust pull mode if your sensor requires a different configuration.
  57.     pinMode(PULSE_PIN, INPUT_PULLUP);
  58.     attachInterrupt(digitalPinToInterrupt(PULSE_PIN), pulseISR, RISING);
  59.     windowStartMillis = millis();
  60.     Serial.println("Pulse counter initialized");
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     // put your main code here, to run repeatedly:
  66.     unsigned long currentMillis = millis();
  67.     if (currentMillis - windowStartMillis >= WINDOW_MS) {
  68.         // Capture the count for the last 1 second window
  69.         noInterrupts();
  70.         unsigned long countThisWindow = pulseCount;
  71.         pulseCount = 0;
  72.         interrupts();
  73.  
  74.         // Display pulses per second on the serial monitor
  75.         Serial.print("Pulses per second: ");
  76.         Serial.println(countThisWindow);
  77.  
  78.         windowStartMillis = currentMillis;
  79.     }
  80.     // Short delay to yield to other tasks and avoid busy-waiting
  81.     delay(1);
  82. }
  83.  
  84. /* END CODE */
  85.  
Advertisement
Add Comment
Please, Sign In to add comment