this is my first post in this forum and I start with a complex theme. (At least for me)
I'd like to record a Video with the PiCam and simultaniously control the output of Adafruit NeoPixls. It is for an experiment at the university. It seems to work more or less as expected. Looking at the recorded video the red light is turned on faster after the recording has stated compared to the blue one. I don't know why.
I now that this is not the best way to program but I'm not that familiar with programming at all. So I wanted to ask for some advice what I could do better.
Thanks for reading.
This is my current code:
Code: Select all
# Imports
import threading
import os
from neopixel import *
from picamera import PiCamera
from time import sleep, time
# Functions
# just checking if there is already a file with that name
def check_for_file(fps):
File_Exist = True
i = 1
try:
while File_Exist == True:
if (os.path.exists('video_640-480_' + str(fps) + 'fps_' + str(i) + '.h264')):
i = i+1
else:
name = 'video_640-480_' + str(fps) + 'fps_' + str(i) + '.h264'
File_Exist = False
except:
print "Error: checking for existing file"
return(name)
def Record_Video(fps, recordtime):
# initialize PiCam
camera = PiCamera()
# start videopreview
camera.resolution = (640, 480)
camera.framerate = fps
camera.start_preview(fullscreen = False, window =(50, 50, 320, 240)) # Preview disabled while without Touchscreen
name = check_for_file(fps)
print 'start recording ' + str(time()) # just an info for me
camera.start_recording(name)
sleep(recordtime)
camera.stop_recording()
print 'stop recording ' + str(time()) # just an info for me
camera.stop_preview() # Preview disabled while without Touchscreen
camera.close()
return(True)
# Functions concerning LED control
def LED_RING(lightOn, GreenIntens, BlueIntens, RedIntens): # time light is set on; Intensity of color 0 to 255 approx. 0,39% per step
# LED Configuration
LED_COUNT = 16 # Number of LEDs
LED_PIN = 18 # Pin the LED_IN is attached to
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating ignal (try 5)
LED_INVERT = False # True to invert signal (when using NPN transistor level shift)
# Create NeoPixel object with appropriate configuration
ring = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT)
# Initialize the library (must be called once before other functions)
ring.begin()
# start sequence
for i in range(ring.numPixels()):
ring.setPixelColorRGB(i, GreenIntens, BlueIntens, RedIntens) # Set all pixel to red (or whatever color)
ring.show() # Important to actually change the color
print 'Light is on ' + str(time()) # just an info for me
sleep(lightOn)
for i in range(ring.numPixels()):
ring.setPixelColorRGB(i, 0, 0, 0) # Set all pixels dark
ring.show()
print 'Light is off ' + str(time()) # just an info for me
return(True)
# --- Main ---
# Variables
fps = 60
lightOn = 3
afterDark = 5 # Time after light on
GreenIntens = 0
RedIntens = 50
BlueIntens = 0
recordtime = lightOn + afterDark
Round = 1
print 'Red'
while Round <= 3:
t1 = threading.Thread(target=Record_Video, args=(fps, recordtime))
t2 = threading.Thread(target=LED_RING, args=(lightOn, GreenIntens, RedIntens, BlueIntens))
t1.start()
sleep(0.5)
t2.start()
t1.join()
t2.join()
print 'Round: ' + str(Round) # just an info for me
Round += 1
RedIntens = 0
BlueIntens = 50
Round = 1
print 'Blue'
while Round <= 3:
t1 = threading.Thread(target=Record_Video, args=(fps, recordtime))
t2 = threading.Thread(target=LED_RING, args=(lightOn, GreenIntens, RedIntens, BlueIntens))
t1.start()
sleep(1)
t2.start()
t1.join()
t2.join()
print 'Round: ' + str(Round) # just an info for me
Round += 1
print 'Close Main' # just an infor for me