Trying to get motor to turn on at specific time

i am trying to turn my 12v dc motor on at a certain time (i.e. 10am) spin for a set amount of time (i.e. 10 seconds) and then turn off until the next time (i.e. 6pm) and do the same function. This is going to be for a pet feeder. I have set the RTC which is a DS1307 type, i have the code for that set up with the date and time synced to real time, however i cannot get the motor to turn at a specific time.
this is the code i have so far.

#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 time = 20500;

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

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

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

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);

if (time == "20500"){
digitalWrite(motorPin, HIGH);
}
else (time != "20500"){
digitalWrite(motorPin, LOW);
}

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);
}

 if (time == "20500"){
  1. time is NOT a string.
  2. That is NOT how to compare strings.
else (time != "20500"){

NOTHING follows the else, unless it is another if statement.

i am a total beginner to this coding, is there any way to provide some help with the code, as in help me write it... that would be much appreciated!

mbasile:
i am a total beginner to this coding, is there any way to provide some help with the code, as in help me write it... that would be much appreciated!

something like this pseudo code, lastHour being a global variable.

currentHour = now.hour();
if (lastHour != currentHour && (currentHour == 6 || currentHour == 18)) // if the hour just changed to 6 or 18
{
  do.something();
  lastHour = currentHour;
}

ive been reading a lot on line, and trying to figure this out. I'm not completely clueless when it comes to coding, i have MatLab experience and some general knowledge, however, i feel like this is more C++ oriented which i have not gone over.

Nonetheless, i have continued trying this code and havent found much on what i want to do. All the videos i have watched have just been of peoples completed projects without explanation. I have watched videos on if statements and while loops, but i am confused on how to implement the functions i need. does anyone have anylinks??

The Arduino IDE is packed with programming examples.
Work through a few, see how it goes

mbasile:
does anyone have anylinks??

Did you try looking at what I posted?

You would implement it something like this (untested) bit of code:

#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;
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 is NOT running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }
}

void loop ()
{
  DateTime now = RTC.now();
  int currentHour = now.hour();
  if (lastHour != currentHour && (currentHour == 6 || currentHour == 18)) // if the hour just changed to 6 or 18
  {
    feedTime = millis();
    feedPet = true;
    lastHour = currentHour;
  }
  if (feedPet)
  {
    turnFeeder();
  }
  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)
    {
      digitalWrite(motorPin, pinState);
      pinState = LOW;
    }
  }
  else
  {
    digitalWrite(motorPin, pinState);
    pinState = HIGH;
    feedPet = false;
  }
}

it may feed on startup, I'll leave it to you to figure out how to prevent that.

BullDogLowell, i looked at the code you posted and implemented it into my code almost like that last one you posted. however i got some errors that i could not figure out,

when i run your code i get this error

error: lvalue required as left operand of assignment

do you know what that means? i am currently googling to figure out how to overcome this.

do you know what that means?

Some things, like variables, are lvalues. That means that they can appear on the left of an assignment statement. Some things are rvalues, like function calls. That means that they can appear on the right side of an assignment statement.

Some things can be either lvalues or rvalues. A variable can appear on either side. A constant can not. A function call can not.

The line number in the error message gave you a clue as to where in the code the problem is.

the error is in the void turnFeeder section

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

the error message points to what Delta said, its somewhere in the line

if (millis() = feedTime < 6000UL) // 60 seconds

uploaded.ino: In function 'void turnFeeder()':

uploaded.ino:56:16: error: lvalue required as left operand of assignment

Error compiling.

if (millis() = feedTime < 6000UL) // 60 seconds

changing to this

if (millis() = - feedTime < 6000UL) // 60 seconds

still has the same error

i figured it out as soon as i posted but it wouldnt let me edit my post since i had already posted to much in 10 minutes or whatever.

code is currently

however i set the time to 626 and 1826 to get the the motor to turn, but it did not do so at the specified time...

#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;
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();
if (lastHour != currentHour && (currentHour == 626 || currentHour == 1826)) // if the hour just changed to 6 or 18
{
feedTime = millis();
feedPet = true;
lastHour = currentHour;
}
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)
{
digitalWrite(motorPin, pinState);
pinState = LOW;
}
}
else
{
digitalWrite(motorPin, pinState);
pinState = HIGH;
feedPet = false;
}
}

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.

Delta_G:
OK, so on my planet there are only 24 hours in a day. Where do you live that there are more than 1826 hours in one day?

Damn, you beat me to it.That was the exact question I was going to ask.

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.

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;
}
}

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

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