编程语言
首页 > 编程语言> > Arduino R3 红外接收控制小坦克程序

Arduino R3 红外接收控制小坦克程序

作者:互联网

程序功能:通过红外遥控器接收指令,坦克玩具进行前进、后退和行进中的炮塔转动

#include <Unistep2.h>//步进电动机库

#include <IRremote.h>//马达库

#define STEPS 4096

int steps = -STEPS;

Unistep2 stepper(8, 9, 10, 11, STEPS, 1000);// IN1, IN2, IN3, IN4, 總step數, 每步的延遲(in micros)

int RECV_PIN =7;

int runNum = 0;

decode_results results;

IRrecv irrecv(RECV_PIN);

#define A_1A 3 // A_1A控制A馬達的前進(HIGH)、後退(LOW)

#define A_1B 4 // A_1B控制A馬達的速度 0~255 ,LOW表示停止

#define B_1A 5 // B_1A控制B馬達的前進(HIGH)、後退(LOW)

#define B_1B 6 // B_1B控制B馬達的速度 0~255,LOW表示停止

 

int highSpeed = 200;

int lowSpeed = 100;

#define Speed 150

void setup()

{

    Serial.begin(9600);

    irrecv.enableIRIn();

    pinMode(A_1A,OUTPUT);

    pinMode(A_1B,OUTPUT);

    pinMode(B_1A,OUTPUT);

    pinMode(B_1B,OUTPUT);

    digitalWrite(A_1A,LOW);

    digitalWrite(A_1B,LOW);

    digitalWrite(B_1A,LOW);

    digitalWrite(B_1B,LOW);

}

 

//車子停止

void stopcar(){

    digitalWrite(A_1A,LOW);

    digitalWrite(A_1B,LOW);

    digitalWrite(B_1A,LOW);

    digitalWrite(B_1B,LOW);

 

//車子向前走

void forward(){

    digitalWrite(A_1A,HIGH);

    analogWrite(A_1B,LOW);

    digitalWrite(B_1A,HIGH);

    analogWrite(B_1B,LOW);

}

 

//車子向後走

void backward(){

    digitalWrite(A_1A,LOW);

    analogWrite(A_1B,Speed);

    digitalWrite(B_1A,LOW);

    analogWrite(B_1B,Speed);

}

//車子右轉

void turnRight(){

    digitalWrite(A_1A,LOW);

    analogWrite(A_1B,LOW);

    digitalWrite(B_1A,HIGH);

    analogWrite(B_1B,LOW);

}

//車子左轉

void turnLeft(){

    digitalWrite(A_1A,HIGH);

    analogWrite(A_1B,LOW);

    digitalWrite(B_1A,LOW);

    analogWrite(B_1B,LOW);

}

void loop()

{

  stepper.run();  //步進機啟動

  //红外接收

   if (irrecv.decode()) {  

          Serial.println(irrecv.decodedIRData.command);

          switch(irrecv.decodedIRData.command){

            case 70:

             runNum = 0;

             stepper.moveTo(runNum);

            break;

            case 69:

              runNum = runNum + 409;

              stepper.moveTo(runNum);

              //stepper.run();  

            break;

            case 71:

             runNum = runNum - 409;

             stepper.moveTo(runNum);

             // stepper.run();  

            break;

            case 68:

              backward();

            break;

            case 64:

             forward();

            break;

            case 67:

             stopcar();

            break;

            case 7:

             turnLeft();

            break;

            case 21:

              turnRight();

            break;

          }

          irrecv.resume();   

    }

 

参考博文:https://blog.jmaker.com.tw/tag/arduino_getting_started/

标签:case,runNum,Arduino,digitalWrite,R3,1A,红外,1B,LOW
来源: https://blog.csdn.net/kangguang/article/details/117448073