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: MindWell Breathing
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-09-05 12:03:48
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Generate the full Arduino code and technical */
- /* documentation for my MindWell Wellness Device */
- /* project. About the Device: Name: MindWell */
- /* Wellness Device Core Function: Pocket-sized */
- /* device to pr */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /***** MindWell Wellness Device - Breathing Coach *****/
- /*
- MindWell is a pocket-sized breathing coach for relaxation and mindfulness.
- Core concept: guide the user through a paced breathing session using a PWM LED
- and optional buzzer feedback. Start/stop with a push button. Data over Serial for
- basic monitoring.
- */
- /****** INITIAL SETUP ******/
- // No external libraries required for core functionality on Arduino Uno.
- /****** USER INTERFACE PINS ******/
- const int BUTTON_PIN = 2; // Push button to start/stop sessions (active LOW)
- const int BREATH_LED_PIN = 3; // PWM LED to visualize breathing (inhale/hold/exhale)
- const int BUZZER_PIN = 9; // Optional buzzer for audible cues
- const int INDICATOR_PIN = 13; // Built-in LED to indicate active session
- /****** BREATHING PARAMETERS ******/
- // Breathing cycle: inhale -> hold -> exhale
- const unsigned long INHALE_MS = 4000; // 4 seconds inhale
- const unsigned long HOLD_MS = 4000; // 4 seconds hold
- const unsigned long EXHALE_MS = 6000; // 6 seconds exhale
- /****** GLOBAL STATE ******/
- bool isRunning = false; // true when a session is active
- unsigned long phaseStart = 0; // millis() when current phase started
- enum Phase { INHALE, HOLD, EXHALE } currentPhase = INHALE; // current breathing phase
- unsigned long cycleCount = 0; // number of completed breath cycles
- /****** BUTTON DEBOUNCE ******/
- const int DEBOUNCE_MS = 50;
- int buttonState = HIGH; // current reading from the input pin
- int lastButtonState = HIGH; // previous reading
- unsigned long lastDebounceTime = 0;
- /****** FUNCTION PROTOTYPES ******/
- void setup(void);
- void loop(void);
- void startSession(void);
- void stopSession(void);
- void beep(int times, int durationMs);
- /****** SETUP FUNCTION ******/
- void setup(void)
- {
- // Initialize serial monitor for debugging/tech docs access
- Serial.begin(9600);
- while (!Serial) { /* wait for serial */ }
- Serial.println("MindWell Wellness Device - Breathing Coach");
- // Initialize pins
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(BREATH_LED_PIN, OUTPUT);
- pinMode(BUZZER_PIN, OUTPUT);
- pinMode(INDICATOR_PIN, OUTPUT);
- digitalWrite(INDICATOR_PIN, LOW);
- analogWrite(BREATH_LED_PIN, 0);
- digitalWrite(BUZZER_PIN, LOW);
- // Quiet start
- Serial.println("System ready. Press the button to start a MindWell session.");
- }
- /****** MAIN LOOP ******/
- void loop(void)
- {
- // Read and debounce the button
- int reading = digitalRead(BUTTON_PIN);
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > DEBOUNCE_MS) {
- if (reading != buttonState) {
- buttonState = reading;
- // Button pressed (active LOW)
- if (buttonState == LOW) {
- if (!isRunning) {
- startSession();
- } else {
- stopSession();
- }
- }
- }
- }
- lastButtonState = reading;
- // If a session is active, drive the breathing LED and timing
- if (isRunning) {
- unsigned long now = millis();
- unsigned long phaseElapsed = now - phaseStart;
- switch (currentPhase) {
- case INHALE:
- if (phaseElapsed >= INHALE_MS) {
- // Move to hold
- currentPhase = HOLD;
- phaseStart = now;
- // Ensure LED is fully bright at phase start of HOLD
- analogWrite(BREATH_LED_PIN, 255);
- } else {
- // Ramp LED brightness from 0 to 255 during inhale
- int brightness = map((int)phaseElapsed, 0, (int)INHALE_MS, 0, 255);
- analogWrite(BREATH_LED_PIN, brightness);
- }
- break;
- case HOLD:
- if (phaseElapsed >= HOLD_MS) {
- // Move to exhale
- currentPhase = EXHALE;
- phaseStart = now;
- // Short audible cue before exhale begins
- beep(1, 120);
- } else {
- // Keep LED at max brightness during hold
- analogWrite(BREATH_LED_PIN, 255);
- }
- break;
- case EXHALE:
- if (phaseElapsed >= EXHALE_MS) {
- // Completed one breathing cycle; restart cycle
- cycleCount++;
- currentPhase = INHALE;
- phaseStart = now;
- // Brief cue to indicate cycle completion and restart
- beep(1, 100);
- // Reset LED to start from inhale phase (0 brightness on next inhale)
- analogWrite(BREATH_LED_PIN, 0);
- } else {
- // Ramp LED brightness from 255 down to 0 during exhale
- int brightness = map((int)phaseElapsed, 0, (int)EXHALE_MS, 255, 0);
- analogWrite(BREATH_LED_PIN, brightness);
- }
- break;
- }
- // Optional periodic serial update every second
- static unsigned long lastSerial = 0;
- if ((now - lastSerial) >= 1000) {
- lastSerial = now;
- Serial.print("Cycle: "); Serial.print(cycleCount); Serial.print(" Phase: ");
- switch (currentPhase) {
- case INHALE: Serial.print("INHALE"); break;
- case HOLD: Serial.print("HOLD"); break;
- case EXHALE: Serial.print("EXHALE"); break;
- }
- Serial.print(" | LED: "); Serial.print(analogRead(BREATH_LED_PIN));
- Serial.println();
- }
- }
- // If not running, idle state: keep LED off
- if (!isRunning) {
- analogWrite(BREATH_LED_PIN, 0);
- digitalWrite(INDICATOR_PIN, LOW);
- } else {
- digitalWrite(INDICATOR_PIN, HIGH);
- }
- }
- /****** START/STOP HELPERS ******/
- void startSession(void)
- {
- isRunning = true;
- currentPhase = INHALE;
- phaseStart = millis();
- analogWrite(BREATH_LED_PIN, 0);
- digitalWrite(INDICATOR_PIN, HIGH);
- Serial.println("MindWell session started.");
- }
- void stopSession(void)
- {
- isRunning = false;
- analogWrite(BREATH_LED_PIN, 0);
- digitalWrite(INDICATOR_PIN, LOW);
- Serial.println("MindWell session stopped.");
- }
- void beep(int times, int durationMs)
- {
- for (int i = 0; i < times; i++) {
- digitalWrite(BUZZER_PIN, HIGH);
- delay(durationMs);
- digitalWrite(BUZZER_PIN, LOW);
- delay(durationMs);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment