Welcome to Roboduino V2 repository
05.Car Infrared Remote
Car Infrared Remote
Car Infrared RemoteDevice connectionHardware connectionSoftware connectionImplementation ideasCode AnalysisExperimental resultsNotes
Control RGB, buzzer and car motion status through infrared remote control.
xxxxxxxxxxSince the previous tutorial has explained the basic knowledge of all car modules, the car control tutorial will not repeat it, and mainly explain the ideas for implementing car functions!
Device connection
Hardware connection
Use Type-B data cable to connect Arduino Uno and computer.
Software connection
Open the "Arduino IDE" software and select the model and serial port number corresponding to the development board.
Implementation ideas
Use the infrared remote control with the infrared receiver to parse the data, and then control the corresponding peripheral status of the car according to the data.
| IR remote control | Parsed data | Function |
|---|---|---|
| Power | 0x00 | Stop all peripheral functions |
| RGB | 0x40 | Switch RGB colors |
| Buzzer | 0xA0 | Control buzzer sound |
| Car Forward | 0x80 | Car forward |
| Car Backward | 0x90 | Car backward |
| Car Left | 0x20 | Car moves to the left |
| Car Right | 0x60 | Car moves to the right |
| Car Left Spin | 0x10 | Car rotates to the left |
| Car Right Spin | 0x50 | Car rotates to the right |
| Add | 0x30 | Increase speed |
| Sub | 0x70 | Decrease speed |
| Number 0 | 0xB0 | None |
| Number 1 | 0x08 | None |
| Number 2 | 0x88 | None |
| Number 3 | 0x48 | None |
| Number 4 | 0x28 | None |
| Number 5 | 0xA8 | None |
| Number 6 | 0x68 | None |
| Number 7 | 0x18 | None |
| Number 8 | 0x98 | None |
| Number 9 | 0x58 | None |
Code Analysis
Here is only a brief introduction to the code content. For detailed code, please refer to the corresponding code file, which is provided in the download area!
Include
Wire、IRremote、Adafruit_NeoPixel、Adafruit_PWMServoDriverlibrary
xxxxxxxxxx#include <Wire.h> // 包含Wire(I2C)通讯库 Include Wire library#include <IRremote.hpp> // 包含IRremote库 Include IRremote library#include <Adafruit_NeoPixel.h> // 包含Adafruit NeoPixel库 Include Adafruit NeoPixel library#include <Adafruit_PWMServoDriver.h> // 包含Adafruit PWMServoDriver库 Include Adafruit PWMServoDriver library
Define motor, infrared, RGB, buzzer control pins, I2C address, motor frequency and initial speed, etc.
x// 定义红外接收引脚 Define IR receiver pins#define IR_RECEIVE_PIN A3// 定义电机控制引脚 Define motor control pins#define Motor_L1_F_PIN 11 // 控制小车左前方电机前进 Control the motor on the left front of the car#define Motor_L1_B_PIN 10 // 控制小车左前方电机后退 Control the motor back on the left front of the car#define Motor_L2_F_PIN 8 // 控制小车左后方电机前进 Control car left rear motor forward#define Motor_L2_B_PIN 9 // 控制小车左后方电机后退 Control the car left rear motor back#define Motor_R1_F_PIN 13 // 控制小车右前方电机前进 Control the right front motor of the car to move forward#define Motor_R1_B_PIN 12 // 控制小车右前方电机后退 Control the motor back on the right front of the car#define Motor_R2_F_PIN 14 // 控制小车右后方电机前进 Control car right rear motor forward#define Motor_R2_B_PIN 15 // 控制小车右后方电机后退 Control car right rear motor back// 控制LOGO对应的LED引脚 Control LED pins#define LED_PIN 7// 定义底层驱动芯片参数 Bottom-layer driver chip related parameters#define Bottom_Layer_Driver_ADDR 0x40// 定义PWM频率 Define PWM frequency#define PWM_FREQUENCY 50// 定义RGB控制引脚和数量 Define RGB control pins and quantity#define RGB_PIN 6#define RGB_NUM 1// 定义蜂鸣器控制引脚 Define BUZZER control pins#define BUZZER_PIN 10// 定义红外键值对应的红外数据 Define the value corresponding to the infrared remote control keyconst uint8_t Power = 0x00;const uint8_t RGB_Light = 0x40;const uint8_t Buzzer = 0xA0;const uint8_t CarForward = 0x80;const uint8_t CarBackward = 0x90;const uint8_t CarLeft = 0x20;const uint8_t CarRight = 0x60;const uint8_t CarLeftSpin = 0x10;const uint8_t CarRightSpin = 0x50;const uint8_t Add = 0x30;const uint8_t Sub = 0x70;const uint8_t Number_0 = 0xB0;const uint8_t Number_1 = 0x08;const uint8_t Number_2 = 0x88;const uint8_t Number_3 = 0x48;const uint8_t Number_4 = 0x28;const uint8_t Number_5 = 0xA8;const uint8_t Number_6 = 0x68;const uint8_t Number_7 = 0x18;const uint8_t Number_8 = 0x98;const uint8_t Number_9 = 0x58;int iCarSpeed = 75;unsigned int uRgbState = 0;
Enumerate common colors
xxxxxxxxxx// 枚举常见颜色 Enumerate common colorsenum ColorType {BLACK,RED,GREEN,BLUE,YELLOW,MAGENTA,CYAN,WHITE,};
Enumerate the common movement modes of omnidirectional cars
xxxxxxxxxx// 枚举全向小车的常见运动方式 Enumerate the common movement modes of omnidirectional carsenum OmniDirectionalCar {STOP,FORWARD,BACKWARD,LEFT,RIGHT,LEFT_ROTATE,RIGHT_ROTATE,LEFT_FORWARD,RIGHT_BACKWARD,RIGHT_FORWARD,LEFT_BACKWARD,};
Create an instance of the Adafruit_PWMServoDriver class
xxxxxxxxxx// 创建Adafruit_PWMServoDriver类的实例 Create an instance of the Adafruit_PWMServoDriver classAdafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(Bottom_Layer_Driver_ADDR);// 创建Adafruit_NeoPixel类的实例 Create an instance of the Adafruit_NeoPixel classAdafruit_NeoPixel RGB = Adafruit_NeoPixel(RGB_NUM, RGB_PIN, NEO_GRB + NEO_KHZ800);
Setting the Motor Speed
xxxxxxxxxx/*** @brief 设置单个电机速度 Setting the Motor Speed* @param motor_forward_pin: 控制电机前进引脚 Control the motor forward pin* @param motor_backward_pin: 控制电机后退引脚 Control the motor backward pin* @param motor_speed: 设置电机速度 Setting the Motor Speed* @retval 无 None*/void setMotorSpeed(uint16_t motor_forward_pin, uint16_t motor_backward_pin, int motor_speed) {motor_speed = map(motor_speed, -255, 255, -4095, 4095);if (motor_speed >= 0) {pwm.setPWM(motor_forward_pin, 0, motor_speed);pwm.setPWM(motor_backward_pin, 0, 0);} else if (motor_speed < 0) {pwm.setPWM(motor_forward_pin, 0, 0);pwm.setPWM(motor_backward_pin, 0, -(motor_speed));}}
Set the car movement mode and speed
xxxxxxxxxx/*** @brief 设置小车运动方式和速度 Set the car movement mode and speed* @param Movement: 小车运动方式 Car movement* @param Speed: 小车运动速度 Car speed* @retval 无 None*/void setCarMove(uint8_t Movement, int Speed) {