Thiyraash
Posts: 8
Joined: Thu Feb 23, 2017 9:14 pm

How to create function?

Sat Feb 25, 2017 1:56 pm

Hi. My name is Raash and im newbie for python.. I want to know how to create function in python. Below shows the code that i want to put in function. I have tried to put but the function is not working. When i add the def FishFeeder() and return as shown in code below it does not work. But when i delete them and run it works. Can someone help about it?

Code: Select all

# Import required libraries
def FishFeeder():
    import sys
    import time
    import RPi.GPIO as GPIO

# Use BCM GPIO references
# instead of physical pin numbers
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
# Define GPIO signals to use
# Physical pins 11,15,16,18
# GPIO17,GPIO22,GPIO23,GPIO24
    StepPins = [17,22,23,24]

    KOKOI=0

# Set all pins as output
    for pin in StepPins:
      #print "Setup pins"
      GPIO.setup(pin,GPIO.OUT)
      GPIO.output(pin, False)

# Define advanced sequence
# as shown in manufacturers datasheet
    Seq = [[1,0,0,1],
           [1,0,0,0],
           [1,1,0,0],
           [0,1,0,0],
           [0,1,1,0],
           [0,0,1,0],
           [0,0,1,1],
           [0,0,0,1]]
       
    StepCount = len(Seq)
    StepDir = 1 # Set to 1 or 2 for clockwise
            # Set to -1 or -2 for anti-clockwise

# Read wait time from command line
#if len(sys.argv)>1:
 # WaitTime = int(sys.argv[1])/float(1000)
#else:
 # WaitTime = 10/float(1000)
    WaitTime = 0.0015
# Initialise variables
    StepCounter = 0

# Start main loop
#while KOKOI<4300:
    for KOKOI in range(0,4300):
 # print StepCounter,
 # print Seq[StepCounter]

      for pin in range(0, 4):
        xpin = StepPins[pin]
        if Seq[StepCounter][pin]!=0:
          #print " Enable GPIO %i" %(xpin)
          GPIO.output(xpin, True)
        else:
          GPIO.output(xpin, False)

      StepCounter += StepDir

  # If we reach the end of the sequence
  # start again
      if (StepCounter>=StepCount):
        StepCounter = 0
      if (StepCounter<0):
        StepCounter = StepCount+StepDir

      # Wait before moving on
      time.sleep(WaitTime)
      KOKOI+=1
      return;

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: How to create function?

Sat Feb 25, 2017 2:08 pm

You need to call the function. Functions don't run by themselves, they have to be called to tell them to run. Just add:
FishFeeder()
To the end, not inside the function.
You also don't need a return function and semicolons are not used in Python.
There are 10 types of people: those who understand binary and those who don't.

Thiyraash
Posts: 8
Joined: Thu Feb 23, 2017 9:14 pm

Re: How to create function?

Sat Feb 25, 2017 2:11 pm

Bro thanks alot! Its work! :D

Return to “Python”