ali noble
ali noble

Reputation: 13

Using a set time to trigger a motor function

I'm making and automatic dog feeder and i want the user to be able to set a preset time and when it is that time, the device will dispense food via a motor function.

this is the code (60 Lines):

import RPi.GPIO as GPIO

import time

import datetime


PIN_MOTOR = 14

PIN_LIMITSENSOR = 15


GPIO.setmode(GPIO.BCM)

GPIO.setup(PIN_MOTOR, GPIO.OUT)

GPIO.setup(PIN_LIMITSENSOR, GPIO.IN, GPIO.PUD_DOWN)

GPIO.output(PIN_MOTOR, GPIO.LOW)


def doCycle():

    GPIO.output(PIN_MOTOR, GPIO.HIGH)

    while not GPIO.input(PIN_LIMITSENSOR):

        time.sleep(0.2)

    GPIO.output(PIN_MOTOR, GPIO.LOW)

    time.sleep(3)

def hour():

    return int(datetime.now().hour)


def minute():

    return int(datetime.now().minute)


option = raw_input("Would you like manual or automatic mode [manual/auto]?: ")

if option == "manual":

    while True:

        selection = str(raw_input("How big is your dog (small/medium/large/exit)?: "))

        if selection == "small":

            doCycle()

        elif selection == "medium":

            doCycle()

            doCycle()

        elif selection == "large":

            doCycle()

            doCycle()

            doCycle()

        elif selection == "exit":

            break

        else:

            print("Invalid selection")

else:
    print("Automatic mode selected.")

    schedhr = int(raw_input("Please enter the hour to schedule: "))

    schedmin = int(raw_input("Please enter the minute to schedule: "))

    iterations = 1

    selection = str(raw_input("How big is your dog (small/medium/large)?: "))

    if selection == "small":

        iterations = 1

    elif selection == "medium":

        iterations = 2

    elif selection == "large":

        iterations = 3

    print("Now scheduled to do " + str(iterations) + "cycles at "+str(schedhr)+":"+str(schedmin))

    while (hour() != schedhr) or (schedmin != minute()):

        time.sleep(1)

    for x in xrange(iterations):

        print ("Doing cycle.")

        doCycle()

and this is the error message:

Automatic mode selected.

Please enter the hour to schedule: 19

Please enter the minute to schedule: 00

How big is your dog (small/medium/large)?: small

Now scheduled to do 1cycles at 19:0

Traceback (most recent call last):

File "code4.py", line 59, in

while (hour() != schedhr) or (schedmin != minute()):

File "code4.py", line 22, in hour

return int(datetime.now().hour)

AttributeError: 'module' object has no attribute 'now'

Upvotes: 1

Views: 275

Answers (1)

Alex Riley
Alex Riley

Reputation: 176800

The datetime module contains the identically-named class datetime, of which now() is a class method.

Therefore to call now() and return a datetime instance, in line 22 you need to write:

datetime.datetime.now().hour 

Alternatively, if you're just calling methods from the datetime class, it's common to write from datetime import datetime instead of importing only the module name (then you needn't change line 22).

Upvotes: 2

Related Questions