ducje
Posts: 2
Joined: Wed Mar 09, 2016 7:38 pm

Starter question rpi.gpio

Wed Mar 09, 2016 8:42 pm

I just bought my first raspberry pi 2 and wanted to learn some python, but wanted to have a goal.
So connected and rgb led strip and wrote a simple program which in a loop increases the value of the R, G and B led until it is 100 and then i should restart at 1 again:

For the first 40 - 60 steps there is output and then all leds are turned off?
Does somebody can shine a light at this issue for me?

Code: Select all

import RPi.GPIO as GPIO
import time

rood = 0
blauw = 0
groen = 0
frequentie = 0
s1 = 1 #sleep variable


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
red = 11
blue = 13
green = 15

GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
print GPIO.VERSION

x = 1
while x < 100:
 print "rood, blauw en groen op : ", x, ", frequentie op 100, delay = 5"
 RED = GPIO.PWM(red, 100)
 RED.start(x)
 #time.sleep (s1)
 GREEN = GPIO.PWM(green, 100)
 GREEN.start(x)
 #time.sleep (s1)
 BLUE = GPIO.PWM(blue, 100)
 BLUE.start(x)
 time.sleep(s1)
 x = x + 1
 if x == 99:
  x = 1

print("\n")
x = raw_input("eind rood, blue and green 0..100")

RED.stop()
GREEN.stop()
BLUE.stop()
GPIO.cleanup()
print "............einde gpio cleanup"

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

Re: Starter question rpi.gpio

Thu Mar 10, 2016 7:43 am

Please post your code in tags so that the code formatting (extremely important in Python code) is visible.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Starter question rpi.gpio

Thu Mar 10, 2016 1:43 pm

DirkS wrote:Please post your code in tags so that the code formatting (extremely important in Python code) is visible.
I've done the CODE bit.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Starter question rpi.gpio

Thu Mar 10, 2016 3:05 pm

Its not clear to me how this works at all. Yo seem to turn the LEDs on every time round the loop but not turn them off. Also there seems to be no way for the loop to exit.

One thing you should do is not create PWM objects each time round the loop. Lines like:
RED = GPIO.PWM(red, 100)

should be before the loop. Then you can start (and possibly stop) within the loop.

ducje
Posts: 2
Joined: Wed Mar 09, 2016 7:38 pm

Re: Starter question rpi.gpio

Thu Mar 10, 2016 6:34 pm

Sorry for not placing the code in the correct way..........first time, thanks for solving that!
Anyway also that there is no exit in the loop was also done on purpose.

Bottom line I think that creating the PWM object each time in the loop is the issue that it's stopping with the output.
I will see if I can find a way to solve it.

Return to “Troubleshooting”