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!

That's what he's done. Nobody is going to write it for you.

He said time is not a string. Strings use quotes. Numbers don't. Try to find some examples of code and look for people comparing to a number. How do they do it?

He told you not to try to put a condition on an else without it being an if. OK, go look at some other code. Do you see anyone else doing that? No. So don't do it.

Make those changes and let's see where we're at. If you're not willing to try to work through this then go sell your board and find a new hobby. Programming isn't easy. It's going to require some effort on your part to learn. Perhaps it would be much better for you to start with some simpler examples and learn the basics before you try a code this big and involved.

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.

mbasile: 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.

Maybe if you posted that code where we could see it we could tell you why you got that error.

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.

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

Is it this line? He did say untested didn’t he?

How can you assign a value to a function like that? You can’t. Maybe that was supposed to be a minus sign like in every other use of the blink without delay technique?

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.

Ok, in my last post I told you how to fix that. It was a typo.

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

changing to this

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

still has the same error

SMDH!

Do you not get that you can't assign something to a function call? Trying to assign something negative isn't going to work any better than trying to assign something positive.

You have to be willing to think a little or this is going to get real old real quick.

Go look at the Blink Without Delay example and see how that line should be written.

Or hell, look a few lines up in the code @BulldogLowell gave you and see the other place where he used that technique.

You can't assign anything to a function call. You can subtract something from the value it returns. Does that clue you in?

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

 if (lastHour != currentHour && (currentHour == 626 || currentHour == 1826)) // if the hour just changed to 6 or 18

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?

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.