Infrared Control of Servo Angle

From Microduino Wiki
Revision as of 01:28, 25 January 2016 by Admin (talk) (Created page with "{{Language| Infrared Control of Servo Angle }} {| style="width: 800px;" |- | ==Objective== To control the angle of a servo via an infrared controller. ==Principle== To recei...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Language: English  • 中文

Objective

To control the angle of a servo via an infrared controller.

Principle

To receive infrared signals sent from the infrared sensor via the remote controller and adjust rotation angle of the servo according to different signals.

Equipment

Module Numbner Function
mCookie-CoreUSB 1 Core board
mCookie-Hub 1 Sensor pin board
Microduino-IR receiver 1 Infrared receiving sensor
Microduino-Servo Connector 1 Servo connector
  • Other Equipment
    • Servo
    • Infrared remote controller
    • USB cable
    • Two sensor cables
IR servo.jpg

Preparation

  • Setup 1:Connect the Microduino-Servo Connector with the servo to the upper row of the pins.
MCookie-Servo Connector-sensor.JPG
  • Setup 2:Connect the Microduino-Servo Connector to the mCookie-Hub's ICC port and the IR Receiver to the D10 pin of the Hub.
MCookie-Servo Connector-IR-hub.JPG
  • Setup 3:Get all modules stacked, and then connect them to the computer via a USB cable.
MCookie-servo-IR-pc.JPG

Debugging

  • Open Arduino IDE, copy the following code into the IDE.
#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);
  }
}
  • Choose the right board and the COM port.
Upload.JPG
  • Compile
    • It'll remind you to save program while compiling.
  • Upload
    • Upload after the compile and it'll show "Uploading done" when finished.
  • Result
    • Increase 10°of the servo angle via "+" or decrease 10°via "-"
    • The number from 0 to 9 corresponds to a specific angle respectively.
  • Open the serial monitor and you'll see the received infrared signal.
Ir rec.JPG

Software Debugging

  • Define the encoder of the remote controller. Here we adopt NEC encoding format.
// 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
  • The encoding format of the infrared remote controller adopts data hexadecimal representation.
Ir.jpg
  • Receive infrared remote control signals and judge the status of the key to control the servo rotation angle.
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);     
  }

Result

By pressing down different keys on the infrared remote controller, you can control the servo to a different angle.

Video