“Lesson 11--Microduino Infrared Controls Servo Angle”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{{Language | 第十一课--Microduino 红外控制舵机角度}} {| style="width: 800px;" |- | ==Objective== This lesson will teach you control the servo by infrared. ==Equip...")
 
Pkj讨论 | 贡献
Program
 
第27行: 第27行:
  
 
==Program==
 
==Program==
 +
Download program:
 +
https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Advanced/MicroduinoIRControlServeAngle
 +
 
<source lang="cpp">
 
<source lang="cpp">
  

2014年3月29日 (六) 07:52的最新版本

Language English

Objective

This lesson will teach you control the servo by infrared.

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other equipments:
    • Breadboard Jumper one box
    • Breadboard one piece
    • USB Data cable one
    • The infrared receiver one
    • Remote control one
    • Servo one


Servo

第十一课-舵机.jpg

Servo is a set of automatic control system which consists of DC motor, gear set, sensor and control circuit. By sending a signal, the output shaft will rotate specified angle. Generally servo has a maximum rotation angle (such as 180 degrees). The mainly difference between a common DC motor is taht DC motor is circular rotation, servo only can rotate within a certain Angle, can't rotate as circle (digital servo can switch in the servo model and motor model, doesn't have this problem). Common DC motor can't return the rotation angle information, and servo can be. Usage is different also, common DC motor is made power with the whole circle turn, servo is used to control an objuect to rotate a certain angle (such as robot joints).

Schematic

第十一课-Microduino红外控制舵机角度原理图.jpg

In schematic, the black line is the ground wire and need connect to GNU, red line is the power line to 5V voltage, the blue line is the infrared receiver signal line, the yellow line is the servo signal line.

Program

Download program: https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Advanced/MicroduinoIRControlServeAngle

#include <IRremote.h>
#include <Servo.h>

int action;
int deg=0;
int t;

int RECV_PIN = 11;//Defien the infrared receive pin
IRrecv irrecv(RECV_PIN);
Servo servo;
decode_results results;

int deckey(unsigned long t)
{
  switch(t){
    case 0xFD08F7://Button 1 encoding
    return 1;
    break;
    case 0xFD8877://Button 2 encoding
    return 2;
    break;
    case 0xFD48B7://Button 3 encoding
    return 3;
    break;
    case 0xFD28D7://Button 4 encoding
    return 4;
    break;
    case 0xFDA857://Button 5 encoding
    return 5;
    break;
    case 0xFD6897://Button 6 encoding
    return 6;
    break;
    case 0xFD18E7://Button 7 encoding
    return 7;
    break;
    case 0xFD9867://Button 8 encoding
    return 8;
    break;
    case 0xFD58A7://Button 9 encoding
    return 9;
    break;
    default:
    return 0;
    break;
  }
}   

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Initialize the infrared receiver
  servo.attach(9);
  servo.write(0);
  t=0;
}

void loop() {

  deg=servo.read();
  if (irrecv.decode(&results))
  {
    action=deckey(results.value);
    Serial.println(results.value, HEX);//In hexadecimal output to receive code with a new line
    Serial.println(action);
    Serial.println();//In order to easy observe the output increase a blank line

    switch(action)
    {
      case 1:
      servo.write(0);
      break;
      case 2:
      servo.write(20);
      break;
      case 3:
      servo.write(40);
      break;
      case 4:
      servo.write(60);
      break;
      case 5:
      servo.write(80);
      break;
      case 6:
      servo.write(100);
      break;
      case 7:
      servo.write(120);
      break;
      case 8:
      servo.write(140);
      break;
      case 9:
      servo.write(160);
      break;
    }
    delay(10);
    irrecv.resume();
  }

}

Debug

Step 1:Download IRremote library [1]

Step 2:Uncompress the library to libraries folder of Arduino IDE

Step 3: Set up the circuit;

Step 4: open the serial monitor, the remote control towards to the infrared receiver button, infrared codes will be shown.

第十一课-串口显示红外接收编码并转换为按键值.jpg

Result

Press the button 1,2,3,4..., servo will rotate.

Video

http://v.youku.com/v_show/id_XNjc1NjQ4Mzcy.html