My end project about a smart house

Sure, here's the corrected version of your text:


So my end project is a smart house and I have my first show tomorrow. I made a project with some friends, ChatGPT, and nice people here but I have a problem.

This is what my house does:

  • Your heart rate increases when you wake up. You can choose a wake-up time, for example, between 7:00 and 12:00. If you have an elevated heart rate during this period (the moment of waking up), you can calmly let an alarm or alert go off. The curtains will automatically open, the room lights will dim, and a cup of coffee will be ready for you. I also want to hang a sensor on the door so the lights turn off when I leave the room.

My code works partially. If you have some time to help me, that would be awesome.

Fix 1 (probably won't take long):
When my BPM is above 155, my lights go on and my buzzer plays music. When I press my button, the sound stops like a snooze button, and my lights stay on (that's good). When my PIR sensor detects movement for the first time, it shuts off the LEDs as it is supposed to. But when I walk in the second time, my code needs to reset to the original state, so if my BPM rises above 155 again, the buzzer plays. However, it won't do it.

Fix 2 (would take a while):
When my BPM rises, a water pump that I connected to a battery and relay needs to be on for 1 second (connected on Pin 11) and then turned off until I walk in a second time like in Fix 1 (the reset).

I also have a DC motor connected to an L293N with (IN1=Pin 12, IN2=Pin 7, IN3=Pin 1) and a potentiometer on pin A1. It just needs to turn for 2 seconds after my BPM rises above 155 BPM (so I can fine-tune it with the potentiometer).

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
#include "pitches.h"

// Pin where the buzzer is connected
const int buzzerPin = 13;
const int PulseWire = 0;  // Use analog pin 0 for PulseSensor
const int stopbutton = 9;
const int PIRPin = 8;  // Pin where the PIR sensor is connected

const int ledPins[] = {6, 2, 3, 4, 5}; // Pins for the 5 LEDs
int Threshold = 550;
PulseSensorPlayground pulseSensor;

// Melody from "Tokyo Drift" (simplified and extended version)
int melody[] = {
  NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5,
  NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5,
  NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5,
  NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5,
  NOTE_D5, NOTE_D5, NOTE_D5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, NOTE_F5,
  NOTE_D5, NOTE_D5, NOTE_D5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, NOTE_F5,
};

// Note durations (in milliseconds)
int noteDurations[] = {
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8
};

bool stopFlag = false;
bool ledsOn = false; // Flag to keep LEDs on
bool pirTriggered = false; // Flag to track PIR detection

void setup() {
  // Initialize buzzer pin as output
  pinMode(buzzerPin, OUTPUT);
  
  // Initialize stop button pin as input
  pinMode(stopbutton, INPUT);

  // Initialize PIR sensor pin as input
  pinMode(PIRPin, INPUT);

  // Initialize LED pins as outputs
  for (int i = 0; i < 5; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off at start
  }

  // Start serial communication
  Serial.begin(9600);

  // Initialize PulseSensor library
  pulseSensor.analogInput(PulseWire);
  pulseSensor.setThreshold(Threshold);

  if (pulseSensor.begin()) {
    Serial.println("PulseSensor object created!");
  }
}

void loop() {
  // Read BPM value
  int myBPM = pulseSensor.getBeatsPerMinute();

  // Print BPM value when a heartbeat is detected
  if (pulseSensor.sawStartOfBeat()) {
    Serial.println("♥  A HeartBeat Happened !");
    Serial.print("BPM: ");
    Serial.println(myBPM);
  }

  // Check if PIR sensor detects motion for the second time
  if (digitalRead(PIRPin) == HIGH && !pirTriggered) {
    pirTriggered = true;
    ledsOn = true; // Turn on LEDs
  }

  // If PIR sensor triggered second time, disable normal operation
  if (pirTriggered) {
    // Turn off LEDs
    for (int i = 0; i < 5; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    
    // Wait a bit before starting loop again
    delay(2000);
    return; // Exit loop to prevent normal operation
  }

  // Normal operation if BPM is above 155 or LEDs are on
  if (myBPM > 155 || ledsOn) {
    // Turn on LEDs
    for (int i = 0; i < 5; i++) {
      digitalWrite(ledPins[i], HIGH);
    }
    
    // Play melody for 2 minutes or until stop button is pressed
    unsigned long startTime = millis();
    stopFlag = false;

    while (millis() - startTime < 120000 && !stopFlag) {
      for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
        if (stopFlag) break;

        // Calculate note duration
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(buzzerPin, melody[thisNote], noteDuration);

        // Wait slightly longer than the note duration
        int pauseBetweenNotes = noteDuration * 1.30;
        unsigned long timer = millis();
        
        while (millis() - timer < pauseBetweenNotes) {
          if (digitalRead(stopbutton) == HIGH) {
            stopFlag = true;
            break;
          }
        }

        // Stop playing the note
        noTone(buzzerPin);

        // Read and print BPM value during melody playback
        myBPM = pulseSensor.getBeatsPerMinute();
        if (pulseSensor.sawStartOfBeat()) {
          Serial.println("♥  A HeartBeat Happened !");
          Serial.print("BPM: ");
          Serial.println(myBPM);
        }

        if (stopFlag) break;
      }
    }
  } else {
    // Turn off LEDs if BPM is not above 155 and stop button is not pressed
    if (!ledsOn) {
      for (int i = 0; i < 5; i++) {
        digitalWrite(ledPins[i], LOW);
      }
    }
  }



  // Wait a bit before starting loop again
  delay(2000);
}

This pin is connected to a motor driver board.

I think this pin needs to be identified as "A0"...

This says "button" is on pin 9 but your drawings show a motor driver board or nothing.

Would you take these images out of your code block?

Image 1:
dsqsqdsq

Image 2:

he sorry potentiometer is on A1 and images are taken out i just put the images is beacause it is how i hound out how to connect them online.

The button is also only connected to the arduino uno.

You must have your drawing match your code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.