Trying to get motor to turn on at specific time

what should i enter then for the time if it was say 6:38pm? thats 1838hrs military time, thats why i put that, obviously i was wrong.

No, it is not one thousand, eight hundred thirty eight hours.

The value from now.hour() will be between 0 and 23. The value from now.minute() will be between 0 and 59. You have to use a compound if statement.

mbasile:
what should i enter then for the time if it was say 6:38pm? thats 1838hrs military time, thats why i put that, obviously i was wrong.

Well, if the DateTime library is where you are getting the hour from, maybe you could go look at that and THINK about it. Or I guess you could just ask and make us tell you that there is also a minutes function.

@BulldogLowell's code was for your original specification that it should happen at 6 oclock. Now you want it between 6 and 7, so you're going to have to do some more thinking. His code isn't going to work if you're not looking for the point where the hour just changed. You're going to have to think about how to write this differently to look at hours and minutes.

mbasile:
what should i enter then for the time if it was say 6:38pm? thats 1838hrs military time, thats why i put that, obviously i was wrong.

So, looking at @BulldogLowell's code where he just used 6 and 18, do either of those look like he was talking about military time? Or just the hour part of it? If it were military time don't you think he would have used 600 and 1800?

Again, this is going to require some thought on your part.

should this be the format for getting the motor to turn with the time at 630pm?

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

RTC_DS1307 RTC;
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
int motorPin = 9;
int lastHour = 24;
int lastMinute = 59;
bool feedPet = false;
unsigned long feedTime = 0;
unsigned long lastPrintTime = 0;

void setup ()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
lcd.println("RTC NOT Running!");
RTC.adjust(DateTime((DATE), (TIME)));
}
}

void loop ()
{
DateTime now = RTC.now();
int currentHour = now.hour();
int currentMinute = now.minute();
if (lastHour != currentHour && (currentHour == 6 || currentHour == 18)) // if the hour just changed to 6 or 18
if (lastMinute != currentMinute && (currentMinute == 30 || currentMinute == 30)) //if the minute is 30
{
feedTime = millis();
feedPet = true;
lastHour = currentHour;
lastMinute = currentMinute;
}
if (feedPet)
{
turnFeeder();
}
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.print(" ");

if (millis() - lastPrintTime > 1000UL)
{
lcd.setCursor(0, 0);
lcd.print("DATE: ");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lastPrintTime = millis();
}
}
void turnFeeder(void)
{
static byte pinState = HIGH;
if (millis() - feedTime < 3000UL) // 30 seconds
{
if (pinState = HIGH)
{
digitalWrite(motorPin, pinState);
pinState = LOW;
}
}
else
{
digitalWrite(motorPin, pinState);
pinState = HIGH;
feedPet = false;
}
}

Yeah but I don't see any reason to test that currentMinute is 30 twice in the same if statement.

just tried to implement the code with the motor.. the only thing that happened was the seconds dissapeared from the lcd and it said "time" but no motor spin

mbasile, check your messages

if anyone could offer assistance as to why the motor still wont turn on and why i cant get it to turn on at 10am and at 630pm for 30 seconds at a time..... i really need some help and i have done a ton of research that still hasnt helped much

If you write a sketch without all the time stuff and just write the motor pin high does the motor run then?

If you put a print statement in turnFeeder function does it print and let you know that function is being called?

i am not sure on how to properly add the print statement to turn feeder however, it will spin the motor if i write a code to do that

You've got a dozen or more other lines in this sketch to print to the LCD. If you really can't figure out how to add one to that function then this project is way over your head and you need to go back and study the basics and stop asking us to write this for you. I would suggest making it the first line in that function and putting a delay of a second or two after it to make sure you have time to see it. You can take it out after you know that function is being called.

void turnFeeder(void)
{
  lcd.setCursor(0,0);
  lcd.print("turnFeeder");
  static byte pinState = HIGH;
  if (millis() - feedTime < 3000UL) // 30 seconds
  {
    if (pinState = HIGH)
    {
      digitalWrite(motorPin, pinState);
      pinState = LOW;
    }
  }
  else
  {
    digitalWrite(motorPin, pinState);
    pinState = HIGH;
    feedPet = false;
  }
}

what happened when the time hit the specified target was the date is read "turnFeeder" and the place where the seconds are read "time"

Alright, so now you know that function is being called. Now move it inside the if(millis... right after where it is now and see if it is getting into that if.

Do you see what we are doing here? Tracking down where the code is going?

i understand what we are trying to do, but i am confused as to how to print the function inside of the milis... i just tried and got a error preventing me from running it.

im sure i did not put it in the right place, i tried putting it under the if statement but that didnt work, this was the only way to not have an error that i found... and it just had the same result as the last run....

void turnFeeder(void)
{
  static byte pinState = HIGH;
  lcd.setCursor(0, 0);
  lcd.print("turnFeeder");
  if (millis() - feedTime < 3000UL) // 30 seconds
  {
    if (pinState = HIGH)
    {
      digitalWrite(motorPin, pinState);
      pinState = LOW;
    }
  }
  else
  {
    digitalWrite(motorPin, pinState);
    pinState = HIGH;
    feedPet = false;
  }
}

Well that's not inside the if. Put it right there between the opening brace of that block and the if(pinState == HIGH)

The if pinState line should be a comparison, double equal sign. Right now it is assignment. That may be your problem.

Does the motor run when you write the motor Pin HIGH or LOW?

mbasile:
i understand what we are trying to do, but i am confused as to how to print the function inside of the milis... i just tried and got a error preventing me from running it.

something like this (untested, to make it even more fun):

void turnFeeder(void)
{
  static byte pinState = HIGH;
  if (millis() - feedTime < 6000UL) // 60 seconds
  {
    if (pinState == HIGH)
    {
      lcd.setCursor(0, 0);
      lcd.print("turnFeeder");
      digitalWrite(motorPin, pinState);
      pinState = LOW;
    }
  }
  else
  {
    digitalWrite(motorPin, pinState);
    pinState = HIGH;
    feedPet = false;
  }
}

if by does the motor run when i write high or low, when i put in a code to simply run the motor high and low, (a seperate code) yes the motor runs,

i made these changes to the code and the only thing that changed was when it used to override the seconds on the lcd display, it now reads part of the "turnFeeder"

void loop ()
{
  DateTime now = RTC.now();
  int currentHour = now.hour();
  int currentMinute = now.minute();
  if (lastHour != currentHour && (currentHour == 9 || currentHour == 21)) // if the hour just changed to 6 or 18
    if (lastMinute != currentMinute && (currentMinute == 57)) //if the minute is 30

    {
      feedTime = millis();
      feedPet = true;
      lastHour = currentHour;
      lastMinute = currentMinute;
    }
  if (feedPet)
  {
    turnFeeder();
  }
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);
  lcd.print(" ");

  if (millis() - lastPrintTime > 1000UL)
  {
    lcd.setCursor(0, 0);
    lcd.print("DATE: ");
    lcd.print(now.month(), DEC);
    lcd.print("/");
    lcd.print(now.day(), DEC);
    lcd.print("/");
    lcd.print(now.year(), DEC);
    lastPrintTime = millis();
  }
}
void turnFeeder(void)
{
  static byte pinState = HIGH;
  if (millis() - feedTime < 6000UL) // 60 seconds
  {
    if (pinState == HIGH)
    lcd.setCursor(0, 0);
      lcd.print("turnFeeder");
    {
      digitalWrite(motorPin, pinState);
      pinState = LOW;
    }
  }
  else
  {
    digitalWrite(motorPin, pinState);
    pinState = HIGH;
    feedPet = false;
  }
}