Open 4.3Remote control.py and click .
'''
* Filename : Remote control
* Thonny : Thonny 4.1.4
* Auther : http//www.keyestudio.com
'''
from machine import Pin,PWM
import time
import utime
# set ir receiver pin to 11
ird = Pin(11,Pin.IN)
# set INA and INB to PWM pins
pwm_INA = PWM(Pin(14))
pwm_INB = PWM(Pin(15))
# set PWM frequency. The frequency depends on the motor application.
pwm_INA.freq(500)
pwm_INB.freq(500)
# Set the maximum and minimum rotation speed
max_duty = 65535
min_duty = 0
# Linear interpolation is used to calculate duty values for different speeds
def calc_duty(speed):
speed = int(speed * (max_duty - min_duty) / 100 + min_duty)
return speed
# change the rotation speed
def set_speed_INA(speed):
duty = calc_duty(speed)
pwm_INA.duty_u16(duty)
# change the rotation speed
def set_speed_INB(speed):
duty = calc_duty(speed)
pwm_INB.duty_u16(duty)
#set the inital key values of '1' '2' '3' on the remote control to 0
key1_press_num = 0;
key2_press_num = 0;
key3_press_num = 0;
act = {"1": "LLLLLLLLHHHHHHHHLHHLHLLLHLLHLHHH","2": "LLLLLLLLHHHHHHHHHLLHHLLLLHHLLHHH","3": "LLLLLLLLHHHHHHHHHLHHLLLLLHLLHHHH",
"4": "LLLLLLLLHHHHHHHHLLHHLLLLHHLLHHHH","5": "LLLLLLLLHHHHHHHHLLLHHLLLHHHLLHHH","6": "LLLLLLLLHHHHHHHHLHHHHLHLHLLLLHLH",
"7": "LLLLLLLLHHHHHHHHLLLHLLLLHHHLHHHH","8": "LLLLLLLLHHHHHHHHLLHHHLLLHHLLLHHH","9": "LLLLLLLLHHHHHHHHLHLHHLHLHLHLLHLH",
"0": "LLLLLLLLHHHHHHHHLHLLHLHLHLHHLHLH","Up": "LLLLLLLLHHHHHHHHLHHLLLHLHLLHHHLH","Down": "LLLLLLLLHHHHHHHHHLHLHLLLLHLHLHHH",
"Left": "LLLLLLLLHHHHHHHHLLHLLLHLHHLHHHLH","Right": "LLLLLLLLHHHHHHHHHHLLLLHLLLHHHHLH","Ok": "LLLLLLLLHHHHHHHHLLLLLLHLHHHHHHLH",
"*": "LLLLLLLLHHHHHHHHLHLLLLHLHLHHHHLH","#": "LLLLLLLLHHHHHHHHLHLHLLHLHLHLHHLH"}
# read the infrared signals and decode them
def read_ircode(ird):
wait = 1
complete = 0
seq0 = []
seq1 = []
while wait == 1:
if ird.value() == 0:
wait = 0
while wait == 0 and complete == 0:
start = utime.ticks_us()
while ird.value() == 0:
ms1 = utime.ticks_us()
diff = utime.ticks_diff(ms1,start)
seq0.append(diff)
while ird.value() == 1 and complete == 0:
ms2 = utime.ticks_us()
diff = utime.ticks_diff(ms2,ms1)
if diff > 10000:
complete = 1
seq1.append(diff)
code = ""
for val in seq1:
if val < 2000:
if val < 700:
code += "L"
else:
code += "H"
#print(code)
command = ""
for k,v in act.items():
if code == v:
command = k
if command == "":
command = code
return command
while True:
set_speed_INA(0) #INA zero out. In this experiment, we control the rotation speed only through INB
command = read_ircode(ird)
print(command, end = '')
if command == '1':
key1_press_num += 1
key2_press_num = 0
key3_press_num = 0
if key1_press_num % 2 == 1:
set_speed_INB(100) # High speed
print(' High speed')
else:
set_speed_INB(0) # fan stops
print(' off')
elif command == '2':
key2_press_num += 1
key1_press_num = 0
key3_press_num = 0
if key2_press_num % 2 == 1:
set_speed_INB(70) # Medium speed
print(' Medium speed')
else:
set_speed_INB(0) # fan stops
print(' off')
elif command == '3':
key3_press_num += 1
key1_press_num = 0
key2_press_num = 0
if key3_press_num % 2 == 1:
set_speed_INB(40) # Low speed
print(' Low speed')
else:
set_speed_INB(0) # fan stops
print(' off')
if command == '1':
if command equals the string 1.
Single quotes refer to string(s) rather than value(s).
Rather than value(s), the command returns the corresponding string(s) on the infrared remote control after decoding and conversion. Therefore, single quotes are required to referring that it is not a value but a string.
In other words, this code means if key 1 on the remote control is pressed.
Operator |
Description |
---|---|
% |
Modulo, return the remainder of division. |
if command == '1':
key1_press_num += 1
key2_press_num = 0
key3_press_num = 0
if key1_press_num % 2 == 1:
set_speed_INB(100) # High speed
print(' High speed')
else:
set_speed_INB(0) # fan stops
print(' off')
If key 1 on the remote control is pressed, the value of key1_press_num
adds 1, and key2_press_num
and key3_press_num
zero out.
Calculation: key1_press_num
is divided by 2 with a remainder of 1. So then the condition is met, and fan rotates at high speed.
Press the key 1 again, and the value of key1_press_num
will add 2, and the other two values zero out.
Calculation: key1_press_num
is divided by 2 with a remainder of 0. At this time, the condition is not satisfied, so the fan stops.
Similarly, pressing key 2 and 3 can also perform these functions. Press for the first time and the fan rotates at corresponding speed; press again and the fan stops.