开启左侧

【培训作品】迎宾机器人“小红人”_董滨棱 王祺颖

[复制链接]
发表于 2018-1-15 06:24:48 | 显示全部楼层 |阅读模式
迎宾机器人“小红人”

作品简介:
       该作品主要通过Arduino UNO、i创学院扩展板、电机驱动模块、红外遥控模块、语音模块、伺服器模块实现机器人迎宾挥手、说话与风扇开关的功能。

作品照片:
IMG_45900.jpg
IMG_67750.jpg
IMG_67760.jpg
程序源代码:
  1. #include <Servo.h>
  2. #include <Voice.h>

  3. Voice voice(11, 12, 13);
  4. Servo servoLeft;
  5. Servo servoRight;

  6. const int TrigPin = 3;
  7. const int EchoPin = 2;
  8. const int distMin = 10;
  9. const int distMax = 20;
  10. const int servoLeftPin = A1;
  11. const int servoRightPin = A3;
  12. const int redLed = 8;
  13. const int yellowLed = 9;
  14. const int greenLed = 10;

  15. int distance;
  16. int preDist = 0;
  17. int nearOrLeft = 0;

  18. const int enablePin = 5;//电机设置
  19. const int inputPin1 = 6;
  20. const int inputPin2 = 7;
  21. const int speedPin = A0;

  22. #include <IRremote.h>//红外按键

  23. volatile int dianji;
  24. long ir_item;

  25. IRrecv irrecv_4(4);
  26. decode_results results_4;



  27. void setup() {
  28.   Serial.begin(9600);
  29.   pinMode(TrigPin, OUTPUT);
  30.   pinMode(EchoPin, INPUT);
  31.   pinMode(redLed, OUTPUT);
  32.   pinMode(yellowLed, OUTPUT);
  33.   pinMode(greenLed, OUTPUT);
  34.   servoLeft.attach(servoLeftPin);
  35.   servoRight.attach(servoRightPin);
  36.   servoLeft.write(90);
  37.   delay(100);
  38.   servoRight.write(90);
  39.   delay(100);

  40.   pinMode(inputPin1, OUTPUT);//电机设置
  41.   pinMode(inputPin2, OUTPUT);

  42.   dianji = 0;//红外按键初始值
  43.   irrecv_4.enableIRIn();


  44. }

  45. void loop() {

  46.   if (irrecv_4.decode(&results_4)) {
  47.     ir_item = results_4.value; //红外按键控制电机
  48.     String type = "UNKNOWN";
  49.     String typelist[14] = {"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
  50.     if (results_4.decode_type >= 1 && results_4.decode_type <= 13) {
  51.       type = typelist[results_4.decode_type];
  52.     }
  53.     Serial.print("IR TYPE:" + type + "  ");
  54.     switch (ir_item) {
  55.       case 0xFFA25D:
  56.         dianji = 1;
  57.         Serial.println(dianji);

  58.         digitalWrite(inputPin1, HIGH);
  59.         digitalWrite(inputPin2, LOW);
  60.         analogWrite(enablePin, 150);


  61.         break;
  62.       case 0xFF629D:
  63.         dianji = 0;
  64.         Serial.println(dianji);

  65.         digitalWrite(inputPin1, LOW);
  66.         digitalWrite(inputPin2, LOW);
  67.         analogWrite(enablePin, 0);
  68.         break;

  69.     }
  70.     irrecv_4.resume();
  71.   }
  72.   else {
  73.   }


  74.   getDistance();
  75.   Serial.println(distance);

  76.   if ((distance <= distMax) && (distance >= distMin)) {
  77.     if (preDist < distance) {
  78.       nearOrLeft = 1;
  79.     }
  80.     else {
  81.       nearOrLeft = 0;
  82.     }
  83.     if ((distance == distMin) && (nearOrLeft == 0)) {

  84.       digitalWrite(redLed, HIGH);
  85.       digitalWrite(yellowLed, HIGH);
  86.       digitalWrite(greenLed, HIGH);
  87.       voice.VoiceWord(21);
  88.       voice.VoiceWord(26);
  89.       servoLeft.write(10);
  90.       delay(100);
  91.       servoRight.write(10);
  92.       delay(100);

  93.     }
  94.     else if ((distance == distMax) && (nearOrLeft == 1)) {

  95.       digitalWrite(redLed, LOW);
  96.       digitalWrite(yellowLed, LOW);
  97.       digitalWrite(greenLed, LOW);
  98.       voice.VoiceWord(50);
  99.       voice.VoiceWord(25);
  100.       voice.VoiceWord(59);

  101.       servoLeft.write(170);
  102.       delay(100);
  103.       servoRight.write(170);
  104.       delay(100);

  105.     }
  106.     else {

  107.     }
  108.     preDist = distance;
  109.   }
  110.   digitalWrite(redLed, LOW);
  111.   digitalWrite(yellowLed, HIGH);
  112.   digitalWrite(greenLed, LOW);
  113.   servoLeft.write(90);
  114.   delay(100);
  115.   servoRight.write(90);
  116.   delay(100);
  117. }

  118. void getDistance() {
  119.   digitalWrite(TrigPin, LOW);
  120.   delayMicroseconds(2);
  121.   digitalWrite(TrigPin, HIGH);
  122.   delayMicroseconds(10);
  123.   digitalWrite(TrigPin, LOW);
  124.   distance = pulseIn(EchoPin, HIGH) / 58.0;
  125. }
复制代码

【附件】
程序源文件:
http://imakeedu.com/forum.php?mod=attachment&aid=NzUyfDM3MWUzYTZmMWMzZmJkMjY1YmM3Y2U5NWExM2RmMmJjfDE1MTk2MDAyMDU%3D&request=yes&_f=.ino
作品演示视频:
http://imakeedu.com/forum.php?mod=attachment&aid=NzUxfGNlN2UwMzMzNjgxMDBkNzNkYTlkYTRiYzJhN2YzMTVkfDE1MTk2MDAyMDU%3D&request=yes&_f=.mp4
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩课程推荐

关于我们

帮助中心

商务合作

点击这里给我发消息

关注微信公众号

Copyright © 2016-2017 i创学院 - 上海享渔教育科技有限公司 Powered by Discuz!     ( 沪ICP备16033954号-1 )