Critque needed for Dog Feeder Program

Hello guys,

So I’m trying to write a simple program for a ‘Dog Feeder’. Basically, every day at 2:30PM, a latch connected to a servo motor needs to be turned 90 degrees and all the food inside needs to be dispensed out into a food bowl.
I have tried my best to write the code, but could use your input in improving it.

I have connected a servo motor (DXW90) and an RTC Module (DS1307) to an arduino at 5V. Servo is connected to pin 9. I’m using the RTC Module to keep accurate time but am not sure if its really required. Also I have used the library RTCLib for the RTC Module. Library Link

My current problem is that I feel the code is too bloated for something so simple. Also at 2:30PM it will turn only halfway, and won’t go back to the original position. Help!!

Thanks! :smiley:

// Hardware- RTC1307 and Servo Motor

#include <Arduino.h>
#include <Wire.h>         // this #include still required because the RTClib depends on it
#include "RTClib.h"
#include <Servo.h>

Servo myservo;
int pos = 0; 


#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

RTC_Millis rtc;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

    /////
    myservo.attach(9);
    /////
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(1000);

/////
if ((now.hour() == 14) && (now.minute() == 30) && (now.second() == 30)) {
for (pos = 0; pos <= 90; 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(50);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 90; pos <= 0; 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(50);                       // waits 15ms for the servo to reach the position
}
}
else{
  }

}

You have an empty else at the end that needs clean up but your pb is the second for loop that is supposed to bring back the servo to its original position

for (pos = 90; [color=red]pos <= 0; pos += 1[/color]) {

The red part needs some work. See what’s wrong?

Hi JML, Thanks for the feedback!

I deleted the else() statement at the end.

I realized that the second 'for' statement had the wrong signs! It should have been

for (pos = 90; pos >= 0; pos -= 1)

Good news is that the servo motor is now turning 90 degrees and after a few secs, turning back 90 degrees as intended. However, bad news is that after coming to the final resting position, the servo motor is making a 'buzzing' noise continuously. Without stop. And getting a tad bit hot as well :/

command

achan12345: Good news is that the servo motor is now turning 90 degrees and after a few secs, turning back 90 degrees as intended. However, bad news is that after coming to the final resting position, the servo motor is making a 'buzzing' noise continuously. Without stop. And getting a tad bit hot as well :/

don't go back to zero start at 10 (or where ever the buzzing stops) and go to 100 adjust your link for the new positions

Do you actually need the for loops that move the servo ? Why not a single write() that moves the servo to the required position ?

Why don't the comments match what the code does in the for loops ? It does not matter, of course, as long as the code does what you want, but i smacks of sloppy programming,

zhomeslice: command don't go back to zero start at 10 (or where ever the buzzing stops) and go to 100 adjust your link for the new positions

Hey zhomeslice,

I've started and stopped at all ranges from 0-180. The servo motor always ends up humming/buzzing after doing its movements. Is that normal with a servo? Not sure what the problem is.

UKHeliBob:
Do you actually need the for loops that move the servo ? Why not a single write() that moves the servo to the required position ?

Why don’t the comments match what the code does in the for loops ? It does not matter, of course, as long as the code does what you want, but i smacks of sloppy programming,

Hi UKHeliBob,

Thanks for the input! I revised it to a single write() command and it does move correctly. The issue is that the servo is still humming/buzzing after completing its movement. I’m not sure what the issue is. Maybe the servo needs to be given a command to have 0 power in during all times except when required :S

I’ve included the updated code below-
PS- I had changed the rotation degree and time code several times during editing and had forgotten to update the comments. My bad!

// Hardware- RTC1307 and Servo Motor

#include <Arduino.h>
#include <Wire.h>         // this #include still required because the RTClib depends on it
#include "RTClib.h"
#include <Servo.h>

Servo myservo;


#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

RTC_Millis rtc;

void setup () {
  Serial.begin(57600);
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  myservo.attach(9);
  myservo.write(0);

}

void loop () {
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(1000);

  if ((now.hour() == 14) && (now.minute() == 30) && (now.second() == 30)) {
    myservo.write(90);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 5s for the servo to reach the position
    myservo.write(0);              // tell servo to go to position in variable 'pos'
    delay(5000);                      // waits 5s for the servo to reach the position
  }
}

It sounds like the servo is binding and is not free to move completely to the positions commanded. Try changing the end points or removing the servo output arm temporarily to see whether the servo still buzzes when at the extremes.

Sorry to bang on about comments, but none of the ones about moving the servo in your code match what the code does and you don't need the second delay() anyway.