Created
March 31, 2014 06:09
-
-
Save ETiV/9886235 to your computer and use it in GitHub Desktop.
红外遥控器控制风扇转速. 红外信号针脚接11, 马达正极接6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IRremote.h> | |
int RECV_PIN = 11; | |
int MOTOR_PIN = 6; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
const int analogMin = 0; | |
const int analogMax = 255; | |
float power = 1.0f; | |
boolean state_on_off = false; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(MOTOR_PIN, OUTPUT); | |
analogWrite(MOTOR_PIN, analogMin); | |
irrecv.enableIRIn(); | |
} | |
void loop() | |
{ | |
int val = irrecv.decode(&results); | |
if (val) { | |
switch (results.value) { | |
case 0xFFA25D: // REMOTE CONTROL BUTTON 'ON' | |
{ | |
state_on_off = true; | |
analogWrite(MOTOR_PIN, power * analogMax); | |
} | |
break; | |
case 0xFFE21D: // REMOTE CONTROL BUTTON 'OFF' | |
{ | |
state_on_off = false; | |
analogWrite(MOTOR_PIN, analogMin); | |
} | |
break; | |
case 0xFF9867: // REMOTE CONTROL BUTTON 'UP' | |
{ | |
if (state_on_off) { | |
power += 0.2; | |
if (power > 1.0) { | |
power = 1.0f; | |
} | |
analogWrite(MOTOR_PIN, power * analogMax); | |
} | |
} | |
break; | |
case 0xFFB04F: // REMOTE CONTROL BUTTON 'DOWN' | |
{ | |
if (state_on_off) { | |
power -= 0.2; | |
if (power < 0.2) { | |
power = 0.2f; | |
} | |
analogWrite(MOTOR_PIN, power * analogMax); | |
} | |
} | |
break; | |
} | |
Serial.println(results.value, HEX); | |
irrecv.resume(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment