pleasedontcode

MindWell Breathing rev_01

Sep 5th, 2025
330
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: MindWell Breathing
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-09-05 12:03:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Generate the full Arduino code and technical */
  21.     /* documentation for my MindWell Wellness Device */
  22.     /* project.    About the Device:    Name: MindWell */
  23.     /* Wellness Device    Core Function: Pocket-sized */
  24.     /* device to pr */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /***** MindWell Wellness Device - Breathing Coach *****/
  31. /*
  32.    MindWell is a pocket-sized breathing coach for relaxation and mindfulness.
  33.    Core concept: guide the user through a paced breathing session using a PWM LED
  34.    and optional buzzer feedback. Start/stop with a push button. Data over Serial for
  35.    basic monitoring.
  36. */
  37.  
  38. /****** INITIAL SETUP ******/
  39. // No external libraries required for core functionality on Arduino Uno.
  40.  
  41. /****** USER INTERFACE PINS ******/
  42. const int BUTTON_PIN       = 2;  // Push button to start/stop sessions (active LOW)
  43. const int BREATH_LED_PIN   = 3;  // PWM LED to visualize breathing (inhale/hold/exhale)
  44. const int BUZZER_PIN       = 9;  // Optional buzzer for audible cues
  45. const int INDICATOR_PIN    = 13; // Built-in LED to indicate active session
  46.  
  47. /****** BREATHING PARAMETERS ******/
  48. // Breathing cycle: inhale -> hold -> exhale
  49. const unsigned long INHALE_MS  = 4000; // 4 seconds inhale
  50. const unsigned long HOLD_MS    = 4000; // 4 seconds hold
  51. const unsigned long EXHALE_MS  = 6000; // 6 seconds exhale
  52.  
  53. /****** GLOBAL STATE ******/
  54. bool isRunning = false;                 // true when a session is active
  55. unsigned long phaseStart = 0;               // millis() when current phase started
  56. enum Phase { INHALE, HOLD, EXHALE } currentPhase = INHALE; // current breathing phase
  57. unsigned long cycleCount = 0;               // number of completed breath cycles
  58.  
  59. /****** BUTTON DEBOUNCE ******/
  60. const int DEBOUNCE_MS = 50;
  61. int buttonState = HIGH;        // current reading from the input pin
  62. int lastButtonState = HIGH;    // previous reading
  63. unsigned long lastDebounceTime = 0;
  64.  
  65. /****** FUNCTION PROTOTYPES ******/
  66. void setup(void);
  67. void loop(void);
  68.  
  69. void startSession(void);
  70. void stopSession(void);
  71. void beep(int times, int durationMs);
  72.  
  73. /****** SETUP FUNCTION ******/
  74. void setup(void)
  75. {
  76.     // Initialize serial monitor for debugging/tech docs access
  77.     Serial.begin(9600);
  78.     while (!Serial) { /* wait for serial */ }
  79.     Serial.println("MindWell Wellness Device - Breathing Coach");
  80.  
  81.     // Initialize pins
  82.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  83.     pinMode(BREATH_LED_PIN, OUTPUT);
  84.     pinMode(BUZZER_PIN, OUTPUT);
  85.     pinMode(INDICATOR_PIN, OUTPUT);
  86.     digitalWrite(INDICATOR_PIN, LOW);
  87.     analogWrite(BREATH_LED_PIN, 0);
  88.     digitalWrite(BUZZER_PIN, LOW);
  89.  
  90.     // Quiet start
  91.     Serial.println("System ready. Press the button to start a MindWell session.");
  92. }
  93.  
  94. /****** MAIN LOOP ******/
  95. void loop(void)
  96. {
  97.     // Read and debounce the button
  98.     int reading = digitalRead(BUTTON_PIN);
  99.     if (reading != lastButtonState) {
  100.         lastDebounceTime = millis();
  101.     }
  102.     if ((millis() - lastDebounceTime) > DEBOUNCE_MS) {
  103.         if (reading != buttonState) {
  104.             buttonState = reading;
  105.             // Button pressed (active LOW)
  106.             if (buttonState == LOW) {
  107.                 if (!isRunning) {
  108.                     startSession();
  109.                 } else {
  110.                     stopSession();
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     lastButtonState = reading;
  116.  
  117.     // If a session is active, drive the breathing LED and timing
  118.     if (isRunning) {
  119.         unsigned long now = millis();
  120.         unsigned long phaseElapsed = now - phaseStart;
  121.  
  122.         switch (currentPhase) {
  123.             case INHALE:
  124.                 if (phaseElapsed >= INHALE_MS) {
  125.                     // Move to hold
  126.                     currentPhase = HOLD;
  127.                     phaseStart = now;
  128.                     // Ensure LED is fully bright at phase start of HOLD
  129.                     analogWrite(BREATH_LED_PIN, 255);
  130.                 } else {
  131.                     // Ramp LED brightness from 0 to 255 during inhale
  132.                     int brightness = map((int)phaseElapsed, 0, (int)INHALE_MS, 0, 255);
  133.                     analogWrite(BREATH_LED_PIN, brightness);
  134.                 }
  135.                 break;
  136.  
  137.             case HOLD:
  138.                 if (phaseElapsed >= HOLD_MS) {
  139.                     // Move to exhale
  140.                     currentPhase = EXHALE;
  141.                     phaseStart = now;
  142.                     // Short audible cue before exhale begins
  143.                     beep(1, 120);
  144.                 } else {
  145.                     // Keep LED at max brightness during hold
  146.                     analogWrite(BREATH_LED_PIN, 255);
  147.                 }
  148.                 break;
  149.  
  150.             case EXHALE:
  151.                 if (phaseElapsed >= EXHALE_MS) {
  152.                     // Completed one breathing cycle; restart cycle
  153.                     cycleCount++;
  154.                     currentPhase = INHALE;
  155.                     phaseStart = now;
  156.                     // Brief cue to indicate cycle completion and restart
  157.                     beep(1, 100);
  158.                     // Reset LED to start from inhale phase (0 brightness on next inhale)
  159.                     analogWrite(BREATH_LED_PIN, 0);
  160.                 } else {
  161.                     // Ramp LED brightness from 255 down to 0 during exhale
  162.                     int brightness = map((int)phaseElapsed, 0, (int)EXHALE_MS, 255, 0);
  163.                     analogWrite(BREATH_LED_PIN, brightness);
  164.                 }
  165.                 break;
  166.         }
  167.  
  168.         // Optional periodic serial update every second
  169.         static unsigned long lastSerial = 0;
  170.         if ((now - lastSerial) >= 1000) {
  171.             lastSerial = now;
  172.             Serial.print("Cycle: "); Serial.print(cycleCount); Serial.print(" Phase: ");
  173.             switch (currentPhase) {
  174.                 case INHALE: Serial.print("INHALE"); break;
  175.                 case HOLD: Serial.print("HOLD"); break;
  176.                 case EXHALE: Serial.print("EXHALE"); break;
  177.             }
  178.             Serial.print(" | LED: "); Serial.print(analogRead(BREATH_LED_PIN));
  179.             Serial.println();
  180.         }
  181.     }
  182.     // If not running, idle state: keep LED off
  183.     if (!isRunning) {
  184.         analogWrite(BREATH_LED_PIN, 0);
  185.         digitalWrite(INDICATOR_PIN, LOW);
  186.     } else {
  187.         digitalWrite(INDICATOR_PIN, HIGH);
  188.     }
  189. }
  190.  
  191. /****** START/STOP HELPERS ******/
  192. void startSession(void)
  193. {
  194.     isRunning = true;
  195.     currentPhase = INHALE;
  196.     phaseStart = millis();
  197.     analogWrite(BREATH_LED_PIN, 0);
  198.     digitalWrite(INDICATOR_PIN, HIGH);
  199.     Serial.println("MindWell session started.");
  200. }
  201.  
  202. void stopSession(void)
  203. {
  204.     isRunning = false;
  205.     analogWrite(BREATH_LED_PIN, 0);
  206.     digitalWrite(INDICATOR_PIN, LOW);
  207.     Serial.println("MindWell session stopped.");
  208. }
  209.  
  210. void beep(int times, int durationMs)
  211. {
  212.     for (int i = 0; i < times; i++) {
  213.         digitalWrite(BUZZER_PIN, HIGH);
  214.         delay(durationMs);
  215.         digitalWrite(BUZZER_PIN, LOW);
  216.         delay(durationMs);
  217.     }
  218. }
  219.  
  220. /* END CODE */
  221.  
Advertisement
Add Comment
Please, Sign In to add comment