arduino 智能车

 

const uint8_t IR_Receiver_Pin = 11;            // Infrared receiving pin
const uint8_t Led_Control_Pin = 7;             // LED control pin
const uint8_t Motor_Speed_Pin = 6;             // Motor speed control pin
const uint8_t Motor_Dir_Pin[4] = {3, 2, 5, 4}; // Motor direction control pin
const uint8_t Ultrasonic_Sensor_Pin = 8;       // Ultrasonic sensor pin
#include <IRremote.h>                          
uint8_t Motor_Speed = 0;                       // Store the motor speed
void setup()
{
    Serial.begin(9600);                 // Initialize the serial port, 9600 baud rates.This command is used to print debugging information
    analogWrite(Motor_Speed_Pin, 0);    // Set motor speed to 0
    pinMode(Led_Control_Pin, OUTPUT);   // Set LED pin to output
    digitalWrite(Led_Control_Pin, LOW); // LED goes out
    for (uint8_t i = 0; i < 4; i++)     // Traverse the motor direction control pin array
    {
        pinMode(Motor_Dir_Pin[i], OUTPUT);   // Set pin to output
        digitalWrite(Motor_Dir_Pin[i], LOW); // Pin outputs low level
    }
    IrReceiver.begin(IR_Receiver_Pin); // Initialize the infrared sensor
}

void loop()
{
    if (IrReceiver.decode()) // If the infrared decoder works
    {
        Serial.print("IR Code: ");                               // Debugging information is displayed over the serial port
        Serial.println(IrReceiver.decodedIRData.decodedRawData); // Debugging information is displayed over the serial port
        switch (IrReceiver.decodedIRData.decodedRawData)         // Perform different operations based on the infrared value
        {
            // Fast forward
        case 4010852096:
            Motor_Speed = 255; // Maximum motor speed
            digitalWrite(Motor_Dir_Pin[0], HIGH);
            digitalWrite(Motor_Dir_Pin[1], LOW);
            digitalWrite(Motor_Dir_Pin[2], HIGH);
            digitalWrite(Motor_Dir_Pin[3], LOW);
            break;
            // Slow forward
        case 3994140416:
            Motor_Speed = 127; // Motor speed 50%
            digitalWrite(Motor_Dir_Pin[0], HIGH);
            digitalWrite(Motor_Dir_Pin[1], LOW);
            digitalWrite(Motor_Dir_Pin[2], HIGH);
            digitalWrite(Motor_Dir_Pin[3], LOW);
            break;
            // Fast reverse
        case 3977428736:
            Motor_Speed = 255; // Maximum motor speed
            digitalWrite(Motor_Dir_Pin[0], LOW);
            digitalWrite(Motor_Dir_Pin[1], HIGH);
            digitalWrite(Motor_Dir_Pin[2], LOW);
            digitalWrite(Motor_Dir_Pin[3], HIGH);
            break;
            // Slow reverse
        case 3944005376:
            Motor_Speed = 127; // Motor speed 50%
            digitalWrite(Motor_Dir_Pin[0], LOW);
            digitalWrite(Motor_Dir_Pin[1], HIGH);
            digitalWrite(Motor_Dir_Pin[2], LOW);
            digitalWrite(Motor_Dir_Pin[3], HIGH);
            break;
            // Brake
        case 3927293696:
            Motor_Speed = 0; // Motor speed is 0
            digitalWrite(Motor_Dir_Pin[0], LOW);
            digitalWrite(Motor_Dir_Pin[1], LOW);
            digitalWrite(Motor_Dir_Pin[2], LOW);
            digitalWrite(Motor_Dir_Pin[3], LOW);
            break;
        }
        IrReceiver.resume(); // Infrared is picking up again
    }
    int distance = Get_Distance();           // Measure distance
    if (distance <= 100)                     // If the distance is less than or equal to 100
        digitalWrite(Led_Control_Pin, HIGH); // LED is on
    else                                     // Else
        digitalWrite(Led_Control_Pin, LOW);  // LED goes out
    if (Motor_Speed != 0)                    // If the motor speed is not zero
    {
        if (distance <= 100 && distance > 50)          // If the distance is between 50 and 100
            analogWrite(Motor_Speed_Pin, 10);          // Control motor speed is (10/255)%
        else if (distance <= 50)                       // If the distance is less than 50
            analogWrite(Motor_Speed_Pin, 0);           // Control motor speed is 0
        else                                           // If the distance is greater than 100
            analogWrite(Motor_Speed_Pin, Motor_Speed); // Set motor speed to remote control speed
    }
}
int Get_Distance(void)
{
    pinMode(Ultrasonic_Sensor_Pin, OUTPUT);   // Set the ultrasonic sensor pin as the output
    digitalWrite(Ultrasonic_Sensor_Pin, LOW); // Pull the ultrasonic sensor pin low
    delayMicroseconds(2);                     
    // Send a 10US high level pulse
    digitalWrite(Ultrasonic_Sensor_Pin, HIGH);
    delayMicroseconds(10);
    digitalWrite(Ultrasonic_Sensor_Pin, LOW);
    pinMode(Ultrasonic_Sensor_Pin, INPUT);                // Sets the ultrasonic sensor pin as the input
    int _distance = pulseIn(Ultrasonic_Sensor_Pin, HIGH); // Obtain the high level width of ultrasonic sensor pin
    _distance = _distance / 47;                           // Calculate the distance from the pulse width
    Serial.print("Distance: ");                           // Debugging information is displayed over the serial port
    Serial.print(_distance);                              // Debugging information is displayed over the serial port
    Serial.println(" cm");                                // Debugging information is displayed over the serial port
    return _distance;                                     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值