-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwm.py
executable file
·51 lines (45 loc) · 1.03 KB
/
pwm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
import time
import math
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
# 11 = rouge
# 13 = vert
# 15 = bleu
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
r = GPIO.PWM(11, 100) # channel=11 frequency=100Hz
g = GPIO.PWM(13, 100) # channel=13 frequency=100Hz
b = GPIO.PWM(15, 100) # channel=15 frequency=100Hz
r.start(0)
g.start(0)
b.start(0)
smooth = 20
def calcLog(v):
# return v
return 100-math.floor(100*(math.log(101+smooth-v)-math.log(smooth))/(math.log(101+smooth)-math.log(smooth)))
def setRed(v):
r.ChangeDutyCycle(calcLog(v))
def setGreen(v):
g.ChangeDutyCycle(calcLog(v))
def setBlue(v):
b.ChangeDutyCycle(calcLog(v))
try:
while 1:
for dc in range(0, 101, 1):
setRed(dc)
setGreen(dc)
setBlue(dc)
time.sleep(0.1)
for dc in range(100, -1, -1):
setRed(dc)
setGreen(dc)
setBlue(dc)
time.sleep(0.1)
except KeyboardInterrupt:
pass
r.stop()
g.stop()
b.stop()
GPIO.cleanup()