problem including if statement into clock code

I want my code to turn a servo when the serial monitor displays a certain time and I can’t seem to get my code to work…

I would greatly appreciate any help!
I’m really new to Arduino so this may be a pretty simple question.
Thanks so much!

  • Evan
#include <Time.h>
#include <TimeLib.h>
#include <Servo.h>
Servo myservo;

int pos;

void setup()
{
  myservo.attach(9, 500, 2500);
  Serial.begin(9600);
  setTime(9, 0, 1, 29, 12, 20);
}

void loop()
{
  digitalClockDisplay();

  if ( Serial.print(hour()) = 9 ) && (Serial.print(minute()) = 00) && (Serial.print(second()) = 00 ) ) {

    for (pos = 0; pos <= 110; pos += 1) {
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // wait 30 ms for servo to reach the position
      delay(30);
    }

    for (pos = 110; pos >= 0; pos -= 1) {
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // wait 30 ms for servo to reach the position
      delay(30);
    }
  }
}
}

delay(1000);

}

void digitalClockDisplay()
{
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();

}
void printDigits(int digits)
{
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

clock_and_servo.ino (1006 Bytes)

if ( Serial.print(hour()) = 9 ) && (Serial.print(minute()) = 00) && (Serial.print(second()) = 00 ) )

Oops...

Also: the number of curly braces doesn't match!

    }
  }
}
}
delay(1000);
}

ah true the curly brackets were off!

But also I'm not sure what the problem is with the line you mentioned. That was the one I was having problems with but I'm not sure how to fix it...
If you could point me in the right direction that would be fantastic!

Thanks so much!

evanbrorby:
If you could point me in the right direction

You need to use == not = in an if(), thus:

if ( Serial.print(hour()) == 9 )

... not:

if ( Serial.print(hour()) = 9 )

I count 9 ) and 10 (

Serial.print() returns the number of bytes printed, so you are comparing 9 (from your example) to the number of bytes sent to the serial monitor, not to the hour. You need to do the comparison separately from the print:

if (hour == 9) {
  Serial.print...
  Etc
}

That's a good spot....