-2

I've been trying to make this code work, but for some reason, it doesn't work for me, even if I do everything like in the tutorial. So I've tried to do a new code and the outcome was the same. The servo doesn't move at all when it is supposed to.

this is my code:

#include <Servo.h>
#include <DS1302.h>

Time t;
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
// Init a Time-data structure

int pos = 0;    // variable to store the servo position
int Hour;
int Min;
int Sec;

// Init the DS1302
DS1302 rtc(2, 3, 4);

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(15);

  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1302
//  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
//  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
//  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
}

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== 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
  }
 }
}

Since everything about arduino is new for me, I'm afraid I might be doing something wrong other than the code. So I wanna show some print screens to you in order you can identify something I am doing wrong.

enter image description here

enter image description here

enter image description here

Update 1:

I have add "Serial.println("moving");" to the clause IF, and it shows me the servo is moving every secound. How to solve that? enter image description here

#include <Servo.h>
#include <DS1302.h>

Time t;
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
// Init a Time-data structure

int pos = 0;    // variable to store the servo position
int Hour;
int Min;
int Sec;

// Init the DS1302
DS1302 rtc(2, 3, 4);

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(15);

  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1302
//  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
//  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
//  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
}

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 
Serial.println("moving");
if ((Hour== 12 && Min== 28 && Sec== 5)||(Hour== 21 && Min== 0 && Sec== 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
  }
 }
}
2
  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Sep 14 '20 at 8:40
  • Comments are for clarifying the question, not general problem-solving. Please use the chat for that. – Nick Gammon Sep 14 '20 at 8:41
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); } } – Tiago Kotryk Sep 13 '20 at 14:49
  • But anyways, the issue here is that the servo doesn't even start either way! – Tiago Kotryk 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 – Tiago Kotryk 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

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