Skip to content

Instantly share code, notes, and snippets.

@rhettallain
Created March 8, 2017 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rhettallain/9e5af0d08226f5348c82442d3cb35ad6 to your computer and use it in GitHub Desktop.
Save rhettallain/9e5af0d08226f5348c82442d3cb35ad6 to your computer and use it in GitHub Desktop.
This is the code to run on raspberry pi for an interactive pi-day poster
#!/usr/bin/python
# Example using a character LCD connected to a Raspberry Pi or BeagleBone Black.
import time
import Adafruit_CharLCD as LCD
import Adafruit_CharLCD as LCD
import turtle
import random
from math import sqrt
from gpiozero import Button
from gpiozero import Buzzer
# Raspberry Pi pin configuration:
lcd_rs = 27 # Note this might need to be changed to 21 for older revision Pi's.
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4
# BeagleBone Black configuration:
# lcd_rs = 'P8_8'
# lcd_en = 'P8_10'
# lcd_d4 = 'P8_18'
# lcd_d5 = 'P8_16'
# lcd_d6 = 'P8_14'
# lcd_d7 = 'P8_12'
# lcd_backlight = 'P8_7'
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Alternatively specify a 20x4 LCD.
# lcd_columns = 20
# lcd_rows = 4
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)
button=Button(19)
buzzer=Buzzer(12)
outcount=0
incount=0
tom=turtle.Turtle()
tom.speed(1000)
tom.penup()
n=0
while n<1000000:
r1=random.random()*400-200
r2=random.random()*400-200
tom.setpos(r1,r2)
rtemp=sqrt((r1+200)**2+(r2+200)**2)
buzzer.on()
if rtemp<400:
tom.pencolor('blue')
tom.dot()
incount=incount+1
else:
tom.pencolor('red')
tom.dot()
outcount=outcount+1
n=n+1
buzzer.off()
lcd.clear()
picalc=4.0*incount/(incount+outcount)
lcd.message('Pi = ')
lcd.message(str(picalc))
lcd.message('\n')
lcd.message('N = ')
lcd.message(str(n))
if button.is_pressed:
n=0
lcd.clear()
tom.reset()
incount=0
outcount=0
tom.penup()
tom.speed(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment