- #include <IRremote.h>//包含紅外庫
- int RECV_PIN = A4;//端口聲明
- IRrecv irrecv(RECV_PIN);
- decode_results results;//結構聲明
- int on = 0;//標志位
- unsigned long last = millis();
- long run_car = 0x00FF18E7;//按鍵2
- long back_car = 0x00FF4AB5;//按鍵8
- long left_car = 0x00FF10EF;//按鍵4
- long right_car = 0x00FF5AA5;//按鍵6
- long stop_car = 0x00FF38C7;//按鍵5
- long left_turn = 0x00ff30CF;//按鍵1
- long right_turn = 0x00FF7A85;//按鍵3
- //==============================
- int Left_motor_back=8; //左電機后退(IN1)
- int Left_motor_go=9; //左電機前進(IN2)
- int Right_motor_go=10; // 右電機前進(IN3)
- int Right_motor_back=11; // 右電機后退(IN4)
- void setup()
- {
- //初始化電機驅動IO為輸出方式
- pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
- pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
- pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
- pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
- pinMode(13, OUTPUT);////端口模式,輸出
- Serial.begin(9600); //波特率9600
- irrecv.enableIRIn(); // Start the receiver
- }
- void run() // 前進
- {
- digitalWrite(Right_motor_go,HIGH); // 右電機前進
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
- //analogWrite(Right_motor_back,0);
- digitalWrite(Left_motor_go,HIGH); // 左電機前進
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
- //analogWrite(Left_motor_back,0);
- //delay(time * 100); //執行時間,可以調整
- }
- void brake() //剎車,停車
- {
- digitalWrite(Right_motor_go,LOW);
- digitalWrite(Right_motor_back,LOW);
- digitalWrite(Left_motor_go,LOW);
- digitalWrite(Left_motor_back,LOW);
- //delay(time * 100);//執行時間,可以調整
- }
- void left() //左轉(左輪不動,右輪前進)
- {
- digitalWrite(Right_motor_go,HIGH); // 右電機前進
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);
- //analogWrite(Right_motor_back,0);//PWM比例0~255調速
- digitalWrite(Left_motor_go,LOW); //左輪不動
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,0);//PWM比例0~255調速
- //delay(time * 100); //執行時間,可以調整
- }
- void spin_left() //左轉(左輪后退,右輪前進)
- {
- digitalWrite(Right_motor_go,HIGH); // 右電機前進
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);
- //analogWrite(Right_motor_back,0);//PWM比例0~255調速
- digitalWrite(Left_motor_go,LOW); //左輪后退
- digitalWrite(Left_motor_back,HIGH);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,200);//PWM比例0~255調速
- //delay(time * 100); //執行時間,可以調整
- }
- void right() //右轉(右輪不動,左輪前進)
- {
- digitalWrite(Right_motor_go,LOW); //右電機不動
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,0);//PWM比例0~255調速
- digitalWrite(Left_motor_go,HIGH);//左電機前進
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);
- //analogWrite(Left_motor_back,0);//PWM比例0~255調速
- //delay(time * 100); //執行時間,可以調整
- }
- void spin_right() //右轉(右輪后退,左輪前進)
- {
- digitalWrite(Right_motor_go,LOW); //右電機后退
- digitalWrite(Right_motor_back,HIGH);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,200);//PWM比例0~255調速
- digitalWrite(Left_motor_go,HIGH);//左電機前進
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);
- //analogWrite(Left_motor_back,0);//PWM比例0~255調速
- //delay(time * 100); //執行時間,可以調整
- }
- void back() //后退
- {
- digitalWrite(Right_motor_go,LOW); //右輪后退
- digitalWrite(Right_motor_back,HIGH);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,150);//PWM比例0~255調速
- digitalWrite(Left_motor_go,LOW); //左輪后退
- digitalWrite(Left_motor_back,HIGH);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,150);//PWM比例0~255調速
- //delay(time * 100); //執行時間,可以調整
- }
- void dump(decode_results *results)
- {
- int count = results->rawlen;
- if (results->decode_type == UNKNOWN)
- {
- //Serial.println("Could not decode message");
- brake();
- }
- //串口打印,調試時可以打開,實際運行中會影響反應速度,建議屏蔽
- /*
- else
- {
- if (results->decode_type == NEC)
- {
- Serial.print("Decoded NEC: ");
- }
- else if (results->decode_type == SONY)
- {
- Serial.print("Decoded SONY: ");
- }
- else if (results->decode_type == RC5)
- {
- Serial.print("Decoded RC5: ");
- }
- else if (results->decode_type == RC6)
- {
- Serial.print("Decoded RC6: ");
- }
- Serial.print(results->value, HEX);
- Serial.print(" (");
- Serial.print(results->bits, DEC);
- Serial.println(" bits)");
- }
- Serial.print("Raw (");
- Serial.print(count, DEC);
- Serial.print("): ");
- for (int i = 0; i < count; i++)
- {
- if ((i % 2) == 1)
- {
- Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
- }
- else
- {
- Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
- }
- Serial.print(" ");
- }
- Serial.println("");
- */
- }
- void loop()
- {
- if (irrecv.decode(&results)) //調用庫函數:解碼
- {
- // If it's been at least 1/4 second since the last
- // IR received, toggle the relay
- if (millis() - last > 250) //確定接收到信號
- {
- on = !on;//標志位置反
- digitalWrite(13, on ? HIGH : LOW);//板子上接收到信號閃爍一下led
- dump(&results);//解碼紅外信號
- }
- if (results.value == run_car )//按鍵2
- run();//前進
- if (results.value == back_car )//按鍵8
- back();//后退
- if (results.value == left_car )//按鍵4
- left();//左轉
- if (results.value == right_car )//按鍵6
- right();//右轉
- if (results.value == stop_car )//按鍵5
- brake();//停車
- if (results.value == left_turn )//按鍵1
- spin_left();//左旋轉
- if (results.value == right_turn )//按鍵3
- spin_right();//右旋轉
- last = millis();
- irrecv.resume(); // Receive the next value
- }
- }
復制代碼 |