Skip to content
Snippets Groups Projects

Example Raspberry Pi

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Rasmus Ringdahl
    Edited
    led_sound_test.py 1.28 KiB
    import RPi.GPIO as GPIO
    import random
    import time
    import os
    
    class Sunshine:
    
    
      def __init__(self, red, green, blue):
        self._blue = blue
        self._green = green
        self._red = red
        
        # Setup
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self._blue,GPIO.OUT)
        GPIO.setup(self._green, GPIO.OUT)
        GPIO.setup(self._red, GPIO.OUT)
    
      def red(self,state):
        self._change_state(self._red,state)
    
      def green(self,state):
        self._change_state(self._green,state)
    
      def blue(self,state):
        self._change_state(self._blue,state)
    
      def _change_state(self,color, state):
        GPIO.output(color,state)
    
    class Sun:
      def __init__(self, pin):
        self.pin = pin
        GPIO.setup(self.pin,GPIO.OUT)
        
    if __name__ == "__main__":
      red = 5
      blue = 13
      green = 6
      pin = 10
      sunshine = Sunshine(red, green, blue)
    
      sound = ['2059.wav','226.wav','600.wav']
      color_function = [[sunshine.red], [sunshine.green], [sunshine.red, sunshine.green]]
      sequence = [2, 2, 2, 0, 0, 1]
    
      for i in sequence:
        choice = i
        color = color_function[choice]
        chosen_sound = sound[choice]
        for c in color:
          c(True)
        print("playing sound {}".format(chosen_sound))
        os.system('aplay {}'.format(chosen_sound))
        time.sleep(0.2)
    
        for c in color:
          c(False)
        time.sleep(.5)
      GPIO.cleanup()
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment