Servo bypassing RTC

[code]
#include <RTClib.h>    // RTC Library
#include <Wire.h>      // I2C Library
#define HOUR 19       // Wake-Up Time (hour)
#define MINUTE 42    // Wake-Up Time (minutes) //  i.e. Wake up at 5:40 (24hr clock)
#include <Servo.h>
RTC_DS1307 rtc;
bool wake_up_now = false;
#include <LiquidCrystal.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most board  
int pos=0 ;
int ServoPin=10; // the pin where the servo is connected
int position_target= 0;
int position_last_set= 0;
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
  // put your lcdsetup code here, to run once:
 lcd.setCursor(0,0);
 lcd.begin(16,2);
 Serial.begin(5600);           // Start Serial Monitor
 myservo.attach(ServoPin); // attaches servo on pin 10 to servo object

  if (!rtc.begin())
  {
    Serial.println("Couldn't find RTC"); // Error Message!
    while (1);  
  }
  if (!rtc.isrunning())
  {
    Serial.println("RTC is NOT running!"); // Error Message!
                                           // Sync RTC with the system clock at the time of compilation

                                           rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Uncomment – Compile – Upload)
   
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,0);
DateTime now = rtc.now();
  Serial.print(now.hour(), DEC); // Only For Serial Monitor
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

if ((now.minute() == MINUTE) & (now.hour() == HOUR))

  {
    // Wake-Up Now
    wake_up_now = true;
  }
  else
  {
   
   
  }
  // moves the servo
  if (wake_up_now == true);
{
   for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(120);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 3ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(0);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 3ms for the servo to reach the position
  }
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(120);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 3ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(0);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 3ms for the servo to reach the position

  myservo.write(0); //moves forward
delay (1000); //waits 1s
myservo.write(90); //stops
  }
}}
[/code]

I'm sorry if the code looks horrid but I'm new to using a Arduino and im doing this for a project so most of the code is pasted from the net so basically the problem is that the servo is moving before the RTC is telling it to via a wake up alarm

  if (wake_up_now == true);

I see a semicolon where there should not be one

Also you don't have to compare to true to see if a variable is true, just:

  if (wake_up_now)

is all you need

Thanks but is there anything you can see which could be the reason my servo is moving before the set time?

Why is that? also Thank You

Suggest you add seconds to this.

& is not the same as &&.

if ((now.minute() == MINUTE) & (now.hour() == HOUR) & (now.seconds() ==SECONDS))

So something like this then? also why would you suggest this?

if ((now.minute() == MINUTE) && (now.hour() == HOUR) && (now.seconds() ==SECONDS))


#define HOUR 19
#define MINUTE 42
#define SECONDS 0

Ok just read the arduino page about && Thank you

#define HOUR 19
#define MINUTE 42
#define SECONDS 0

Tell us what you think this is doing ?

      for (pos = 0; pos <= 180; pos += 1)   //goes from 0 degrees to 180 degrees
      {
        //in steps of 1 degree
        myservo.write(120);                 //tell servo to go to position in variable 'pos'
        delay(3);                           //waits 3ms for the servo to reach the position
      }

It goes to 120 degrees? or am i wrong

Then tell us what the difference is with these changes ?

for (pos = 0; pos <= 180; 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(3);                     //waits 3ms for the servo to reach the position
      }

I want it to go down to 120 degrees then back up then down again- Down 2 times and Up 2 times

for (byte x = 0; x < 2; x++)            //2 times
{
  for (pos = 0; pos <= 120; pos += 1)   //goes from 0 degrees to 120 degrees in steps of 1 degree
  {
    myservo.write(pos);                 //tell servo to go to position in variable 'pos'
    delay(3);                           //waits 3ms for the servo to reach the position
  }

  for (pos = 120; pos >= 0; pos -= 1)   //goes from 120 degrees to 0 degrees in steps of 1 degree
  {
    myservo.write(pos);                 //tell servo to go to position in variable 'pos'
    delay(3);                           //waits 3ms for the servo to reach the position
  }
} //END of for (byte x = 0; x < 2; x++)

BTW

Suggest you use this format to do the things needed when the second changes:

  if (lastSECOND != now.second())
  {
    //update to the new second
    lastSECOND = now.second();

   //do the things you need to do when the second changes

}

What does this do? Also where do I place it?

Is this a more compact form of what I did? Also should I replace what my code for the servo with this

There is little use checking 100's of times to see if seconds has changed.

This code checks to see if seconds has changed to the next second; you then can test to see if the alarm time has been reached and operate the servo as needed.

Its not a matter of more compact, which it is.

However, the way you had your code written you were not using the for( ) loop as it is intended.


BTW
Suggest you add these lines to setup( ):
Wire.begin();
rtc.begin();


This is not tested, might need a few tweaks:

//***********************************************************************
void loop()
{
 lcd.setCursor(0, 0);
 DateTime now = rtc.now();

//has seconds changed ?  
if (lastSECOND != now.second())
 {
   //update to the new second
   lastSECOND = now.second();

   Serial.print(now.hour(), DEC);
   Serial.print(':');
   Serial.print(now.minute(), DEC);
   Serial.print(':');
   Serial.print(now.second(), DEC);
   Serial.println();

   //is it time to operate the servo ?
   if ((now.minute() == MINUTE) && (now.hour() == HOUR) && (now.second() == SECOND))
   {
     //*****************
     for (pos = 0; pos <= 120; pos += 1)   //goes from 0 degrees to 120 degrees
     {
       myservo.write(pos);                 //tell servo to go to position in variable 'pos'
       delay(3);                           //waits 3ms for the servo to reach the position
     }

     //*****************
     for (pos = 120; pos >= 0; pos -= 1)   //goes from 120 degrees to 0 degrees
     {
       myservo.write(pos);                 //tell servo to go to position in variable 'pos'
       delay(3);                           //waits 3ms for the servo to reach the position
     }

     //*****************
     for (pos = 0; pos <= 120; pos += 1)   //goes from 0 degrees to 120 degrees
     {
       myservo.write(pos);                 //tell servo to go to position in variable 'pos'
       delay(3);                           //waits 3ms for the servo to reach the position
     }

     //*****************
     for (pos = 120; pos >= 0; pos -= 1)   //goes from 120 degrees to 0 degrees
     {
       myservo.write(pos);                 //tell servo to go to position in variable 'pos'
       delay(3);                           //waits 3ms for the servo to reach the position

     }
     
     //        myservo.write(0);                   //moves forward
     //        delay (1000);                       //waits 1 second
     //        myservo.write(90);                  //stops

   } //END of  if ((now.minute() == MINUTE) && (now.hour() == HOUR) && (now.second() == SECOND))

 } //END of if (lastSECOND != now.second())

} //END of loop()