Go Down

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

mbasile

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

Delta_G

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

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

mbasile

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

Delta_G

#33
Nov 15, 2015, 03:25 am Last Edit: Nov 15, 2015, 03:25 am by Delta_G
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?
|| | ||| | || | ||  ~Woodstock

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

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.

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

Code: [Select]
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;
  }
}

Delta_G

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

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

Delta_G

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

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

Delta_G

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

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

BulldogLowell

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

Code: [Select]
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;
  }
}

mbasile

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"
 




Code: [Select]
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;
  }
}

Delta_G

No what I meant was does HIGH make the motor run or does LOW make the motor run?  It depends on how you have it wired.

How do you have the motor powered?  It has its own power supply right?
|| | ||| | || | ||  ~Woodstock

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

mbasile

high powers the motor because when i coded just the motor to run high was on and low was off


i have the motor powered by the arduino itself, right now the arduino is plugged into the computer i am using.

i created basically a motor-shield i guess, i used a 2,2k resistor and a transistor and a diode so that when the motor turns off it will not hurt the arduino or anything else.

i feel that i should say that the pin i have designated is pin 9 for the motorpin which is an output, however i put the wire i have attached to pin 9 in the arduino to pin 8 and it spins infinitely until i take the wire off that pin.... idk why this is.

BulldogLowell

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


your code will not do what you want it to do... let's read it out loud:

If the hour JUST CHANGED from 8 to 9 or from 20 to 21
if the minute JUST CHANGED from 56 to 57

on what clock can that happen?

I think you want it tr read something like:

if the current hour is 9 or 21 and the minute JUST CHANGED to 57

ergo

Code: [Select]
if((currentHour == 9 || currentHour == 21) && (lastMinute != currentMinute) && (currentMinute == 57))
{
  feedTime = millis();
  feedPet = true;
  lastHour = currentHour;
}


untested, naturally.

mbasile

#43
Nov 15, 2015, 04:24 am Last Edit: Nov 15, 2015, 04:27 am by mbasile
this is my code now, i made the new changes and it is the exact same result, nothing has changes results wise......


ALSO IT DOESNT MATTER IF I HAVE THE WIRE IN PIN 9 OR NOT THE RESULT IS THE SAME


Code: [Select]
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <TimeAlarms.h>
#include <Time.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 == 10 || currentHour == 22)) && (lastMinute != currentMinute) && (currentMinute == 23)) // if the hour just changed to 6 or 18


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

BulldogLowell

your turnFeeder() function is not at all correct and not at all like the one I posted and it will not do what you expect it to do.

FYI, the location of braces happens to be non-trivial.

Also, my advice... take this OUT OF YOUR loop() function:

Code: [Select]
  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(" ");


I know you don't get it but you are updating your LCD every few milliseconds... and you don't want that.

comment that out, fix your turnFeeder() function and try that.


Go Up