Skip to content
Snippets Groups Projects
BlinkenPi.ipynb 1.96 KiB
Newer Older
Holger Dinkel's avatar
Holger Dinkel committed
{
 "metadata": {
  "name": "BlinkenPi"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "Import the GPIO module and set the board layout:"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "import random\nimport time\nimport RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM)",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 25
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "Initialize the Pins:"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "red=27\nblue=18\nyellow=22\npins = [red,blue,yellow]\n\nGPIO.setup(red, GPIO.OUT)\nGPIO.setup(blue, GPIO.OUT)\nGPIO.setup(yellow, GPIO.OUT)\n\ndef turn_off():\n    for pin in pins:\n         GPIO.output(pin, False)\n\nturn_off()",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 26
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "Now switch the LEDs:"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "GPIO.output(red, True)\nGPIO.output(yellow, False)\nGPIO.output(blue, True)",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "for i in range(100):\n    pin = random.choice(pins)\n    mode = random.choice((True, False))\n    GPIO.output(pin, mode)\n    time.sleep(0.1)\nprint 'done'\nturn_off()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "done\n"
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 28
    }
   ],
   "metadata": {}
  }
 ]
}