I need help with servo part of automatic feeder code

Hello,

I have written an program for aan automatic petfeeder that uses an real time clock.
Its almost correct but there are some problems.
After 5 seconds it shows the time and date of the real time clock but not very clear.
It shows "petfeeder" perfect in the first 5 seconds so i don't know why it won't show it clear after the setup.
Also after the 5 seconds the Servo turns constantly instead of when the feeding time is the same as the real time like i have written in the program.
I don't have any failure messages yet.
I am using an RTC DS1302, servo for an plate that turn 45 degrees so there is an hole where the food can fall in, potentiometer for changing the feeding time, a button so i can go to the feeding time and changing the hours and minutes separately and an lcd with I2C where i display the actual time and date but when i change the feeding time it says "feeding time" and shows the time you are setting.
anyone who can help me find the problem.

//REAL TIME CLOCK MODULE
  // CONNECTIONS:
  // DS1302 CLK/SCLK --> 5
  // DS1302 DAT/IO --> 4
  // DS1302 RST/CE --> 2
  // DS1302 VCC --> 3.3v - 5v
  // DS1302 GND --> GND

  #include <ThreeWire.h>  
  #include <RtcDS1302.h>

  ThreeWire myWire(4,5,2); // IO, SCLK, CE
  RtcDS1302<ThreeWire> Rtc(myWire);

  int uur;
  int minuten;
  int seconden;

//SERVO
  #include <Servo.h>
  Servo myservo;


//LCD SCHERM
  #include <LiquidCrystal_I2C.h>
  #include <Wire.h> 

  LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//POTENTIOMETER
  const int pinPotentiometer = A0;

  int resultaatUUR = 0;
  int resultaatMINUUT = 0;

//KNOP  
  const byte button    = 7;

  byte buttonState     = 0;
  byte lastButtonState = HIGH;
  byte buttonCounter   = 0;


// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 1000;  // interval at which to blink (milliseconds)


//******************************************** 
  void setup () 
{
//REAL TIME CLOCK MODULE
  Serial.begin(57600);

  Serial.print("compiled: ");
  Serial.print(__DATE__);
  Serial.println(__TIME__);

  Rtc.Begin();

  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  printDateTime(compiled);
  Serial.println();

  if (!Rtc.IsDateTimeValid()) 
  {
      // Common Causes:
      //    1) first time you ran and the device wasn't running yet
      //    2) the battery on the device is low or even missing

      Serial.println("RTC lost confidence in the DateTime!");
      Rtc.SetDateTime(compiled);
  }

  if (Rtc.GetIsWriteProtected())
  {
      Serial.println("RTC was write protected, enabling writing now");
      Rtc.SetIsWriteProtected(false);
  }

  if (!Rtc.GetIsRunning())
  {
      Serial.println("RTC was not actively running, starting now");
      Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled) 
  {
      Serial.println("RTC is older than compile time!  (Updating DateTime)");
      Rtc.SetDateTime(compiled);
  }
  else if (now > compiled) 
  {
      Serial.println("RTC is newer than compile time. (this is expected)");
  }
  else if (now == compiled) 
  {
      Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  }
  
//LCD SCHERM
  lcd.init();
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print("Petfeeder");
  delay(5000);

//SERVO
  myservo.attach(13);
  myservo.write(55); 

//POTENTIOMETER
  pinMode(pinPotentiometer, INPUT);

//KNOP
  pinMode(button, INPUT_PULLUP);
  
} //END of      s e t u p ( )

//********************************************

bool modeInstellen = false;

 void loop () 
{
//REAL TIME CLOCK MODULE
  RtcDateTime now = Rtc.GetDateTime();

  printDateTime(now);
  Serial.println();
  
  int reading = digitalRead(button);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == LOW) {
        modeInstellen = !modeInstellen;
      }
    }
  }

  /*Serial.print("MI:");
  Serial.println(modeInstellen);*/
  
//KNOP + POTENTIOMETER
  if(modeInstellen == true)
  {
    checkButton();

    if (buttonCounter == 1)
    {
      lcd.clear();
      lcd.setCursor(1,0);
      lcd.print("ingesteld feed uur");
      lcd.setCursor(1,1);
      lcd.print("00:00");
    }
    
    if (buttonCounter == 2)
    {
      int valueUUR = analogRead(pinPotentiometer); //0 1023
      Serial.println(valueUUR);
      resultaatUUR = valueUUR / 42.625 ;
      lcd.setCursor(1,0);
      lcd.print("ingesteld feed uur");
      lcd.setCursor(1,1);
      if(resultaatUUR < 10)
      {
        lcd.print("0");
      }
      lcd.print(resultaatUUR);
      Serial.println(resultaatUUR);
      lcd.print(":00");
    }
    if (buttonCounter == 3)
    {
      int valueMINUUT = analogRead(pinPotentiometer); //0 1023
      Serial.println(valueMINUUT);
      resultaatMINUUT = valueMINUUT / 17.05 ;
      lcd.setCursor(3,1);
      lcd.print(":");
      lcd.setCursor(4,1);
      if(resultaatMINUUT < 10)
      {
        lcd.print("0");
      }
      lcd.print(resultaatMINUUT);
      Serial.println(resultaatMINUUT);
    }
    if (buttonCounter == 4)
    {
      modeInstellen = false;
      buttonCounter = 0;
    }
  }
  else
  {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
  
      //REAL TIME CLOCK MODULE
    
      if (!now.IsValid())
      {
          // Common Causes:
          //    1) the battery on the device is low or even missing and the power line was disconnected
          Serial.println("RTC lost confidence in the DateTime!");
      }      
//SERVO
      else
      {
        Serial.println(resultaatUUR);
        Serial.println(resultaatMINUUT);
        Serial.println(uur);
        Serial.println(minuten);
        Serial.println(seconden);    
        if(uur == resultaatUUR && minuten == resultaatMINUUT && seconden == 00) 
        {
          myservo.write(100);
          delay(1000);
          myservo.write(55);
        }
        
      }
    }
  }

  lastButtonState = reading;
}

  #define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
  char datestring[20];

  snprintf_P(datestring, 
          countof(datestring),
          PSTR("%02u/%02u/%04u "),
          dt.Month(),
          dt.Day(),
          dt.Year() );
  Serial.print(datestring);

  char datestring2[20];

    snprintf_P(datestring2, 
          countof(datestring2),
          PSTR("%02u:%02u:%02u"),
          dt.Hour(),
          dt.Minute(),
          dt.Second() ); 
  uur = dt.Hour();
  minuten = dt.Minute();
  seconden = dt.Second();
  Serial.print(datestring2);
  
//LCD SCHERM
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print(datestring);
  lcd.setCursor(1,1);
  lcd.print(datestring2);

} //END of     l  o o p ( )


//********************************************
void checkButton()
{
  buttonState = digitalRead(button);

  if (lastButtonState != buttonState)
  {
    lastButtonState = buttonState;

    //has the button been released (HIGH is not pushed)
    if (buttonState == HIGH)
    {
      buttonCounter++;
    }
  }

} //END of     c h e c k B u t t o n ( )

Fix one thing at a time. ONLY display information when that information changes.

int valueUUR = analogRead(pinPotentiometer); //0 1023
      Serial.println(valueUUR);
      resultaatUUR = valueUUR / 42.625 ;
int valueMINUUT = analogRead(pinPotentiometer); //0 1023
      Serial.println(valueMINUUT);
      resultaatMINUUT = valueMINUUT / 17.05 ;

What are the decimals supposed to do? Resultaat.... is declared as an integer as well as value... That division is performed as division between 2 integers as I see it.

i used the decimals to devide 1023 to 60 or 24. so i can use an potentiometer to set my hours and minutes.

I think i solved the lcd part.
Now the only thing that remains is to let the servo turn on the Time that has been set.
anyone know why the servo not turns at the time that it has been set on? it keeps turning from the moment he enters the loop.

//SERVO
  myservo.attach(13);
  myservo.write(55);

Are you certain that you don't have a continuous rotation servo?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.