Servo doesn't work with RTC alarm

I am using an arduino mkr1000 board and I am trying to make a dog feeder for my dog. I looked up a many dog and cat feeder project on google and I realised that the best thing to do is with a RTC alarm system. I am using the RTC Zero library cause I have mkr 1000 that support it. When alarm is on arduino call function where saying what to do. When I put the moving servo code in it it worked only half. Servo didn’t do the whole move only half of it. Here is the code that I used:

#include <RTCZero.h>
#include <Servo.h>


RTCZero rtc;
Servo myservo;

const byte seconds = 59;
const byte minutes = 34;
const byte hours = 17;

int pos = 0;   

const byte day = 1;
const byte month = 5;
const byte year = 18;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  rtc.begin(); 
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmTime(17, 35, 10);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  
  rtc.attachInterrupt(Feed);
}

void loop()
{
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  delay(1000);

}
void Feed(){
  //Serial.println("A");
  for (pos = 0; pos <= 100; pos += 1) { 
    myservo.write(pos);                                 
  }
  for (pos = 100; pos >= 0; pos -= 1) { 
    myservo.write(pos);                                   
  }
  //Serial.println("B"); 
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

If anyone can help me with my problem I will be really happy.
It would mean me. Thanks in advance

A servo is quite slow compared to the speed of the code being executed so your code does not allow the servo to fully adjust before you re-adjust it. Try the following code:

myservo.write(100); //You do not need to do it step by step with a for loop.
delay(1000); //Wait one second, this must be calibrated to the speed you need
myservo.write(0);
delay(1000); //May not be required

Servo write is capable of moving a servo a full 180 degrees by writing the angle anywhere from 0-180. Does your code only move it from 0-100? Because 100 isn't full movement. 180 is.

Northof49:
Servo write is capable of moving a servo a full 180 degrees by writing the angle anywhere from 0-180. Does your code only move it from 0-100? Because 100 isn't full movement. 180 is.

Please note that a servo may not be capable of 180 degree movement regardless of the values written to it.

Northof49:
Servo write is capable of moving a servo a full 180 degrees by writing the angle anywhere from 0-180.

That's a misunderstanding of how servos work. Servo.write(0) to Servo.write(180) will move a servo from one end to the other of the range it is capable of. Depending on the particular type of servo that might physically be 100 degrees, 270 degrees or 135 degrees. Sometimes it might even be 180 degrees,but that's quite rare.

Steve

Yes, you are correct about it not corresponding to degrees, but how is his 0-100 command going to move it much more than half way? 0-90 is close to half movement, so 0-100 may not be sufficient.

I was not clear in subject. My servo can move 0 - 180 I was trying (still trying) to move servo 0 - 100 and then 100 - 0 but it goes only 0 - 100 never go back to 0.

What happens when you slow your original sketch down?

When I put this code to my arduino everything go as it should.

#include <Servo.h>

Servo myservo;  
void setup() {
  myservo.attach(9);
  myservo.write(100);
  delay(250);
  myservo.write(0);
  delay(500);
  myservo.write(100);
  delay(250);
}

void loop() {
}

But when I put the same part in last code like this

#include <RTCZero.h>
#include <Servo.h>


RTCZero rtc;
Servo myservo;

const byte seconds = 59;
const byte minutes = 34;
const byte hours = 17;

int pos = 0;   

const byte day = 1;
const byte month = 5;
const byte year = 18;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(100);
  delay(500);
  rtc.begin(); 
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmTime(17, 35, 10);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  
  rtc.attachInterrupt(Feed);
}

void loop()
{
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  delay(1000);

}
void Feed(){
  Serial.println("A");
  myservo.write(0);
  delay(500);
  myservo.write(100);
  delay(250);
  Serial.println("B"); 
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

Servo wont do the whole thing

TolpuddleSartre:
What happens when you slow your original sketch down?

Still only move half a way like he is ignoring the second for loop.

But when I put the same part in last code like this

There's nothing there.

Is that a real interrupt?
With a delay in it?

TolpuddleSartre:
There’s nothing there.

I did this (delay is in the for loop)

#include <RTCZero.h>
#include <Servo.h>


RTCZero rtc;
Servo myservo;

const byte seconds = 59;
const byte minutes = 34;
const byte hours = 17;

int pos = 0;   

const byte day = 1;
const byte month = 5;
const byte year = 18;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  rtc.begin(); 
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmTime(17, 35, 10);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  
  rtc.attachInterrupt(Feed);
}

void loop()
{
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  delay(1000);

}
void Feed(){
  //Serial.println("A");
  for (pos = 100; pos >= 0; pos -= 1) { 
    myservo.write(pos);  
    delay(8);       // I put delay here                          
  }
  for (pos = 0; pos <= 100; pos += 1) { 
    myservo.write(pos); 
    delay(8);       // And here                  
  }
  
  //Serial.println("B"); 
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

TolpuddleSartre:
Is that a real interrupt?
With a delay in it?

Everything about RTC is from the example SimpleRTCalarm

And that example code has a delay in it?

Try inverting your for loops:

void Feed(){
  //Serial.println("A");
  for (pos = 100; pos >= 0; pos -= 1) {
    myservo.write(pos); 
    delay(8);                                  
  }
  for (pos = 0; pos <= 100; pos += 1) {
    myservo.write(pos);
    delay(8)                                 
  }
  //Serial.println("B");
}

EDIT: Changed delay from 5 to 8.

If you are wanting to set the starting position to 100, do this:
In globals:

byte startPos = 100;

In setup() :

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

TolpuddleSartre:
And that example code has a delay in it?

No I added the delay in there for printing time and date like in example SimpleRTC

outsider:
Try inverting your for loops:

void Feed(){

//Serial.println(“A”);
  for (pos = 100; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(8);                                 
  }
  for (pos = 0; pos <= 100; pos += 1) {
    myservo.write(pos);
    delay(8)                               
  }
  //Serial.println(“B”);
}



EDIT: Changed delay from 5 to 8.

If delay is in there program stops and just wait if no then just move on without moving servo

Is the RTCZero alarm interrupt interfering? I'm not familiar with that library.