Page 1 of 1

Building a bird treat dispenser

Posted: Sat Jul 29, 2017 1:58 pm
by YodaVonBeck
So we have recently gotten 2 parrots and they are very bright a needs to be challenged.

So I am building a system that will dispense a treat if they push a button.

I am using this setup as a start to run a step motor, but run into issues when I run the code. Its probably a simply thing, but am not experienced enough to judge what it is.

This is the code:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

ControlPin = [7,11,13,15]

for pin in ControlPin:
	GPIO.setup(pin,GPIO.OUT)
	GPIO.output(pin,0)

seq = [ [1,0,0,0],
	[1,1,0,0],
	[0,1,0,0],
	[0,1,1,0],
	[0,0,1,0],
	[0,0,1,1],
	[1,0,0,1] ]

for i in range(512):
	for halfstep in range(8):
		for pin in range(4):
			GPIO.output(ControlPin[pin], seq[halfstep][pin])
		time.sleep(0.001)

GPIO.cleanup()

And I get an error on line 23 saying the list index is out of range.

Do you guys have any suggestions on what the problem could be?

The code comes from this link : https://www.youtube.com/watch?v=Dc16mKFA7Fo&t=478s

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 3:00 pm
by DirkS
'seq'' only has 7 elements, but you're using 8 (0 to 7)

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 3:24 pm
by davidcoton
+1. Element [0,0,0,1] is missing.

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 3:31 pm
by YodaVonBeck
That helped. Now the code runs without issues, but the motor doesnt move :(
Time to start debugging the GPIO pins...

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 6:18 pm
by YodaVonBeck
OK, so I fixed the code. It seems to run its course.

I have checked that all the GPIOs can deliver current by testing with an LED.

Im using this motor setup and controller : http://hobbycomponents.com/motors-and-s ... iver-board
I have connected it this way :
IN1 to GPIO 7
IN2 to GPIO 11
IN3 to GPIO 13
IN4 to GPIO 15

It does not respond at all. According to the video I linked his code makes his motor turn.

Does anybody have experience with this setup?

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 6:59 pm
by DirkS
YodaVonBeck wrote:
Sat Jul 29, 2017 6:18 pm
I have connected it this way :
IN1 to GPIO 7
IN2 to GPIO 11
IN3 to GPIO 13
IN4 to GPIO 15
Just to make sure: do you mean physical pin number instead of BCM GPIO number in the list above?
In your code it's set to 'BOARD', which indicates that you're using physical pin 7, GPIO4 (see https://pinout.xyz/pinout/pin7_gpio4)
Your list above suggests you connected IN1 to GPIO7, physical pin 26

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 7:04 pm
by YodaVonBeck
Hi

Im attaching the wires to the GPIO numbers, not the pin numbers.
So if I change from BOARD to BCM it should work?

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 7:11 pm
by DougieLawson
YodaVonBeck wrote:
Sat Jul 29, 2017 7:04 pm
Hi

Im attaching the wires to the GPIO numbers, not the pin numbers.
So if I change from BOARD to BCM it should work?
You should stick with BCM numbers as that will cause fewer problems with other stuff you do on your RPi and other stuff you download from the internet.

Use https://pinout.xyz to map BCM numbers to board pins.

Re: Building a bird treat dispenser

Posted: Sat Jul 29, 2017 8:51 pm
by YodaVonBeck
DougieLawson wrote:
Sat Jul 29, 2017 7:11 pm
YodaVonBeck wrote:
Sat Jul 29, 2017 7:04 pm
Hi

Im attaching the wires to the GPIO numbers, not the pin numbers.
So if I change from BOARD to BCM it should work?
You should stick with BCM numbers as that will cause fewer problems with other stuff you do on your RPi and other stuff you download from the internet.

Use https://pinout.xyz to map BCM numbers to board pins.
Thanks. That oversight would explain why it didnt work :)
I have attached to pins according to BCM and the code uses BOARD.
Should be a simple fix :)

Re: Building a bird treat dispenser

Posted: Sun Jul 30, 2017 8:03 pm
by YodaVonBeck
Thanks a bunch. That fixed it :)

Now I have a setup a tactile button to start the sequence as well and some LEDs to indicate of the "machine" is operating or idle :)