Go Down

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

Delta_G

#15
Nov 15, 2015, 12:22 am Last Edit: Nov 15, 2015, 12:28 am by Delta_G
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? 
|| | ||| | || | ||  ~Woodstock

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

mbasile

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

Delta_G

#17
Nov 15, 2015, 12:32 am Last Edit: Nov 15, 2015, 12:33 am by Delta_G
Code: [Select]
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?
|| | ||| | || | ||  ~Woodstock

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

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.

PaulS

#19
Nov 15, 2015, 12:40 am Last Edit: Nov 15, 2015, 12:45 am by PaulS
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.
The art of getting good answers lies in asking good questions.

PaulS

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

Delta_G

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

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

Delta_G

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

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

mbasile

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

Delta_G

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

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

mbasile

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

Gadgetman9


mbasile

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

Delta_G

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

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

Delta_G

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

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

Go Up