Arduino - 小小创客之遥控风扇

电路连接:

  • LED
  • 蜂鸣器
  • 红外接收器
  • 电机+电机驱动
    Arduino - 小小创客之遥控风扇_第1张图片

一、可摇头遥控风扇实例代码:



#include 
#include //

volatile int irPin;
volatile int serPin;
volatile int motorPin;
volatile int angleStep;
volatile int speedStep;//
volatile int minSpeed;//
volatile int motorSpeed;//
volatile int angle;
volatile int motorStatus;//
long ir_item;

Servo servo_serPin;
IRrecv irrecv_12(12);
decode_results results_12;

void setup(){
  irPin = 12;
  serPin = 3;
  motorPin = 6;
  angleStep = 5;
  speedStep = 5;//
  minSpeed = 50;//
  motorSpeed = 150;//
  angle = 90;
  motorStatus = 0;//
  Serial.begin(9600);
  servo_serPin.attach(serPin);
  irrecv_12.enableIRIn();
}

void loop(){
  if (irrecv_12.decode(&results_12)) {
    ir_item=results_12.value;
    String type="UNKNOWN";
    String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
    if(results_12.decode_type>=1&&results_12.decode_type<=13)
	{
      type=typelist[results_12.decode_type];
    }
    Serial.print("IR TYPE:"+type+"  ");
    Serial.println(ir_item,HEX);
    switch (ir_item) 
	{
     case 0xFF18E7:
      motorSpeed = motorSpeed + speedStep;//150+5
      if (motorSpeed > 255) 
	  {
        motorSpeed = 255;

      }
      break;
     case 0xFF38C7:
      if (motorStatus == 0) //on/off
	  {
        motorStatus = 1;

      } else 
	  {
        motorStatus = 0;

      }
      break;
     case 0xFF4AB5:
      motorSpeed = motorSpeed + speedStep;//150-5
      if (motorSpeed < minSpeed) {
        motorSpeed = minSpeed;//50

      }
      break;
     case 0xFF10EF:
      angle = angle - angleStep;
      if (angle < 0) {
        angle = 0;

      }
      break;
     case 0xFF5AA5:
      angle = angle + angleStep;
      if (angle > 180) {
        angle = 180;

      }
      servo_serPin.write(angle);
      delay(10);
      break;
    }
	
    if (motorStatus == 1) 
	{
      analogWrite(motorPin,motorSpeed);//on

    } else 
	{
      analogWrite(motorPin,0);//off

    }
    irrecv_12.resume();
  } else 
  {
  }

}

二、LED灯、蜂鸣器、遥控风扇:

/*
  项目名称:遥控风扇
  作   者: Naiva
  日   期:2019/05/16

*/
#include 
const int MOTOR1PIN = 3;
const int MOTOR2PIN = 5;
const int BUZZERPIN = 4;
const int LEDPIN = 7;
int RECV_PIN = 9;
volatile int speedStep;//
volatile int minSpeed;//
volatile int motorSpeed;//
volatile int motorStatus;//

IRrecv irrecv(RECV_PIN);

decode_results results;

void BUZZER()
{
  digitalWrite(BUZZERPIN,HIGH);
  delay(200);
  digitalWrite(BUZZERPIN,LOW);
  delay(100);
  digitalWrite(BUZZERPIN,HIGH);
  delay(200);
  digitalWrite(BUZZERPIN,LOW);
}

void setup()
{
 

  pinMode(BUZZERPIN,OUTPUT);
  pinMode(LEDPIN,OUTPUT);
  digitalWrite(LEDPIN,LOW);
  digitalWrite(BUZZERPIN,LOW);
  
  speedStep = 5;
  minSpeed = 50;
  motorSpeed = 150;
  motorStatus = 0;
  
  Serial.begin(9600);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
     long RValue = results.value;
     switch(RValue)
     {
       case 0xFFA25D://A  - MOTOR                          
                             analogWrite(MOTOR2PIN,100);
                             
                             BUZZER();
                             break;
       case 0xFF629D://B  - LED 
                             digitalWrite(LEDPIN,!digitalRead(LEDPIN));
                             break;
       case 0xFFE21D://C  - BUZZER
                             digitalWrite(BUZZERPIN,!digitalRead(BUZZERPIN));
                             break;
       case 0xFF02FD://UP  - MOTORSpeed++                          
                             motorSpeed = motorSpeed + speedStep;//150+5
                             if (motorSpeed > 255) 
	                      {
                                motorSpeed = 255;

                              }
                             break;
       case 0xFF9867://DOWN  - MOTORSpeed--                          
                             motorSpeed = motorSpeed + speedStep;//150-5
                             if (motorSpeed < minSpeed) 
                             {
                                motorSpeed = minSpeed;//50

                              }
                             break;
       
         default :
                             Serial.println("ERRO ");
                             break;
         
     
     
     }
      
      
      
     if (motorStatus == 1) 
	{
      analogWrite(MOTOR1PIN,motorSpeed);

    } else 
	{
      analogWrite(MOTOR1PIN,0);

    }
    
    irrecv.resume(); // Receive the next value
  }
  else
  {  
    
  }
  delay(100);
}

(2019/05/16)

你可能感兴趣的:(Arduino)