Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: Pulse Rate
- - Source Code NOT compiled for: XIAO ESP32S3
- - Source Code created on: 2025-10-19 23:08:43
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* count pulses in 1 second period , pulses range */
- /* from 10 per second to 100 per second , read out */
- /* put in pulses per second and display on serial */
- /* monitor */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- #define PULSE_PIN 34
- volatile unsigned long pulseCount = 0;
- const unsigned long WINDOW_MS = 1000; // 1 second window for PPS calculation
- unsigned long windowStartMillis = 0;
- // Pulse input interrupt service routine
- void IRAM_ATTR pulseISR()
- {
- // Increment pulse counter on each rising edge detected
- pulseCount++;
- }
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- while (!Serial) {
- // wait for serial port to connect. Needed for native USB (ESP32-S3)
- yield();
- }
- // Configure the pulse input pin. Adjust pull mode if your sensor requires a different configuration.
- pinMode(PULSE_PIN, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(PULSE_PIN), pulseISR, RISING);
- windowStartMillis = millis();
- Serial.println("Pulse counter initialized");
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- unsigned long currentMillis = millis();
- if (currentMillis - windowStartMillis >= WINDOW_MS) {
- // Capture the count for the last 1 second window
- noInterrupts();
- unsigned long countThisWindow = pulseCount;
- pulseCount = 0;
- interrupts();
- // Display pulses per second on the serial monitor
- Serial.print("Pulses per second: ");
- Serial.println(countThisWindow);
- windowStartMillis = currentMillis;
- }
- // Short delay to yield to other tasks and avoid busy-waiting
- delay(1);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment