Help me to combine these codes and the servo motor to return to its original position

Please check if my code is correct
I need my servo to go back to its original position

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <Servo.h>

RTC_DS3231 rtc;
Servo servo;
const int analogInPin = A1;

const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 4, d7 = 3; // lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int buzz = 13;
int addr=17;
int sensorValue = 0;
int servoPin = 2;
int i = 0;
const int buttonPin2 = 9;
const int buttonPin3 = 8;
const int buttonPin1 = A0;
const int buttonPin4 = 7; // the pin that the pushbutton is attached to
int val2 = 0;
int val3=0;
int pushVal;
int bS1 = 0; // current state of the button
int lBS1 = 0; // previous state of the button

int bS2 = 0; // current state of the button
int lBS2 = 0;

int bS3 = 0; // current state of the button
int lBS3 = 0;

int bS4 = 0; // current state of the button
int lBS4 = 0;

// configure the pins to the right mode
int buzz8amHH = 07; // HH - hours ##Set these for reminder time in 24hr Format
int buzz8amMM = 00; // MM - Minute
int buzz8amSS = 00; // SS - Seconds

int buzz2pmHH = 02; // HH - hours
int buzz2pmMM = 00; // MM - Minute
int buzz2pmSS = 00; // SS - Seconds

int buzz8pmHH = 07; // HH - hours
int buzz8pmMM = 00; // MM - Minute
int buzz8pmSS = 00; // SS - Seconds

int nowHr, nowMin, nowSec;

void gwsMessege(){ // print get well soon messege
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stay Healthy :)"); // Give some cheers
lcd.setCursor(0, 1);
lcd.print("Get Well Soon :)");// wish
}

void helpScreen() { // function to display 1st screen in LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Buttons");
lcd.setCursor(0, 1);
lcd.print("for Reminder...!");
}

void timeScreen() { // function to display Date and time in LCD screen
DateTime now = rtc.now(); // take rtc time and print in display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(6, 0);
lcd.print(nowHr = now.hour(), DEC);
lcd.print(":");
lcd.print(nowMin = now.minute(), DEC);
lcd.print(":");
lcd.print(nowSec = now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
delay(500);
}

void setup() {

servo.attach(servoPin);
Wire.begin();
//rtc.adjust(DateTime(F(DATE), F(TIME))); // uncomment this to set the current time and then comment in next upload when u set the time
rtc.adjust(DateTime(2022, 21, 12, 06, 59, 00)); // manual time set

lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome To Our"); // print a messege at startup
lcd.setCursor(0, 1);
lcd.print("Medicine Reminder");
delay(1000);
gwsMessege();
delay(3000);
helpScreen();
delay(2000);
timeScreen();
delay(3000);
lcd.clear();

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buzz, OUTPUT);
Serial.begin(9600);
}

void ValSet(){
Serial.println(EEPROM.read(addr));
val2 = EEPROM.read(addr); // read previosuly saved value of push button to start from where it was left previously
switch (val2) {
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Once/day !");
pushVal = 1;
delay(500);
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Twice/day !");
pushVal = 2;
delay(500);
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Thrice/day !");
pushVal = 3;
delay(500);
break;
}
}

// the main loop will constantly check to see if the button has been pressed
// if it has, a counter is incremented, and then some action can be taken
void loop()
{ //call to set thrice/day
if (pushVal == 1) { // if push button 1 pressed then remind at 8am
at8am(); //function to start uzzing at 8am
}
else if (pushVal == 2) { // if push button 2 pressed then remind at 8am and 8pm
at8am();
at8pm(); //function to start uzzing at 8mm
}
else if (pushVal == 3) { // if push button 3 pressed then remind at 8am and 8pm
at8am();
at2pm(); //function to start uzzing at 8mm
at8pm();
}
// read the state of the button
bS1 = digitalRead(buttonPin1);
bS2 = digitalRead(buttonPin2);
bS3 = digitalRead(buttonPin3);
bS4 = digitalRead(buttonPin4);

// check to see if it different than the last time we checked

if (bS2 != lBS2) {
// either the button was just pressed or just released
if (bS2 == HIGH) {
// it was just pressed
Serial.println("n02");
EEPROM.write(17,1);
push1();
delay(1000);
}
}
lBS2 = bS2;

if (bS3 != lBS3) {
// either the button was just pressed or just released
if (bS3 == HIGH) {
// it was just pressed
Serial.println("no3");
EEPROM.write(17,2);
push2();
delay(1000);
}
}
lBS3 = bS3;

if (bS4 != lBS4) {
// either the button was just pressed or just released
if (bS4 == HIGH) {
// it was just pressed
Serial.println("no4");
EEPROM.write(17,3);
push3();
delay(1000);
}
}
lBS4 = bS4;

if (bS1 != lBS1) {
// either the button was just pressed or just released
if (bS1 == HIGH) {
val3=EEPROM.read(addr);
EEPROM.write(17,0);
digitalWrite(buzz, LOW);
pinstop();
EEPROM.write(17,val3);
}
}
lBS1 = bS1;

timeScreen();
ValSet();
}
void push1() { // function to set reminder once/day //save the state of push button-1
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Once/day !");
delay(1200);
lcd.clear();
}

void push2() { //function to set reminder twice/day
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Twice/day !");
delay(1200);
lcd.clear();
}

void push3() { //function to set reminder thrice/day
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Thrice/day !");
delay(1200);
lcd.clear();
}

void pinstop(){ //function to stop buzzing when user pushes stop push button
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Take Medicine ");
lcd.setCursor(0, 1);
lcd.print("with Warm Water");
delay(5000);
lcd.clear();
}
void at8am() { // function to start buzzing at 8am
DateTime t = rtc.now();
if (int(t.hour()) == buzz8amHH && int(t.minute()) == buzz8amMM && (int(t.second()) == buzz8amSS || int(t.second()) < buzz8amSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Morning medicines.");
delay(5000);

    /////////////////////////////////////////////////////
  }

}

void at2pm() { // function to start buzzing at 2pm
DateTime t = rtc.now();
if (int(t.hour()) == buzz2pmHH && int(t.minute()) == buzz2pmMM && (int(t.second()) == buzz2pmSS || int(t.second()) < buzz2pmSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Afternoon medicines.");
delay(5000);

    /////////////////////////////////////////////////////
  }

}

void at8pm() { // function to start buzzing at 8pm
DateTime t = rtc.now();
if (int(t.hour()) == buzz8pmHH && int(t.minute()) == buzz8pmMM && (int(t.second()) == buzz8pmSS || int(t.second()) < buzz8pmSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Night medicines.");
delay(5000);

    /////////////////////////////////////////////////////
  }

sensorValue = analogRead(analogInPin);
if(sensorValue < 600){
for (int i = 0; i<=180; i++){
servo.write(i);
delay(10);
}
}
servo.write(i);

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

What should the combined sketch do ?

Please post your best attempt at combining the sketches and explain any problems

Help us help you.

code merging

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

1 Like

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

What does "correct" mean? I cannot possibly read your mind and magically know what you intend for your code to do.

So you want to go back to 90°? That's where the servo moves when you attach the pin.

To go back to 90°:
servo.write(90);

1 Like
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>

RTC_DS3231 rtc;

const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 4, d7 = 3;                 // lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int buzz = 13;
int addr=17;

const int buttonPin2 = 9;
const int buttonPin3 = 8;
const int buttonPin1 = A0;
const int buttonPin4 = 7; 
int val2 = 0;
int val3=0;
int pushVal;
int bS1 = 0;         // current state of the button
int lBS1 = 0;     // previous state of the button


int bS2 = 0;         // current state of the button
int lBS2 = 0;

int bS3 = 0;         // current state of the button
int lBS3 = 0;

int bS4 = 0;         // current state of the button
int lBS4 = 0;

// configure the pins to the right mode
int buzz8amHH = 08;          //    HH - hours         ##Set these for reminder time in 24hr Format 
int buzz8amMM = 00;          //    MM - Minute
int buzz8amSS = 00;          //    SS - Seconds

int buzz2pmHH = 02;          //    HH - hours
int buzz2pmMM = 00;          //    MM - Minute
int buzz2pmSS = 00;          //    SS - Seconds

int buzz8pmHH = 09;          //    HH - hours
int buzz8pmMM = 00;          //    MM - Minute
int buzz8pmSS = 00;          //    SS - Seconds

 

int nowHr, nowMin, nowSec; 

void gwsMessege(){               // print get well soon messege
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Stay Healthy :)");     // Give some cheers
    lcd.setCursor(0, 1);
    lcd.print("Get Well Soon :)");// wish 
}

void helpScreen() {              // function to display 1st screen in LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Press Buttons");
    lcd.setCursor(0, 1);
    lcd.print("for Reminder...!");
 }

void timeScreen() {              // function to display Date and time in LCD screen
  DateTime now = rtc.now();             // take rtc time and print in display
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time:");
    lcd.setCursor(6, 0);
    lcd.print(nowHr = now.hour(), DEC);
    lcd.print(":");
    lcd.print(nowMin = now.minute(), DEC);
    lcd.print(":");
    lcd.print(nowSec = now.second(), DEC);
    lcd.setCursor(0, 1);
    lcd.print("Date: ");
    lcd.print(now.day(), DEC);
    lcd.print("/");
    lcd.print(now.month(), DEC);
    lcd.print("/");
    lcd.print(now.year(), DEC);
    delay(500);
}

void setup()
{
Wire.begin();
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));            // uncomment this to set the current time and then comment in next upload when u set the time
  //rtc.adjust(DateTime(2019, 10, 12, 15, 50, 30));                // manual time set

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome To Our");                                      // print a messege at startup
  lcd.setCursor(0, 1);
  lcd.print("Medicine Reminder");
  delay(1000);
  gwsMessege();
  delay(3000);
  helpScreen();
  delay(2000);
  timeScreen();
  delay(3000);
  lcd.clear();
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buzz, OUTPUT);
  Serial.begin(9600);
}

void ValSet(){
  Serial.println(EEPROM.read(addr));
  val2 = EEPROM.read(addr);                         // read previosuly saved value of push button to start from where it was left previously
  switch (val2) {
    case 1:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Once/day !");
      pushVal = 1;
      delay(500);
      break;
    case 2:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Twice/day !");
      pushVal = 2;
      delay(500);
      break;
    case 3:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Thrice/day !");
      pushVal = 3;
      delay(500);
      break;
  }
}

// the main loop will constantly check to see if the button has been pressed
// if it has, a counter is incremented, and then some action can be taken
void loop() 
{                                         //call to set thrice/day 
    if (pushVal == 1) {                                // if push button 1 pressed then remind at 8am
    at8am();                                           //function to start uzzing at 8am 
  }
  else if (pushVal == 2) {                             // if push button 2 pressed then remind at 8am and 8pm
    at8am();                                            
    at8pm();                                           //function to start uzzing at 8mm
  }
  else if (pushVal == 3) {                             // if push button 3 pressed then remind at 8am and 8pm
    at8am();
    at2pm();                                            //function to start uzzing at 8mm
    at8pm();
  }
  // read the state of the button
  bS1 = digitalRead(buttonPin1);
  bS2 = digitalRead(buttonPin2);
  bS3 = digitalRead(buttonPin3);
  bS4 = digitalRead(buttonPin4);

  // check to see if it different than the last time we checked
  

if (bS2 != lBS2) {
    // either the button was just pressed or just released
    if (bS2 == HIGH) {
      // it was just pressed
      Serial.println("n02");
      EEPROM.write(17,1);
      push1();
      delay(1000);
    }
  }
  lBS2 = bS2;


if (bS3 != lBS3) {
    // either the button was just pressed or just released
    if (bS3 == HIGH) {
      // it was just pressed
      Serial.println("no3");
      EEPROM.write(17,2);
      push2();
      delay(1000);
    }
  }
  lBS3 = bS3;


if (bS4 != lBS4) {
    // either the button was just pressed or just released
    if (bS4 == HIGH) {
      // it was just pressed
      Serial.println("no4");
      EEPROM.write(17,3);
      push3();
      delay(1000);
    }
  }
  lBS4 = bS4;
  
if (bS1 != lBS1) {
    // either the button was just pressed or just released
    if (bS1 == HIGH) {
      val3=EEPROM.read(addr);
      EEPROM.write(17,0);
      digitalWrite(buzz, LOW);
      pinstop();
      EEPROM.write(17,val3);
    }
  }
  lBS1 = bS1;

  timeScreen();
  ValSet();
}
void push1() {                   // function to set reminder once/day                                           //save the state of push button-1 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Once/day !");
    delay(1200);
    lcd.clear();
  }

void push2() {                      //function to set reminder twice/day
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Twice/day !");
    delay(1200);
    lcd.clear();
  }

void push3() {                    //function to set reminder thrice/day
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Thrice/day !");
    delay(1200);
    lcd.clear();
  }

void pinstop(){                   //function to stop buzzing when user pushes stop push button
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Take Medicine  ");
    lcd.setCursor(0, 1);
    lcd.print("with Warm Water");
    delay(5000);
    lcd.clear();
  }
void at8am() {                      // function to start buzzing at 8am
  DateTime t = rtc.now();
  if (int(t.hour()) == buzz8amHH && int(t.minute()) == buzz8amMM  && (int(t.second()) == buzz8amSS || int(t.second()) < buzz8amSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Morning medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

void at2pm() {                          // function to start buzzing at 2pm
  DateTime t = rtc.now();
  if (int(t.hour()) == buzz2pmHH && int(t.minute()) == buzz2pmMM  && (int(t.second()) == buzz2pmSS || int(t.second()) < buzz2pmSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Afternoon medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

void at8pm() {                           // function to start buzzing at 8pm
DateTime t = rtc.now();
  if (int(t.hour()) == buzz8pmHH && int(t.minute()) == buzz8pmMM  && (int(t.second()) == buzz8pmSS || int(t.second()) < buzz8pmSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Night medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

Please help me add a servo motor and an IR sensor code for dispensing machine

Also posted for a second time in the wrong category. Flagged for moderation.

I have merged your posts due to them having too much overlap on the same subject matter @darwin2k.

In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.

The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Thanks in advance for your cooperation.

The codes have no errors it's just that the servo motor and sensor doesn't function
Sometimes it functions but the it doesn't go back to original position of the servo motor

... in what code, does it behave like that? The latest code above, seems like just a request to help merge. Not a complete first test sketch.

The code you have posted in post #8
contains zero lines of code that would activate or control a servo
and it
contains zero lines of code that would do anything with any kind of sensor

So a lot of information is missing.

If a new project starts:
Even the most advanced software-developper-expert with 40 years programming experience will ask his customer:

"what functionality do you want to have?" please describe it in normal words

As you are a beginner it is 100 times more important to give a normal worded description of the wanted functionality. As a beginner it might be that you have some misconceptions about how code works. If the misconception is small it is hard to detect this misconception.
This is the reason why it is very important to describe the wanted functionality in normal words.

You can finish your project in two ways:
way 1: taking looong almost unlimited time

Way 2: taking a limited time.

Way 2 is to take a limited time of maybe 60 minutes to carefully think over how can I describe the wanted functionality in normal words? Starting to write. Reading it again, delete some parts write it new, add more specific words etc.

Way 1
shooting quick in the dark with a very short posting which then invokes asking back
the asking back is after some time maybe 10 minutes maybe 2 hours or 5 hours later

shooting quick in the dark with a very short posting which then invokes asking back
the asking back is after some time maybe 10 minutes maybe 2 hours or 5 hours later
Shooting Quick In the Dark Asking Back
I will use the abbreviation "sqidab" for better making visible what will gonna happen

sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...sqidab...
and after weeks still no finished project.

If sqidab-ing is your favorite pleasure go ahead whirling in circles without moving forward.
If you would like to finish your project go and take the limited time to read 20 minutes

and then write down and RE-editing a decent normal worded description of your project

best regards Stefan

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