Go Down

Topic: Trying to get motor to turn on at specific time (Read 3797 times) previous topic - next topic

mbasile

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

PaulS

Code: [Select]
if (time == "20500"){
1) time is NOT a string.
2) That is NOT how to compare strings.

Code: [Select]
else (time != "20500"){
NOTHING follows the else, unless it is another if statement.
The art of getting good answers lies in asking good questions.

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!

Delta_G

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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

BulldogLowell

#4
Nov 14, 2015, 09:37 pm Last Edit: Nov 14, 2015, 09:40 pm by BulldogLowell
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.

Code: [Select]

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

mbasile

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??

GrooveFlotilla

The Arduino IDE is packed with programming examples.
Work through a few, see how it goes
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

BulldogLowell

#7
Nov 14, 2015, 10:59 pm Last Edit: Nov 14, 2015, 11:03 pm by BulldogLowell
does anyone have anylinks??
Did you try looking at what I posted?

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

Code: [Select]
#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.



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.

Delta_G

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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

PaulS

Quote
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 art of getting good answers lies in asking good questions.

Delta_G

Code: [Select]
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?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

mbasile

#12
Nov 14, 2015, 11:41 pm Last Edit: Nov 14, 2015, 11:44 pm by mbasile
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.

Delta_G

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

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

mbasile

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

changing to this

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


still has the same error

Go Up