Skip to content

Instantly share code, notes, and snippets.

@KramKroc
KramKroc / traffic_lights.py
Last active December 28, 2015 22:56
Lights with gpio
from gpiozero import LED, Button
from time import sleep
red_led = LED(4)
yellow_led = LED(17)
blue_led = LED(23)
button = Button(25)
yellow_led.blink()
@KramKroc
KramKroc / minecraft_led.py
Last active December 30, 2015 22:34
How to control an led from minecraft
from mcpi.minecraft import Minecraft
from gpiozero import LED
mc = Minecraft.create()
led = LED(4)
# blocks
redstone_ore = 73
while True:
@KramKroc
KramKroc / mibecraft_teleport.py
Created December 30, 2015 23:38
Teleport player using a button
from mcpi.minecraft import Minecraft
from time import sleep
from gpiozero import LED, Button
mc = Minecraft.create()
led = LED(4)
button = Button(25)
start = mc.player.getTilePos()
while True:
@KramKroc
KramKroc / minecraft_rgbled.py
Created December 31, 2015 23:43
Control color of RGB LED based on what player is standing on
from mcpi.minecraft import Minecraft
from time import sleep
from gpiozero import RGBLED
mc = Minecraft.create()
rgbled = RGBLED(26,19,13)
# block
grass = 2
ice = 9
@KramKroc
KramKroc / mission_zero.py
Created October 14, 2017 08:10
Code for Mission Zero, displaying CoderDojo Banbridge logo
from sense_hat import SenseHat
from time import sleep
sense = SenseHat()
sense.set_rotation(270)
sense.show_message("Hello from Coderdojo Banbridge!", 0.05)
r = (255,0,0)
g = (0,255,0)
@KramKroc
KramKroc / where_is_iss.py
Last active November 14, 2017 21:00
Get details on ISS
import ephem
import time
iss = ephem.readtle("ISS (ZARYA)",
"1 25544U 98067A 17317.62840120 .00004117 00000-0 69389-4 0 9994",
"2 25544 51.6432 13.3344 0004461 113.6331 45.1344 15.54138509 84996")
while True:
iss.compute(ephem.now())
print("Current position: %s, %s In darkness: %s" % (iss.sublat / ephem.degree, iss.sublong / ephem.degree, iss.eclipsed))
@KramKroc
KramKroc / picameriapillowmean.py
Created December 10, 2017 21:43
Using the Pillow module to calculate average brightness of an image
import io
import time
import picamera
from PIL import Image
from PIL import ImageStat
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
@KramKroc
KramKroc / writing_with_csv.py
Created January 26, 2018 22:39
Short example on how to use csv library to write to a file
import csv
import datetime as dt
from random import randint
from time import sleep
time_format = "%Y-%m-%d-%H-%M-%S"
with open('/home/pi/results.csv', 'w') as csvfile: # open the file for writing
fieldnames = ['time', 'lat', 'long', 'photo', 'lux'] # the different elements we store in each row
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) # create writer with fields
@KramKroc
KramKroc / reading_from_csv.py
Created January 26, 2018 22:40
Short example showing how to read a csv file
import csv
from time import sleep
with open('/home/pi/results.csv') as csvfile:
fieldnames = ['time', 'lat', 'long', 'photo', 'lux'] # Same fields we wrote to the file
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
next(reader) # this lets us skip the header row
for row in reader:
print(row['lux']) # here we are just interested in the lux column
@KramKroc
KramKroc / result.csv
Created January 26, 2018 22:42
Example of file create using csv library
time lat long photo lux
2018-01-26-22-06-35 98 75 photoloc 199
2018-01-26-22-06-40 14 90 photoloc 211
2018-01-26-22-06-45 70 37 photoloc 59
2018-01-26-22-06-50 73 75 photoloc 82
2018-01-26-22-06-55 11 95 photoloc 219
2018-01-26-22-07-00 12 61 photoloc 145
2018-01-26-22-07-05 12 91 photoloc 274
2018-01-26-22-07-10 39 49 photoloc 72
2018-01-26-22-07-15 46 75 photoloc 199