天天看點

Arduino------紅外控制舵機風扇

本篇文章,主要介紹如何使用 “紅外遙控器、接收頭” 、 “ULN2003驅動子產品” 和 “繼電器” 等制作一款Arduino紅外控制舵機風扇。

所需的硬體

● Arduino Uno 開發闆

● 紅外遙控器

● 紅外接收器

● 舵機

● ULN2003驅動子產品

● 繼電器

● 直流電機

● 小風扇

● 若幹條跳線

連接配接

● 舵機

舵機 Arduino
橙色線 引腳9
紅色線 3.3
棕色線 GND

● ULN2003驅動子產品 //防止直流電機幹擾紅外接收器接收紅外信号

Arduino------紅外控制舵機風扇
ULN2003驅動子產品 Arduino
1 GND
2 5V
3 引腳7

● 紅外接收器

紅外接收器 Arduino
VOUT 引腳11
GND GND
VCC VCC

● 繼電器

繼電器 Arduino
VCC 5V
GND GND
IN ULN2003子產品引腳3
COM 外電源負極
NO 直流電機

● 支流電機

直流電機 繼電器
外電源正極 NO

庫的準備及操作說明

程式

//*代碼說明紅外遙控CH:關直流電機,CH+:開直流電機
             NEXT :關舵機 ,PLAY/PAUSE:開舵機。
#include <IRremote.h>

#include <Servo.h> 

Servo myservo;  //舵機起個名字
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 
 
#define MAX 22
 
int RECV_PIN =11;      //定義紅外接收子產品輸出口接arduino數字引腳11
   
IRrecv irrecv(RECV_PIN);
decode_results results;
//Car mp3編碼
unsigned long rremote_code[MAX] = {
    0xFFA25D,0xFF629D,0xFFE21D,//CH- CH CH+
    0xFF22DD,0xFF02FD,0xFFC23D,//PREV NEXT PLAY
    0xFFE01F,0xFFA857,0xFF906F,//VOL- VOL+ EQ
    0xFF6897,0xFF9867,0xFFB04F,// 0 100+ 200+
    0xFF30CF,0xFF18E7,0xFF7A85,// 1  2  3
    0xFF10EF,0xFF38C7,0xFF5AA5,// 4  5  6
    0xFF42BD,0xFF4AB5,0xFF52AD, // 7  8  9
    0xFFFFFFFF//長按
};
 
//Car mp3對應的字元串
String rremote_string[MAX] = {
   "CH-","CH","CH+",
   "PREV","NEXT","PLAY/PAUSE",
   "VOL-","VOL+","EQ",
   "0","100+","200+",
   "1","2","3",
   "4","5","6",
   "7","8","9",
   "longPress"
};
String ServoEn="OFF";
void getRemoteDo();  
void ServoCtr();

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); //初始化紅外遙控
  pinMode(7, OUTPUT);
  myservo.attach(9);

}

void loop()
{
ServoCtr();
}


void ServoCtr()
{
 for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees 
  {      // in steps of 1 degree 
    getRemoteDo();
    while(ServoEn=="OFF")
    {
      getRemoteDo();
      }
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 90; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {  
    getRemoteDo();
      while(ServoEn=="OFF")
    {
      getRemoteDo();
      }                           
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }   
  
 }
 /*判斷紅外接收資訊*/
void getRemoteDo()
{
  String codeString;
  if (irrecv.decode(&results)) 
      {
  //列印字元串
    codeString = getRremoteString(results.value);
    if(codeString.length()!=0)
        Serial.println(codeString);
    irrecv.resume(); // 接收下一個值
       }
   if (codeString=="CH") 
       {
        Serial.println("ok");
        digitalWrite(7, LOW); 
       }
   if (codeString=="CH+") 
      {
       Serial.println("ok_");
       digitalWrite(7, HIGH); 
      }     
   if (codeString=="PLAY/PAUSE") 
      {
       
       ServoEn="On";
       }  
     if (codeString=="NEXT") 
     {
      
       ServoEn="OFF";
     }         
}
/**
* 解析紅外編碼并傳回對應的字元串
*/
String getRremoteString(unsigned long code){
  String rremotestring = "";
  int i = 0;
  for(i = 0;i<MAX-1;i++){
    if(code == rremote_code[i]){
      break;  
    }
  }
  //過濾掉長按
  if(i==MAX-1) return "";
  else return rremote_string[i];
}