monty8604
Posts: 11
Joined: Tue May 10, 2016 1:50 am

LOOPING A PROGRAM (HELP)

Sat May 21, 2016 3:17 am

I have set up an RGB led and was looking into looping this basic program I found over the internet. I found a "While loop" statement to use but don't know how to tie it into my program. Any help on this is greatly appreciated! I would like the program to loop until I push a N/O push button (GPIO 26) closed.

Program:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(2,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)

GPIO.output(2,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(2,GPIO.LOW)

GPIO.output(3,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(3,GPIO.LOW)

GPIO.output(4,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(4,GPIO.LOW)
Last edited by monty8604 on Sat May 21, 2016 1:42 pm, edited 1 time in total.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: LOOPING A PROGRAM (HELP)

Sat May 21, 2016 3:26 am

monty8604 wrote:I have set up an RGB led and was looking into looping this basic program I found over the internet. I found a "While loop" statement to use but don't know how to tie it into my program. Any help on this is greatly appreciated! I would like the program to loop until I push a N/O push button (GPIO 26) closed.
You would then need to figure out a way to test when the button is pushed and put that test in your while loop. A while loop in python is something like the following, where SOMETHING is true while you continue the loop:

Code: Select all

while SOMETHING
   do something
   do something more
The indentation is what tells python it is part of the loop, so when you post here, you need to put that sort of code inside the code blocks as above.

monty8604
Posts: 11
Joined: Tue May 10, 2016 1:50 am

Re: LOOPING A PROGRAM (HELP)

Sat May 21, 2016 3:32 am

I'm new to programming and I know the syntex is wrong but this is something I was thinking.

Code: Select all

While button(26) = False: #logical zero
GPIO.output(2,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(2,GPIO.LOW)

GPIO.output(3,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(3,GPIO.LOW)

GPIO.output(4,GPIO.HIGH)
time.sleep(0.04)
GPIO.output(4,GPIO.LOW)
   If button(26) =True:  #logical 1
    End sub ()

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: LOOPING A PROGRAM (HELP)

Sat May 21, 2016 2:19 pm

Hi monty8604,

Perhaps this program will help you. It is based on a common anode RGB led.
If you have a common cathode led "google" is your friend.
Download the PDF "Introduction to Python Programming" from
https://nclab.com/python-resources/
it's a very good tutorial.
Don't forget to visit http://www.python.org

Success with your project and Programming!!


Code: Select all

#!/usr/bin/python
# 3 color led
# date: 21-05-2016
# Raspberry Pi Forum
#
#              !!!!Common anode RGB led!!!!
#
#              |----|>|---220ohm---  blue led to pin 15
#              |
#    +3.3V-----|----|>|---220ohm--- green led to pin 13
#    pin 1     |
#              |----|>|---220ohm---   red led to pin 11
#
#                                 /
#    push button to pin 37 -----o/  o------ to GND pin
#
#                |------        Blue
#        ....... |
#     .          |----------    Green
#    .  RGB LED  |
#     .          |------------- +3.3V
#        ........|
#                |-------       Red
#
# See for more info https://www.sparkfun.com/products/10820
#


import RPi.GPIO as GPIO
import time
import random

led_red = 17      #attention header pin 11 == GPIO17
led_green = 27    #attention header pin 13 == GPIO27
led_blue = 22     #attention header pin 15 == GPIO22
push_button = 26  #attention header pin 37 == GPIO26

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(led_red,GPIO.OUT)
GPIO.setup(led_green,GPIO.OUT)
GPIO.setup(led_blue,GPIO.OUT)
GPIO.setup(push_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(led_red,False)

loop=True
secondes=1

while loop:
    GPIO.output(led_red,False)
    GPIO.output(led_green,True)
    GPIO.output(led_blue,True)
    print "red"

    time.sleep(secondes)

    GPIO.output(led_red,True)
    GPIO.output(led_green,False)
    GPIO.output(led_blue,True)
    print "green"

    time.sleep(secondes)

    GPIO.output(led_red,True)
    GPIO.output(led_green,True)
    GPIO.output(led_blue,False)
    print "blue"

    time.sleep(secondes)

    if (GPIO.input(push_button) == 0) :
       loop = False
       print "Push Button ---CLOSED---"
    else:
       print "Push Button ----OPEN----"
Success with your project!
Ferdinand

monty8604
Posts: 11
Joined: Tue May 10, 2016 1:50 am

Re: LOOPING A PROGRAM (HELP)

Mon May 23, 2016 2:24 am

Ferdinand,
I had a chance to look over your code. It worked flawless! I made a few modifications to the code to incorporate 8 RGB LED's and had a little fun. I especially liked in the beginning of the code how you set labels on time. My previous code, I had to manually set the time on every LED time command and that took forever. I took most of the weekend to setup different sequences for the LED's and it was a fun project. I will be getting time with my daughter soon and would love to show her your code so she can start learning on this system. Thank you for your time!

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: LOOPING A PROGRAM (HELP)

Mon May 23, 2016 6:49 am

Hi monty8604,

It was a pleasure to help you and I wish you a good time with your daughter!!!
Success with your project!
Ferdinand

Return to “Python”