-4
#include <DS1302.h>
#include <Servo.h>
Time t;
Servo myservo;
int Hour;
int Min;
int Sec;
// Init the DS1302
DS1302 rtc(2, 3, 4);

void setup()
{
myservo.attach(9); 
myservo.write(45); 
// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);
Serial.begin(9200);
// The following lines can be commented out to use the values already stored in the DS1302
//rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(8, 59, 50); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(8, 25, 2019); // Set the date to August 25th, 2019
}

void loop()
{
t = rtc.getTime();
Hour = t.hour;
Min = t.min;
Sec = t.sec;
 Serial.print(Hour);
 Serial.print(":");
 Serial.print(Min);
 Serial.print(":");
 Serial.println(Sec);

//set the time for fish feeding 
if ((Hour== 2 && Min== 0 && Sec== 2)||(Hour== 21 && Min== 0 && Sec== 2)) {
myservo.write(0);
delay (300);
myservo.write(45);
delay (300);}
}
12
  • did the intermediate code work?
    – jsotola
    Sep 13 '20 at 4:48
  • 1
    Nope, nothing seems to work hahaha Sep 13 '20 at 5:14
  • 2
    You need to define "doesn't work". What output are you expecting? What output do you get? What happens? It's like going to a doctor and saying you feel sick. S/he needs more details than that.
    – Nick Gammon
    Sep 13 '20 at 6:08
  • 1
    Serial.begin(9200) ? and depending on the library, the rtc.get time may get stuck in a bad connection to the rtc chip. Sep 13 '20 at 6:42
  • 1
    @st2000 I think that the only thing I didn't cover so far is if the correct pins are being used. How do I check that? Sep 13 '20 at 14:16
1

Explanation about the multiple time execution

if ((Hour== 9 && Min== 14)||(Hour== 21 && Min== 0)) {
  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(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 }
  1. Assume the time is 9:14:00 (9 hours, 14 minutes, 00 seconds).. the if condition passes.

  2. The for loop will get executed. The duration needed to complete the loop is roughly 5 seconds.

  3. Later, it will again go to the if condition. The time will be now 9:14:06.. it means, the condition will pass again.. the motor will again rotate.. this will repeat for about 11 times.. until the time changes to 9:15:00 or more.

  4. So the solution is to give a delay, before exiting the if loop if you wait for a minute with

    delay(60000);

then, the next 'if' condition will fail and works as per your expectation.


Part 2:

debugging:

        if ((Hour== 9 && Min== 14)||(Hour== 21 && Min== 0)) {
          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(15);
            Serial.println("moving");
    ....

enter image description here

13
  • Like this? void loop() { t = rtc.getTime(); Hour = t.hour; Min = t.min; Sec = t.sec; // Serial.print(Hour); // Serial.print(":"); // Serial.print(Min); // Serial.print(":"); Serial.println(Sec); //set the time for fish feeding if ((Hour== 11 && Min== 46 && Sec== 0)||(Hour== 21 && Min== 0 && Sec== 0)) { for (pos = 0; pos <= 180; pos += 1) { // in steps of 1 degree myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } delay(60000); } } Sep 13 '20 at 14:49
  • But anyways, the issue here is that the servo doesn't even start either way! Sep 13 '20 at 14:49
  • you should check the Servo connections once. I doubt on that
    – ArduinoFan
    Sep 13 '20 at 15:05
  • 1
    It's all fine with the connections, I just tested the servo with the servo sweep exemple code Sep 13 '20 at 15:08
  • I just added the simulation snapshot. I created using the wokwi Arduino simulator.. it works just fine.. nevertheless, can you use some print statements to see whether the IF condition is passing ever?
    – ArduinoFan
    Sep 13 '20 at 15:13
-1

I think the problem is that you have given the baud rate as 9200 but the minimum baud rate is 9600(Serial.begin(9600)). And change the baud rate in the serial monitor.

1
  • 1
    4 hours before you posted your answer, Tiago Kotryk already replied in the comments that changing the baud rate to 9600 didn't make a difference. So why post this as an answer?
    – StarCat
    Sep 13 '20 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.