YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Building a bird treat dispenser

Sat Jul 29, 2017 1:58 pm

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

DirkS
Posts: 10345
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Building a bird treat dispenser

Sat Jul 29, 2017 3:00 pm

'seq'' only has 7 elements, but you're using 8 (0 to 7)

User avatar
davidcoton
Posts: 4879
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Building a bird treat dispenser

Sat Jul 29, 2017 3:24 pm

+1. Element [0,0,0,1] is missing.
Signature retired

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a bird treat dispenser

Sat Jul 29, 2017 3:31 pm

That helped. Now the code runs without issues, but the motor doesnt move :(
Time to start debugging the GPIO pins...

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a bird treat dispenser

Sat Jul 29, 2017 6:18 pm

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?

DirkS
Posts: 10345
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Building a bird treat dispenser

Sat Jul 29, 2017 6:59 pm

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

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a bird treat dispenser

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?

User avatar
DougieLawson
Posts: 38784
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Building a bird treat dispenser

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a bird treat dispenser

Sat Jul 29, 2017 8:51 pm

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

YodaVonBeck
Posts: 38
Joined: Wed Mar 16, 2016 10:03 am

Re: Building a bird treat dispenser

Sun Jul 30, 2017 8:03 pm

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

Return to “Troubleshooting”