先放视频
故事背景:
在2030年,主人公程序员加利·史密斯(电影《机械姬》男一号)被某科技公司关在一个密室中,密室里有一个定时炸弹,且安装有超声波,如果有人靠近就会立即爆炸。密室内还有一个AI机器人“美佳侦探”(原型《机械姬》女一号人工智能机器人——伊娃),只有它知道密码箱的密码,密码箱中有出门钥匙。
史密斯要和“美佳侦探”交流沟通,在炸弹爆炸前问出密码,打开密码箱,逃出密室。
设计思路
1、AI机器人
AI机器人“美佳侦探”,使用一部旧手机,编程环境为MIT App Inventor2 WxBit 汉化增强版,网址:https://app.wxbit.com/login/,使用百度语音识别,百度语音合成,连接“图灵机器人开放平台”实现语音对话。并通过蓝牙传递指令,在每次AI机器人说前,让Arduino主控板驱动舵机,呈现耳朵摆动效果。
2、密码箱
密码箱,按数字键盘,有LED灯闪动提示,以“#”号结束输入,当密码正确后,电机驱动齿轮推出箱子。
3、定时炸弹
利用MAX7219LED点阵显示双位数字,实现倒计时。且加有超声波检测,当距离小于50cm时,立即爆炸。爆炸效果利用MP3播放模块播放爆炸音效实现。
整体呈现
左:定时炸弹,中:AI机器人,右:密码箱
制作过程:
1、AI机器人
硬件安装:
(1)安装电磁继电器,LED灯,电池。
(2)蓝牙音箱
(3)安装舵机
(4)安装Arduino主控板
(5)安装手机
程序设计:
arduino主控板程序设计
APP程序设计
界面设计
逻辑设计
具体程序可查看附件aia源代码,也可安装APK手机APP。
2、定时炸弹程序设计
“MAX7219LED点阵显示双位数字”可查看本人的另一个帖子:[高级教程]MAX7219LED点阵显示双位数字 http://mc.dfrobot.com.cn/thread-297748-1-1.html
#include <DFRobot_LedControl.h>
#include <DFRobot_SerialMp3.h>
#include <SoftwareSerial.h>
#include <DFRobot_Ultrasonic.h>
DFRobot_Ultrasonic ultraA0;
DFRobot_SerialMp3 serialMp3;
SoftwareSerial softSerialserialMp3(10, 11);
DFRobot_LedControl max7219(3, 5, 4);
const byte LedArray_left[10][8] = {{B00000000,B11100000,B10100000,B10100000,B10100000,B10100000,B10100000,B11100000},
{B00000000,B11000000,B01000000,B01000000,B01000000,B01000000,B01000000,B11100000},
{B00000000,B11100000,B00100000,B00100000,B11100000,B10000000,B10000000,B11100000},
{B00000000,B11100000,B00100000,B00100000,B11100000,B00100000,B00100000,B11100000},
{B00000000,B10100000,B10100000,B10100000,B11100000,B00100000,B00100000,B00100000},
{B00000000,B11100000,B10000000,B10000000,B11100000,B00100000,B00100000,B11100000},
{B00000000,B11100000,B10000000,B10000000,B11100000,B10100000,B10100000,B11100000},
{B00000000,B11100000,B00100000,B00100000,B00100000,B00100000,B00100000,B00100000},
{B00000000,B11100000,B10100000,B10100000,B11100000,B10100000,B10100000,B11100000},
{B00000000,B11100000,B10100000,B10100000,B11100000,B00100000,B00100000,B11100000}};
const byte LedArray_right[10][8] = {{B00000000,B00000111,B00000101,B00000101,B00000101,B00000101,B00000101,B00000111},
{B00000000,B00000110,B00000010,B00000010,B00000010,B00000010,B00000010,B00000111},
{B00000000,B00000111,B00000001,B00000001,B00000111,B00000100,B00000100,B00000111},
{B00000000,B00000111,B00000001,B00000001,B00000111,B00000001,B00000001,B00000111},
{B00000000,B00000101,B00000101,B00000101,B00000111,B00000001,B00000001,B00000001},
{B00000000,B00000111,B00000100,B00000100,B00000111,B00000001,B00000001,B00000111},
{B00000000,B00000111,B00000100,B00000100,B00000111,B00000101,B00000101,B00000111},
{B00000000,B00000111,B00000001,B00000001,B00000001,B00000001,B00000001,B00000001},
{B00000000,B00000111,B00000101,B00000101,B00000111,B00000101,B00000101,B00000111},
{B00000000,B00000111,B00000101,B00000101,B00000111,B00000001,B00000001,B00000111}};
byte show[]={B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
void setup() {
max7219.begin();
serialMp3.begin(&softSerialserialMp3);
serialMp3.play();
serialMp3.volume(99);
ultraA0.begin(A0);
}
void loop() {
int m;
int n;
for (int i = 99; i> 0;i--) {
m=int(i/10);
n=i%10;
for (int j = 0; j < 8; j++) {
show[j]=LedArray_left[m][j]+LedArray_right[n][j];
}
max7219.show(show);
if ((ultraA0.getDistanceCm() < 50)) {
break;
}
else{
delay(1000);
}
}
serialMp3.playList(17);
delay(5000);
}
3、密码箱程序设计
密码箱的设计,可能相看本人的另一个帖子:[入门教程]“4*4 薄膜数字键盘”使用教程http://mc.dfrobot.com.cn/thread-297845-1-1.html#pid426018
#include <Arduino.h>
char adminPassword[5] = "4869";//设置密码
char inputPassword[5] = " ";//存放输入密码,位数为5,最后一位放置'/0'
unsigned char i = 0;
unsigned char KeyCodeMap[4][4] = {
{0x31,0x32,0x33,0x41},//1、2、3、A
{0x34,0x35,0x36,0x42},//4、5、6、B
{0x37,0x38,0x39,0x43},//7、8、9、C
{0x2A,0x30,0x23,0x44}//*、0、#、D
};
unsigned char KeySta[4][4] = {
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}
};
boolean matchFlag = 0;//比对标识
void pinmode()//引脚初始化
{
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void scanKeyboard() {//扫描按键
char j=0;
digitalWrite(i+6,LOW);//引脚6,7,8,9设置低电平,按行扫描
for(j = 0; j < 4; j++) {
KeySta[i][j] = digitalRead(j+2);
}
digitalWrite(i+6,HIGH);
i++;
if(i==4) {
i=0;
}
}
void checkKeySta() {
static unsigned char KeyStaPrevious[4][4] = {
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}
};
char j = 0;
for(j = 0; j < 4; j++) {
if(KeyStaPrevious[i][j] != KeySta[i][j]){
if(KeyStaPrevious[i][j] == 0){//当相应按键被按下
digitalWrite(10,HIGH);
delay(200);
digitalWrite(10,LOW);
KeyAction(i,j);
}
KeyStaPrevious[i][j]=KeySta[i][j];
}
}
}
void KeyAction(char i,char j){
for(char k = 1;k<5;k++ ){//将输入的密码移位存储
inputPassword[k-1]=inputPassword[k];
}
if(KeyCodeMap[i][j] == 0x23){//以“#”号做为输入结束符
inputPassword[4]='\0';
matchFlag = compare();
clearInput();
}
else{
inputPassword[4] = KeyCodeMap[i][j];
}
}
boolean compare(){//输入的密码与设定密码比较
String a,b;
a=inputPassword;
b=adminPassword;
if(a==b){
return 1;
}
else {
return 0;
}
}
void clearInput(){//清空输入密码
for(char j = 0; j < 5; j++){
inputPassword[j] = ' ';
}
}
void checkMatchFlag(){
if(matchFlag){//比对成功设置相应引脚
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
delay(500);
digitalWrite(11,LOW);
delay(3000);
digitalWrite(10,LOW);
matchFlag = 0;
}
}
void setup() {
pinmode();
}
void loop() {
scanKeyboard();//扫描键盘
checkKeySta();//记录按下的键码
checkMatchFlag();//密码比对
}
注:本文使用的密码“4869”来历
1、名侦探柯南里,日语4869的谐音就是福尔摩斯;
2、工腾新一喝的药叫做APTX4869;
3、柯南的手机密码就是4869;
汤果2019.09.08
超级科幻~~