Hello everybody,
I am trying to make a timed pet feeder however I am having some trouble with my code. I cannot seem to get my servo to active at a specific time. My servo is a continuous from parallax. I got a timer code from the RTC examples of arduino and tried to add on to it. I want my servo to turn on and continuously rotate during a span of a minute however it doesn't turn on at all. I was wondering if anyone of you could advice me or give me feedback.
These are the question, I would like to learn the answer too:
- How do I turn my servo on at a specific time?
- How do I turn it off after a minute?
Thank you so much for all of your time.
// now.pde
// Prints a snapshot of the current date and time along with the UNIX time
// Modified by Andy Wickert from the JeeLabs / Ladyada RTC library examples
// 5/15/11
#include <Wire.h>
#include <Servo.h>
#include "DS3231.h"
int servopin = 9; //setting up pin for servo
int pulse = 1500;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
// clear /EOSC bit
// Sometimes necessary to ensure that the clock
// keeps running on just battery power. Once set,
// it shouldn't need to be reset but it's a good
// idea to make sure.
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select register
Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
Wire.endTransmission();
//Set servo as output
pinMode(servopin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// send request to receive data starting at register 0
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write((byte)0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours)
while(Wire.available())
{
int seconds = Wire.read(); // get seconds
int minutes = Wire.read(); // get minutes
int hours = Wire.read()+1; // get hours
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert BCD to decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
hours = (((hours & 0b00100000)>>5)*20 + ((hours & 0b00010000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
// I would like to turn my servo on at this specific time however it does not work!
if (hours == 14 && minutes >33 && minutes < 34)
{
digitalWrite(servopin, HIGH); // sets the LED on
}
}