Robofun 機器人論壇
標題:
IRremote的問題
[打印本頁]
作者:
mojamoja1202
時間:
2015-3-3 00:18
標題:
IRremote的問題
小弟最近用了以下電子零件:Arduino uno*1
步進馬達+輪胎*2
L198N*1
紅外線搖控器*1
拼湊成一台紅外線搖控小車
以下是小弟的程式,想請教線上的先進大大們,程式該怎麼修改才好
#include <IRremote.h>
const int ENA = 5;
const int IN1 = 6;
const int IN2 = 7;
const int IN3 = 8;
const int IN4 = 9;
const int ENB = 10;
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int motorSpeed = 255;
int act = 0;
const int delayTime = 1000;
//motor act
void forward(){
analogWrite(ENA,motorSpeed);
analogWrite(ENB,motorSpeed);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void backward(){
analogWrite(ENA,motorSpeed);
analogWrite(ENB,motorSpeed);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void turnLeft(){
analogWrite(ENA,motorSpeed);
analogWrite(ENB,motorSpeed);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void turnRight(){
analogWrite(ENA,motorSpeed);
analogWrite(ENB,motorSpeed);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void motorStop(){
analogWrite(ENA,0);
analogWrite(ENB,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
void motorCmd(){
switch(results.value){
case 16736925: //up
act = 1;
break;
case 16754775: //down
act = 2;
break;
case 16720605: //left
act = 3;
break;
case 16761405: //right
act = 4;
break;
}
}
void motorAct(int act = 0){
switch(act){
case 1: //up
forward();
delay(delayTime);
motorStop();
break;
case 2: //down
backward();
delay(delayTime);
motorStop();
break;
case 3: //left
turnLeft();
delay(delayTime);
motorStop();
break;
case 4: //right
turnRight();
delay(delayTime);
motorStop();
break;
default:
motorStop();
}
}
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(ENA,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ENB,OUTPUT);
}
void loop(){
if(irrecv.decode(&results)){
motorCmd();
motorAct(act);
irrecv.resume();
}
}
複製代碼
小弟是想請問,當接收到搖控器的指令後,小車就做動作是沒有問題的
目前小弟的寫法是接收到紅外線,就做相對的動作一秒鐘,然後就停止馬達轉動
但是小弟希望當沒有接收到任何紅外線時,小車是停止不動的,有接收到紅外線才會動
請問這個部份應該要如何修改才對????
作者:
pizg
時間:
2015-3-3 08:12
回復
1#
mojamoja1202
設一變數記錄按下鍵的時間,然後在驅動馬達時減去馬達轉動的時間,
直到時間為0 馬達就停止.
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2