0

I am using LED's and I'm trying to make them go in a random order. I have tried many searches but they all are showing music related stuff which totally changes the code. Please tell me what to edit in this code for that to happen.

import RPi.GPIO as GPIO             ## Import GPIO Library
import time                         ## Import 'time' library (for 'sleep')

yellow = 17
green = 18
red = 22
blue = 4
## These are our LEDs
ourdelay = 0.1                      ## Delay
# pins 4,17,18,21,22,23,24,25

GPIO.setmode(GPIO.BCM)            ## Use BOARD pin numbering
GPIO.setup(yellow, GPIO.OUT)        ## set output
GPIO.setup(green, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
## function to save code

for num in range(1,30):
  print(num)
  GPIO.output(red, GPIO.HIGH )
  GPIO.output(yellow, GPIO.LOW)
  GPIO.output(green, GPIO.LOW)
  GPIO.output(blue, GPIO.LOW)
  ## set HIGH (LED ON)
  time.sleep(ourdelay)                ## wait
  GPIO.output(red, GPIO.LOW)
  GPIO.output(yellow, GPIO.HIGH)
  GPIO.output(green, GPIO.LOW)
  GPIO.output(blue, GPIO.LOW)
  time.sleep(ourdelay)                ## wait
  GPIO.output(red, GPIO.LOW)
  GPIO.output(yellow, GPIO.LOW)
  GPIO.output(green, GPIO.HIGH)
  GPIO.output(blue, GPIO.LOW)
  time.sleep(ourdelay)        ## wait
  GPIO.output(red, GPIO.LOW)
  GPIO.output(yellow, GPIO.LOW)
  GPIO.output(green, GPIO.LOW)
  GPIO.output(blue, GPIO.HIGH)
  time.sleep(ourdelay)        ## wait

  ##activateLED(blue,ourdelay)

GPIO.cleanup()                      ## close down library
  • 1
    Hello and welcome. While it is not an issue wrt the question, something to consider for future coding: per PEP0008 Use 4 spaces per indentation level. – Ghanima Mar 17 '16 at 0:46
1

I haven't quite gone so far as to hook up the LEDs, but I think that's going to look something like this:

import RPi.GPIO as GPIO             # Import GPIO Library
import time                         # Import 'time' library (for 'sleep')
import random                       # Import 'random' library 

# Delay
ourdelay = 0.1

# Use BOARD pin numbering
GPIO.setmode(GPIO.BCM)

# Create a dictionary of pin names and numbers
my_led_dict = {'red': 22, 'yellow': 17, 'green': 18, 'blue': 4}

# Set up every pin in our dictionary as an output
for led_pin_number in my_led_dict.values():
    GPIO.setup(led_pin_number, GPIO.OUT)

# Set up the random function with a seed value
random.seed()

for num in range(1,30):
    print(num)

    # Choose a random pin from our dictionary
    chosen_led = random.choice(my_led_dict.values())
    print("Selected pin is " + str(chosen_led))

    # Iterate over each value in our dictionary
    for led_pin_number in my_led_dict.values():
        if led_pin_number == chosen_led:
            GPIO.output(led_pin_number, GPIO.HIGH)
            print("Set pin number " + str(led_pin_number) + " to GPIO.HIGH")
        else:
            GPIO.output(led_pin_number, GPIO.LOW)
            print("Set pin number " + str(led_pin_number) + " to GPIO.LOW")        

    # Wait
    time.sleep(ourdelay)

# Close down library
GPIO.cleanup()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.