TVの赤外線リモコンを用いた制御のためのArduinoスケッチです。
#define LdirectionA 2 // 1st bit of direction of the left tire// INPUT1
#define LdirectionB 3 // 2nd bit of direction of the left tire// INPUT2
#define Lspeed 9 // Speed of left tire, analog write only 5,6,9,10,11 //Ain
// right tire
#define RdirectionA 4 // 1st bit of direction of the right tire//INPUT1
#define RdirectionB 5 // 2nd bit of direction of the right tire//INPUT2
#define Rspeed 10 // Speed of left tire, analog write only 5,6,9,10,11//Ain
// sensors
#define Lsensor 0 // sensor for tire analog in
#define Rsensor 1 // sensor for tire analog in
#define Fsensor 2 // sensor for tire /analog in
#define Vin 8// sensor for tire /analog in
boolean IR=LOW;
unsigned long timeIR=0;
byte ReceiveA=B00000000; // 8bitの情報
byte Receive[4]={B00000000,B00000000,B00000000,B00000000}; // 8bitの情報
unsigned int ReceiveNo[4]={0,0,0,0};
unsigned int CheckReceive=0;
unsigned int CommandA=0; //最終的なコマンド
unsigned int CommandAold=0; //最終的なコマンド
unsigned int i=0;
unsigned int j=0;
unsigned int Lrpm=150; //0 to 255 //左タイヤの速度
unsigned int Rrpm=150; //0 to 255 //右タイヤの速度
unsigned long timeC=0;
unsigned long dt=500; //dt=0.1 msec
void DecideMotion(){
switch(CommandA){
case 88: Lrpm=Lrpm+10; Rrpm=Rrpm+10; analogWrite(Lspeed,Lrpm); analogWrite(Rspeed,Rrpm); break; //speed up
case 120:Lrpm=Lrpm-10; Rrpm=Rrpm-10; analogWrite(Lspeed,Lrpm); analogWrite(Rspeed,Rrpm); break; //speed down
case 124: PORTD=PORTD & = B00000011; delay(5); PORTD= PORTD |= B00101000;delay(dt); break; //go forward
case 250: PORTD=PORTD &= B00000011; delay(5); PORTD= PORTD |= B00011000;delay(dt); break; //turn left
case 218: PORTD=PORTD &= B00000011; delay(5); PORTD= PORTD |= B00100100;delay(dt); break; //turn right
case 252: PORTD=PORTD &= B00000011; delay(5); PORTD= PORTD |= B00010100;delay(dt); break; //go backward
case 188: PORTD=PORTD &= B00000011; delay(5); break; // stop
}
CommandAold=CommandA;
}
void setup() {
i=0;
j=0;
pinMode(Vin, INPUT);
pinMode(LdirectionA, OUTPUT);
pinMode(LdirectionB, OUTPUT);
pinMode(RdirectionA, OUTPUT);
pinMode(RdirectionB, OUTPUT);
pinMode(Lspeed, OUTPUT);
pinMode(Rspeed, OUTPUT);
analogWrite(Lspeed,Lrpm);
analogWrite(Rspeed,Rrpm);
DDRD=DDRD | B00111100;
Serial.begin(115200); //シリアル通信の開始
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop(){ //プログラム本体(ずっと繰り返す。)
IR=digitalRead(Vin); // 11番ピンを読んでHIGHまたはLOWをIRに代入。ここに赤外線センサーを接続
delayMicroseconds(300); //300usec待つ。
if(IR<digitalRead(Vin)){
timeC=micros()-timeIR;
if(timeC> =20000){j=0;i=0;ReceiveA=B00000000;} //しばらく信号が無ければ読み直し
else if(timeC>9000){ReceiveA=B00000000;} //9msec
else if((timeC)>1700){ReceiveA=((ReceiveA< <1) | 1); i++;Serial.print(1);} //1700 usec
else{ReceiveA=ReceiveA< <1;i++;Serial.print(0);}
timeIR=micros();
} //IRがoffからonになったタイミング
if(i>=8){i=0;j++;ReceiveNo[j]=(int) ReceiveA; ReceiveA=0; //
if(j>=4){j=0;i=0;ReceiveA=0;CheckReceive=ReceiveNo[3]+ReceiveNo[4];
if(CheckReceive==255){
CommandA=ReceiveNo[3];CheckReceive=0;Serial.println(CommandA);Serial.println("OK");
if(CommandA!=CommandAold){
DecideMotion(); //動き方を決定
}
}
}
}
}
ここで公開するアイデア/装置は安全性を保障しておりません。 用途に応じた設計を行い、十分な安全検査を行ってからご利用ください。 本サイトの情報の営利目的での利用はご遠慮ください。 本サイトの内容の無断転載を禁じます。© 2010 TYK