Degančios kelnės - Burning Pants - micro:bit Neopixel projektas

micro:bit
technarium
microbit

#1

Tikslas

Internet of Shit hackatono metu sugalvota padaryti “Degančias kelnes”, kurios

  • stovint “dega”
  • judant “dega” dar labiau

Dalys

Projekte naudojama:

Jungimo schema

Schema informacinio pobūdžio. LED juostos PIN’ai gali skirtis, naudojama ličio jonų baterija kitokiam korpuse, pakrovimo modulis kitoks. Bet esmė turėtų būti aiški.

Eiga ir testai

Trumpa prezentacija

Ugnies efektas micro:bit matricoje

Sujudinus “liepsna” stipresnė

Python programos kodas

# fire fx https://github.com/FastLED/FastLED/blob/master/examples/Fire2012WithPalette/Fire2012WithPalette.ino
# color heat map http://stackoverflow.com/a/20793850/3568597
# http://blog.withcode.uk/2016/06/microbit-python-tutorial-shake-n-burn-fire-simulator/6/

from random import randint
from microbit import pin0, accelerometer, display, Image, button_a
import neopixel

# ----------- STRIP CLASS
class led_strip():
	def __init__(self, num_leds = 16):
		self.num_leds = num_leds
		self.neopixels = neopixel.NeoPixel(pin0, num_leds)
	
	def show(self):
		self.neopixels.show()

	def set(self, led_index = 5, rgb_color = (255, 0, 0)):
		r, g, b = rgb_color
		self.neopixels[led_index] = (r, g, b)



def convert_to_rgb(minval, maxval, val, colors):
	max_index = len(colors)-1
	v = float(val-minval) / float(maxval-minval) * max_index
	i1, i2 = int(v), min(int(v)+1, max_index)
	(r1, g1, b1), (r2, g2, b2) = colors[i1], colors[i2]
	f = v - i1
	return int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))



# --------- FIRE CLASS

# COOLING: How much does the air cool as it rises?
# Less cooling = taller flames.  More cooling = shorter flames.
# Default 55, suggested range 20-100 
# SPARKING: What chance (out of 255) is there that a new spark will be lit?
# Higher chance = more roaring fire.  Lower chance = more flickery fire.
# Default 120, suggested range 50-200.

class fire_class():
	def __init__(self, num_leds = 30, cooling = 75, sparking = 50):
		
		self.heat = bytearray(num_leds)
		self.num_leds = num_leds
		
		self.cooling = cooling
		self.sparking = sparking
		
		self.led = led_strip(num_leds = 60)
		
		self.colors_r = bytearray(256)
		self.colors_g = bytearray(256)
		self.colors_b = bytearray(256)
		for i in range(0, 256):
			self.colors_r[i], self.colors_g[i], self.colors_b[i]  = convert_to_rgb(0, 255, i, [(0, 0, 0), (255, 0, 0), (255, 125, 0), (255, 255, 0)])
		
	def run(self, sparking):
		self.sparking = sparking
		
		# Step 1.  Cool down every cell a little
		for i in range(0, self.num_leds):
			cooldown = randint(0, ((self.cooling * 10) // self.num_leds) + 2)
			if cooldown > self.heat[i]:
				self.heat[i] = 0
			else:
				self.heat[i] = self.heat[i] - cooldown
			
		# Step 2.  Heat from each cell drifts 'up' and diffuses a little
		for i in range(self.num_leds-1, 0, -1):
			self.heat[i] = (self.heat[i - 1] + self.heat[max(i - 2, 0)]) // 2
		
		# Step 3.  Randomly ignite new 'sparks' near the bottom
		if randint(0, 255) < self.sparking:
			y = 0 + randint(0, 2)
			self.heat[y] = min( 255, self.heat[y] + randint(160, 255) )
		
		# Step 4.  Convert heat to LED colors
		for i in range(0, self.num_leds):
			self.set_pixel_heat(i, self.heat[i])
			
		self.led.show()
	
	def set_pixel_heat(self, pixel_id, temperature):
		self.led.set(pixel_id*2, [self.colors_r[temperature], self.colors_g[temperature], self.colors_b[temperature]] )
		self.led.set(pixel_id*2+1, [self.colors_r[temperature], self.colors_g[temperature], self.colors_b[temperature]] )
		# self.led.set(pixel_id, [self.colors_r[temperature], self.colors_g[temperature], self.colors_b[temperature]] )




# ---- MAIN LOOP

# create an empty image
i = Image("00000:" * 5)

# start with the fire at medium intensity
intensity = 0.5

fire = fire_class()
while True:
	display.show(i)

	gesture = accelerometer.current_gesture()
	if gesture == "shake" or button_a.is_pressed():
		# display.show(Image.ANGRY)
		fire.run(100)
		intensity = 1
	else:
		# display.show(Image.HAPPY)
		fire.run(20)
	
	
	# shift the image up and fade it slightly
	i = i.shift_up(1) * intensity
	# let the fire burn down a little (reduce the intensity)
	intensity *= 0.98
	# choose random brightness for bottom row of fire
	for x in range(5):
		i.set_pixel(x, 4, randint(0,9))

Ateičiai

1. micro:bit ir kitų modulių tvirtinimas ant diržo