#Include the library files
"""
IR remote codes:
===============
switch on/off - 162
MENU - 226
TEST - 34
Plus (+) - 2
Back - 194
left (|<<) - 224
play (>) - 168
right (>>|) - 144
zero (0) - 104
minus (-) - 152
C - 176
1 - 48
2 - 24
3 - 122
4 - 16
5 - 56
6 - 90
7 - 66
8 - 74
9 - 82
"""
import time
from machine import Pin, freq, PWM
import print_error
from nec import NEC_8
#Define the IR receiver pin and motor pins
pin_ir = Pin(12, Pin.IN)
ENA = PWM(Pin(2))
IN1 = Pin(3,Pin.OUT)
IN2 = Pin(4,Pin.OUT)
IN3 = Pin(5,Pin.OUT)
IN4 = Pin(6,Pin.OUT)
ENB = PWM(Pin(7))
# speed of this car
speed = 60000 # 0 - 65025
ENA.duty_u16(speed)
ENB.duty_u16(speed)
Up = 2
Down = 152
Left = 224
Right = 144
Stop = 162
def decodeKeyValue(data):
return data
# User callback
def callback(data, addr, ctrl):
if data < 0: # NEC protocol sends repeat codes.
pass
else:
print(data)
if data == Up:
forward()
print("UP")
elif data == Down:
backward()
print("DOWN")
elif data == Left:
left()
print("LEFT")
elif data == Right:
right()
print("RIGHT")
elif data == Stop:
stop()
print("STOP")
else:
print("Unknown")
def forward():
IN1.on()
IN2.off()
IN3.on()
IN4.off()
def backward():
IN1.off()
IN2.on()
IN3.off()
IN4.on()
def left():
IN1.on()
IN2.off()
IN3.off()
IN4.on()
def right():
IN1.off()
IN2.on()
IN3.on()
IN4.off()
def stop():
IN1.off()
IN2.off()
IN3.off()
IN4.off()
ir = NEC_8(pin_ir, callback) # Instantiate receiver
ir.error_function(print_error) # Show debug information
try:
while True:
pass
except KeyboardInterrupt:
ir.close()