Infrared Control of Servo Angle
From Microduino Wiki
Language: | English • 中文 |
---|
ContentsObjectiveTo control the angle of a servo via an infrared controller. PrincipleTo receive infrared signals sent from the infrared sensor via the remote controller and adjust rotation angle of the servo according to different signals. Equipment
Preparation
Debugging
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Servo.h>
//Define the key encoding of the remote controller
#define INCREASE 0XFF02FD //Increase
#define DECREASE 0xFF9867 //Decrease
#define NUM_0 0xFF6897 //Number 0
#define NUM_1 0xFF30CF // Number 1
#define NUM_2 0xFF18E7 // Number 2
#define NUM_3 0xFF7A85 // Number 3
#define NUM_4 0xFF10EF // Number 4
#define NUM_5 0xFF38C7 // Number 5
#define NUM_6 0xFF5AA5 // Number 6
#define NUM_7 0xFF42BD // Number 7
#define NUM_8 0xFF4AB5 // Number 8
#define NUM_9 0xFF52AD // Number 9
Servo myservo; // create servo object to control a servo
#define servo_pin SDA
int RECV_PIN = 10;//Infrared pin control
IRrecv irrecv(RECV_PIN);
decode_results results;
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);//Print the infrared encoder
switch (results.value)
{
case INCREASE:
pos += 10;
if (pos > 180)
pos = 180;
break;
case DECREASE:
pos -= 10;
if (pos < 0)
pos = 0;
break;
case NUM_0:
pos = 0;
break;
case NUM_1:
pos = 20;
break;
case NUM_2:
pos = 40;
break;
case NUM_3:
pos = 60;
break;
case NUM_4:
pos = 90;
break;
case NUM_5:
pos = 100;
break;
case NUM_6:
pos = 120;
break;
case NUM_7:
pos = 140;
break;
case NUM_8:
pos = 160;
break;
case NUM_9:
pos = 180;
break;
default:
break;
}
irrecv.resume(); // Receive the next value
myservo.write(pos);
delay(1000);
}
}
Software Debugging
// Define the encoder of the remote controller.
#define INCREASE 0xFFA857 //Increase"+"
#define DECREASE 0xFFE01F //Decrease"-"
#define NUM_0 0xFF6897 //Number0
#define NUM_1 0xFF30CF // Number 1
#define NUM_2 0xFF18E7 // Number 2
#define NUM_3 0xFF7A85 // Number 3
#define NUM_4 0xFF10EF // Number 4
#define NUM_5 0xFF38C7 // Number 5
#define NUM_6 0xFF5AA5 // Number 6
#define NUM_7 0xFF42BD // Number 7
#define NUM_8 0xFF4AB5 // Number 8
#define NUM_9 0xFF52AD // Number 9
if(irrecv.decode(&results)) //Receive infrared encoder
{
switch(results.value) //Judge key status
{
case INCREASE:
pos += 10;
if(pos > 180)
pos = 180;
break;
case DECREASE:
pos -= 10;
if(pos < 0)
pos = 0;
break;
case NUM_0:
pos = 0;
break;
case NUM_1:
pos = 20;
break;
case NUM_2:
pos = 40;
break;
case NUM_3:
pos = 60;
break;
case NUM_4:
pos = 90;
break;
case NUM_5:
pos = 100;
break;
case NUM_6:
pos = 120;
break;
case NUM_7:
pos = 140;
break;
case NUM_8:
pos = 160;
break;
case NUM_9:
pos = 180;
break;
default:
break;
}
irrecv.resume(); //Receive the next signal
myservo.write(pos); //Servo rotation
delay(100);
} ResultBy pressing down different keys on the infrared remote controller, you can control the servo to a different angle. Video |