Hello, here is the code i’m using:
encoder comes from
# Class to monitor a rotary encoder and update a value. You can either read the value when you need it, by calling getValue(), or
# you can configure a callback which will be called whenever the value changes.
import RPi.GPIO as GPIO
class Encoder:
def __init__(self, leftPin, rightPin, callback=None):
self.leftPin = leftPin
self.rightPin = rightPin
self.value = 0
self.state = '00'
self.direction = None
self.callback = callback
GPIO.setup(self.leftPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.rightPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.leftPin, GPIO.BOTH, callback=self.transitionOccurred)
GPIO.add_event_detect(self.rightPin, GPIO.BOTH, callback=self.transitionOccurred)
def transitionOccurred(self, channel):
This file has been truncated. show original
recorder comes from
recorder.py
# -*- coding: utf-8 -*-
'''recorder.py
Provides WAV recording functionality via two approaches:
Blocking mode (record for a set duration):
>>> rec = Recorder(channels=2)
>>> with rec.open('blocking.wav', 'wb') as recfile:
... recfile.record(duration=5.0)
Non-blocking mode (start and stop recording):
This file has been truncated. show original
lcd driver comes from https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
when i replace 23 and 24 in e1 = Encoder(24, 23, intensity) by 17 and 18. The Ultra+ stops working
import picamera
import datetime
import time
import RPi.GPIO as GPIO
from encoder import Encoder
from recorder import Recorder
import I2C_LCD_driver
import ffmpeg
import os
LED_1 = 12
LED_2 = 13
LED_3 = 5
LED_4 = 6
PHOTO = 15
CAMERA = 16
brightness = 0
cold = 0
warm = 100
record = 0
mylcd = I2C_LCD_driver.lcd()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(CAMERA,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(PHOTO,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(LED_1,GPIO.OUT)
GPIO.setup(LED_2,GPIO.OUT)
GPIO.setup(LED_3,GPIO.OUT)
GPIO.setup(LED_4,GPIO.OUT)
pwm1 = GPIO.PWM(LED_1,1000)
pwm2 = GPIO.PWM(LED_2,1000)
pwm1.start(0)
pwm2.start(0)
mylcd.lcd_display_string(" Justine “,1)
mylcd.lcd_display_string(” Heyde ",2)
time.sleep(2)
mylcd.lcd_clear()
time.sleep(0.3)
GPIO.output(LED_3, GPIO.HIGH)
def intensity(brightness):
brightness = e1.getValue()
cold = e2.getValue()
warm = 100 - cold
pwm1.ChangeDutyCycle(warm*brightness/100)
pwm2.ChangeDutyCycle(cold*brightness/100)
#print("Intensity : {}".format(brightness))
print("Intensity : " , brightness)
mylcd.lcd_display_string("Intensity : " + str(brightness) + " % ",1)
def balance(cold):
cold = e2.getValue()
brightness = e1.getValue()
warm = 100 - cold
pwm1.ChangeDutyCycle(warm*brightness/100)
pwm2.ChangeDutyCycle(cold*brightness/100)
print("Balance : ",(cold-0) * (6500 - 2700) // (100 - 0) + 2700)
mylcd.lcd_display_string("Kelvin : " + str(((cold-0) * (6500 - 2700) // (100 - 0) + 2700)) + " K ",2)
e1 = Encoder(24, 23, intensity)
e2 = Encoder(27, 22, balance)
def camera():
global record
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
camera.framerate = 25
now = datetime.datetime.now()
timestamp = now.strftime("%d_%m_%y\r%Hh%Mm%Ss")
if GPIO.input(CAMERA) == 0 :
record = 1
rec = Recorder(channels=2)
recfile = rec.open("/home/pi/Videos/" + str(timestamp) + ".wav", "wb")
recfile.start_recording()
print ("Recording")
GPIO.output(LED_4, GPIO.HIGH)
GPIO.output(LED_3, GPIO.LOW)
camera.start_preview()
camera.start_recording('/home/pi/Videos/' + str(timestamp) + '.h264')
time.sleep(.5)
while record == 1:
camera.wait_recording(.01)
if GPIO.input(CAMERA) == 0:
print ("Stopped")
recfile.stop_recording()
recfile.close()
camera.stop_recording()
camera.stop_preview()
in1 = ffmpeg.input('/home/pi/Videos/' + str(timestamp) + '.h264')
in2 = ffmpeg.input('/home/pi/Videos/' + str(timestamp) + '.wav')
v1 = in1.video
a2 = in2.audio
out = ffmpeg.output(v1, a2, '/home/pi/Videos/' + str(timestamp) + '.mp4')
out.run()
os.remove('/home/pi/Videos/' + str(timestamp) + '.h264')
os.remove('/home/pi/Videos/' + str(timestamp) + '.wav')
GPIO.output(LED_4, GPIO.LOW)
GPIO.output(LED_3, GPIO.HIGH)
record = 0
while GPIO.input(CAMERA) == 0:
time.sleep(0.1)
time.sleep(.5)
if GPIO.input(PHOTO) == 0 :
mylcd.lcd_clear()
mylcd.lcd_display_string(" mode photo ",1)
mylcd.lcd_display_string(" 3 . . ",2)
time.sleep(1)
mylcd.lcd_display_string(" 3 2 . ",2)
time.sleep(1)
mylcd.lcd_display_string(" 3 2 1 ",2)
time.sleep(0.5)
image_path = '/home/pi/Pictures/' + str(timestamp) + '.jpeg'
GPIO.output(LED_4, GPIO.HIGH)
GPIO.output(LED_3, GPIO.LOW)
mylcd.lcd_clear()
mylcd.lcd_display_string(" photo ",1)
mylcd.lcd_display_string(" prise ",2)
camera.capture(image_path)
print("Took photo")
time.sleep(.5)
GPIO.output(LED_4, GPIO.LOW)
GPIO.output(LED_3, GPIO.HIGH)
cold = e2.getValue()
brightness = e1.getValue()
mylcd.lcd_clear()
mylcd.lcd_display_string("Intensity : " + str(brightness) + " % ",1)
mylcd.lcd_display_string("Kelvin : " + str(((cold-0) * (6500 - 2700) // (100 - 0) + 2700)) + " K ",2)
try:
mylcd.lcd_display_string("Intensity : " + str(brightness) + " % ",1)
mylcd.lcd_display_string("Kelvin : " + str(((cold-0) * (6500 - 2700) // (100 - 0) + 2700)) + " K ",2)
while True:
time.sleep(0.1)
camera()
except KeyboardInterrupt:
print (‘Interrupted’)
GPIO.cleanup()
mylcd.lcd_clear()
finally :
GPIO.cleanup()
mylcd.lcd_clear()