This forum is archived, go to the new forum!

This is the old fritzing discussion forum. Search it for valuable information from 2009 to 2015.

processing - Nachtlicht mit fermata, Raspberry Pi und python

schlumpf 4 years, 8 months ago

Für den raspberrry Pi gibt es leider keine vorkompilierten processing Packete.

 

So habe ich das Nachtlicht mit Fermata und python nachgebaut.

 

Als erstes braucht man einige packete:

  • sudo apt-get install python-pip python-serial
  •  sudo pip install pyfirmata

Dann kann gestartet werden.

 

Zuerste ein kleines Testprogramm - zum testen der Verbindung und zum finden der passenden console:

"" This file is a basic program, testing if the firmata connection
   between Python and arduino works

   it assumes the RGB pins connected to pin 9, 10 and 11.

   And the Arduino serial Interface to be connected to '/dev/ttyACM0'

   If your setup is different you will need to adjust the variables at the start
   of the program.

""


from pyfirmata import Arduino, util
import time


# Module level constants
RED   = 'd:9:p'
GREEN = 'd:10:p'
BLUE  = 'd:11:p'

TTY   = '/dev/ttyACM0'

try:
  board = Arduino(TTY)
  pinRed = board.get_pin(RED)
  pinRed.write(1)
  print "rot"

  time.sleep(10);
  pinBlue = board.get_pin(BLUE)
  pinRed.write(0)
  pinBlue.write(1)
  print "blau"
  time.sleep(10);

except:
    print "Unexpected error:", sys.exc_info()[0]
    raise  
finally:
    print "fertig"


---------------------------------------------------------------------------

Wenn das läuft auf zum fergesteuerten Nachtlicht:

""" This file is a basic program, testing if the firmata connection
   between Python and arduino works

   it assumes the RGB pins connected to pin 9, 10 and 11.

   And the Arduino serial Interface to be connected to '/dev/ttyACM0'

   If your setup is different you will need to adjust the variables at the start
   of the program.

"""


from pyfirmata import Arduino, util
import time
import sys


# Module level constants
RED   = 'd:9:p'
GREEN = 'd:10:p'
BLUE  = 'd:11:p'

TTY   = '/dev/ttyACM0'

WAIT = 0.25
try:

    def loop():
        """ The worker function
    This function loops over and changes the colors or the LEDs

    The old Nightlight had 3 sweet spots keeping one color for a longer time.
    this version continously changes the Color Red->Blue->Green

    The speed can be adjusted with the WAIT constanct meassuring the time in seconds
    the current setting shall be lit. A full cycle takes 300 of these time units.
        """
        # Red -> Blue
        for  i in range (0, 100):
            pinRed.write(0.01*(100-i))
            pinBlue.write(0.01*i)
            time.sleep(WAIT)
        # Blue -> Green
        for  i in range (0, 100):
            pinBlue.write(0.01*(100-i))
            pinGreen.write(0.01*i)
            time.sleep(WAIT)
        # Green -> Red
        for  i in range (0, 100):
            pinGreen.write(0.01*(100-i))
            pinRed.write(0.01*i)
            time.sleep(WAIT)
            

    if __name__ == '__main__':
        # connect the board
        board = Arduino(TTY)
        # store the pins as variables
        pinRed = board.get_pin(RED)
        pinBlue = board.get_pin(BLUE)
        pinGreen = board.get_pin(GREEN)
        # define a starting color - even though this is not neccessary
        # after setup all pins are off and red is the first color in the loop.
        pinRed.write(1)
        pinBlue.write(0)
        pinGreen.write(0)

        while True:
            loop()
             
except KeyboardInterrupt:
    print "Loop interrupted by keyboard"
    raise
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise  
finally:
    print "fertig"