Hi everyone, Ive been using this code for my project I am running the servo with 2 modes the adult and the child and then I have to incorporate the Heart Rate monitoring, I have below the codes. I set the heart rate as a function the same I did with 2 modes of servo. Now the problem is, when I call the Heart(); after each mode (Child(); and Adult() it is requiring me to input a heart rate in the sensor and then after that the motor will work. this is not the intended output.
the output should be:
the motor will run in the each mode selected and then the heart rate will have nothing to do with its running. If I input for heart, the motor will run on what is selected, if i didnt input for heart the motor will still run on what is selected.
thank you!
#include <Servo.h>
#include <dht.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
long randNumber;
Servo myservo;Â // create servo object to control a servo
        // a maximum of eight servo objects can be created
int pos = 0;Â Â // variable to store the servo position
int buttonPin = 3;
int buttonPin2 = 4;
int buttonPin3 = 5;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
String string;
char command;
dht DHT;
int Pulse = A0;
#define DHT11_PIN 7
void setup(){
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buttonPin2,INPUT_PULLUP);
 pinMode(buttonPin3,INPUT_PULLUP);
Â
 randomSeed(analogRead(3));
 Serial.begin(9600); // Used to type in characters
 myservo.attach(9); // attaches the servo on pin 9 to the servo object
 lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
 for(int i = 0; i< 3; i++)
 {
  lcd.backlight();
  delay(200);
  lcd.noBacklight();
  delay(200);
}
lcd.backlight();
}
void Child()
{
Â
 lcd.setCursor(0,3); //Start at character 4 on line 0
 Serial.println("Flow:200-300mL/Pump");
 lcd.print("Flow:200-300mL/Pump");
 delay(15);
 for(pos = 0; pos < 120; pos += 1) // goes from 0 degrees to 180 degrees
 {                 // in steps of 1 degree
  myservo.write(pos);       // tell servo to go to position in variable 'pos'
  delay(13);           // waits 15ms for the servo to reach the position
 }
 for(pos = 120; pos>=0; pos-=1)  // goes from 180 degrees to 0 degrees
 {               Â
  myservo.write(pos);       // tell servo to go to position in variable 'pos'
  delay(13);           // waits 15ms for the servo to reach the position
 }
 int chk = DHT.read11(DHT11_PIN);
 lcd.setCursor(0,1);
 // Serial.print("Temp: ");
 Serial.println("16-20 breaths/min");
 lcd.print("16-20 breaths/min");
 delay(15);
 lcd.setCursor(0,2);
 Serial.println("Humidity: ");
 Serial.print(DHT.humidity);
 lcd.print("Humidity: ");
 lcd.print(DHT.humidity);
 lcd.print("%");
 delay(15);
}
void Adult()
{
Â
 lcd.setCursor(0,3);
 Serial.println("Flow:400-600mL/Pump");Â
 lcd.print("Flow:400-600mL/Pump");
 delay(15);
 for(pos = 0; pos < 120; pos += 1) // goes from 0 degrees to 180 degrees
 {                 // in steps of 1 degree
  myservo.write(pos);       // tell servo to go to position in variable 'pos'
  delay(10);           // waits 15ms for the servo to reach the position
 }
 for(pos = 120; pos>=0; pos-=1)  // goes from 180 degrees to 0 degrees
 {               Â
  myservo.write(pos);       // tell servo to go to position in variable 'pos'
  delay(10);           // waits 15ms for the servo to reach the position
 }
int chk = DHT.read11(DHT11_PIN);
 lcd.setCursor(0,1);
 // Serial.print("Temp: ");
 Serial.println("16-20 breaths/min");
 lcd.print("10-12 breaths/min");
 delay(15);
 lcd.setCursor(0,2);
 Serial.println("Humidity: ");
 Serial.print(DHT.humidity);
 lcd.print("Humidity: ");
 lcd.print(DHT.humidity);
 lcd.print("%");
 delay(15);
}
void Heart()
{
 while (digitalRead(Pulse) == HIGH);
 while (digitalRead(Pulse) == LOW);
 int T1 = millis();
 while (digitalRead(Pulse) == HIGH);
 while (digitalRead(Pulse) == LOW);
 int T2 = millis();
 int Time = T2-T1;
 unsigned long HeartRate = 60000L;
 HeartRate = HeartRate/Time;
 lcd.setCursor(0,0);
 Serial.print("BPM : ");
 Serial.println(HeartRate);
 lcd.print("BPM : ");
 lcd.print(HeartRate);
 if (HeartRate < 10) lcd.print(" "); // 0-9 add one space
 if (HeartRate < 100) lcd.print(" "); // 0-99 add one space
 if (HeartRate < 1000) lcd.print(" "); // 0-999 add one space
 millis();
Â
Â
}
void loop()
{
 buttonState = digitalRead(buttonPin);
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
if (Serial.available() > 0)
 {string = "";} Â
 while(Serial.available() > 0)
  {
  command = ((byte)Serial.read());
  if(command == ':')
  {
  break;
  }
  else
   {
    string += command;
  }
   delay(1);
 }
Â
 if (buttonState==HIGH || string=="c"){
 Child();
 Heart();
Â
 randNumber = random(80, 99);
 lcd.setCursor(0,0);
 //Serial.print("BPM : ");
 //Serial.println(randNumber);
 //lcd.print("BPM : ");
 //lcd.print(randNumber);
 delay(1000); Â
 }
Â
 if (buttonState2==HIGH||string=="a"){
 Adult();
 Heart();
Â
 randNumber = random(80, 99);
 lcd.setCursor(0,0);
 //Serial.print("BPM : ");
 //Serial.println(randNumber);
 //lcd.print("BPM : ");
 //lcd.print(randNumber);
 delay(1000);
 }
Â
 if (string=="off"){
 myservo.detach();
 if (string=="on"){
 }
 Â
 }
Â
Â
}