Arduino------红外控制舵机风扇

本篇文章,主要介绍如何使用 “红外遥控器、接收头” 、 “ULN2003驱动模块” 和 “继电器” 等制作一款Arduino红外控制舵机风扇。

所需的硬件
● Arduino Uno 开发板
● 红外遥控器
● 红外接收器
● 舵机
● ULN2003驱动模块
● 继电器
● 直流电机
● 小风扇
● 若干条跳线

连接
● 舵机

舵机 Arduino
橙色线 引脚9
红色线 3.3
棕色线 GND

● ULN2003驱动模块 //防止直流电机干扰红外接收器接收红外信号
Arduino------红外控制舵机风扇_第1张图片

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 

#include  

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

你可能感兴趣的:(Arduino,舵机,红外控制)