找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5354|回复: 2
打印 上一主题 下一主题
收起左侧

公寓不让养猫?那就自己做个机器猫

[复制链接]
跳转到指定楼层
楼主
ID:101489 发表于 2016-1-3 18:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

想要养只猫当宠物,但是又担心“猫老爷”的吃喝拉撒睡和掉毛?那就自己做一只机器猫吧!首先让我们从最简单的开始:做一只会随机发出猫叫声的机器猫;另外,它还有一双可以变化的眼睛,当你抚摸它的时候,它还会发出咕噜咕噜的声音。而更重要的是,因为本项目是基于Arduino开发的,你还可以为这只可爱的机器猫设计更多功能!
第一步:准备材料
  • Arduino UNO
  • 面包板
  • 喇叭
  • 触控传感器(FSR-Interlink 406)
  • 12个5mm LED
  • 6个220Ω电阻
  • 47kΩ电阻
  • 15条母对母跳线
  • 16条24条母对公跳线
  • 3D打印组件若干

第二步:线路设计
首先确定各个输入输出引脚的定义,然后画出电路图。如图所示,除了基本的接地连接外,喇叭连接引脚2,而触控传感器则连接5V和A0,另外,注意在触控传感器上串联一个47kΩ的电阻,这个电阻的阻值大小是可调的,这取决你想要的触控灵敏度。
接下来将LED连接到6、7、8、9、11、12引脚。
第三步:编程确定好连接方式之后就开始编程,基本功能的代码已经写好(点击这里下载 机器猫.rar (46.17 KB, 下载次数: 10) ),需要你期望实现更多功能,向其中添加即可。
  1. //bool LEDIsOn = false;
  2. //bool LEDsChanged = false;
  3. bool isBeingPetted = false;
  4. int sensorValue = 0;
  5. int totalSensorValue = 0;
  6. float averageSensorValue;
  7. int count = 0;
  8. int randomMeowTime;

  9. bool defaultEyes [6] = {false, true, true,
  10.                         true, true, true};
  11. bool blinkEyes [6] = {false, false, false,
  12.                       true, true, true};
  13. bool purrEyes [6] = {false, true, false,
  14.                      true, false, true};
  15. bool meowEyes [6] = {true, true, false,
  16.                      false, false, true};
  17. bool mewEyes [6] = {false, true, false,
  18.                     false, true, false};

  19. int ledEyes [6] = {6,7,8,9,11,12};

  20. void setup() {
  21.   // put your setup code here, to run once:
  22.   // pin 6,7,8,9,11,12 is an LED, pin 2 is the speaker
  23.   for (int i = 0; i <= 5; i++){
  24.     pinMode(ledEyes[i], OUTPUT);
  25.   }
  26.   
  27.   pinMode(2, OUTPUT);

  28.   // pin A0 is the touch sensor input
  29.   pinMode(A0, INPUT);

  30.   changeEyes(defaultEyes);
  31. }

  32. void loop() {
  33.   randomSeed(analogRead(0));
  34.   randomMeowTime = random(0.0, 400.0);

  35.   // put your main code here, to run repeatedly:
  36.   sensorValue = analogRead(A0);
  37.   totalSensorValue += sensorValue;
  38.   count += 1;

  39.   if (count == 5){
  40.     averageSensorValue = totalSensorValue / 5;
  41.     if (averageSensorValue > 1005){
  42.       // kat wordt aangeraakt
  43.       isBeingPetted = true;
  44.       //delay(1000);
  45.     } else {
  46.       isBeingPetted = false;
  47.       // kat wordt niet (meer) aangeraakt
  48.     }
  49.    
  50.     if (isBeingPetted == true){
  51.       changeEyes(purrEyes);
  52.       purr();
  53.     } else {
  54.       changeEyes(defaultEyes);
  55.       if (randomMeowTime <= 4){ //randomly meow if not being petted
  56.         changeEyes(meowEyes);
  57.         meow();
  58.       } else if (randomMeowTime == 5){
  59.         changeEyes(meowEyes);
  60.         meow2();
  61.       } else if (randomMeowTime == 6){
  62.         changeEyes(mewEyes);
  63.         mew();
  64.       }
  65.     }

  66.     totalSensorValue = 0;
  67.     count = 0;
  68.   }
  69.   
  70.   delay(1);
  71. }

  72. //the sounds the cat makes, the meowing sounds are from here: https://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds
  73. void meow() {  // cat meow (emphasis ow "me")
  74.   uint16_t i;
  75.   playTone(5100,50);        // "m" (short)
  76.   playTone(394,180);        // "eee" (long)
  77.   for(i=990; i<1022; i+=2)  // vary "ooo" down
  78.      playTone(i,8);
  79.   playTone(5100,40);        // "w" (short)
  80. }

  81. void meow2() {  // cat meow (emphasis on "ow")
  82.   uint16_t i;
  83.   playTone(5100,55);       // "m" (short)
  84.   playTone(394,170);       // "eee" (long)
  85.   delay(30);               // wait a tiny bit
  86.   for(i=330; i<360; i+=2)  // vary "ooo" down
  87.      playTone(i,10);
  88.   playTone(5100,40);       // "w" (short)
  89. }

  90. void mew() {  // cat mew
  91.   uint16_t i;
  92.   playTone(5100,55);       // "m"   (short)
  93.   playTone(394,130);       // "eee" (long)
  94.   playTone(384,35);        // "eee" (up a tiny bit on end)
  95.   playTone(5100,40);       // "w"   (short)
  96. }

  97. void purr() { // cat purr
  98.   uint16_t i;
  99.   uint16_t h;
  100.   for(i=0; i<10; i++){
  101.     playTone(9750,2);
  102.     playTone(9250,2);
  103.     delay(15);//was 6
  104.   }
  105.   for(i=0; i<10; i++){
  106.     playTone(11750,2);
  107.     playTone(11250,2);
  108.     delay(15);
  109.   }
  110. }

  111. void playTone(uint16_t tone1, uint16_t duration) {
  112.   if(tone1 < 50 || tone1 > 15000) return;  // these do not play on a piezo
  113.   for (long i = 0; i < duration * 1000L; i += tone1 * 2) {
  114.      digitalWrite(2, HIGH);
  115.      delayMicroseconds(tone1);
  116.      digitalWrite(2, LOW);
  117.      delayMicroseconds(tone1);
  118.   }     
  119. }

  120. //this method changes the eyes.
  121. void changeEyes(bool eyes[]){
  122.   for (int i = 0; i <= 5; i++){
  123.     if (eyes[i]==true){
  124.       digitalWrite(ledEyes[i], HIGH);
  125.     } else {
  126.       digitalWrite(ledEyes[i], LOW);
  127.     }
  128.   }
  129. }
复制代码



第四步:外形设计
创客们在设计一个项目的时候往往会忽视外形方面的设计,但外形设计实际上在创客项目上扮演着极其重要的角色。
而作为一只机器宠物猫,“萌值”的重要性想必是不用再提了。
这是作者的设计,看起来怎么样,如果觉得不顺眼,也可以自己修改或重新设计。
第五步:完成组装因为这个机器猫项目相对比较简单,组装也并不复杂,实际上就是把LED灯放入到猫眼部位就够了。鉴于Arduino的强大性能,实际上这个项目还有巨大的可扩展空间。比如通过和机器小车结合做一个可以运动的机器猫,或者和无人机结合做一只飞行的猫头鹰,甚至还能直接做成猫的形状,套上毛绒玩具就是一只如假包换的机器猫了,当然,做成哆啦A梦的样子也不错。接下来看一下展示吧:



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:560595 发表于 2019-7-17 17:48 | 只看该作者
厉害了
回复

使用道具 举报

板凳
ID:579057 发表于 2019-7-29 07:13 | 只看该作者
蓝猫
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表