arduino程序-MC猜数字1(基础知识)
1-18 MC猜数字0-介绍
按键开关和显示数字,每次按下开关会产生随机的数字
数字产生于arduino开发板
可以使用意念控制数字?(数字是随机的,如何做?,猜的准确)
1-19 MC猜数字1-电路搭建
制作一个可以进行猜数字游戏的制作
不仅搭建电路,还有更多知识
将arduino拿出来,实际动手搭建
此处只是构建电路,理论知识暂时放下
电路图
所需元件
10根杜邦线(公对公)
10根左右面包板跳线
一般论盒卖,连接面包板小孔
数码管
按键
电阻1个(200~500欧姆)
面包板
Arduino UNO R3*1
官方版,质量好,价格高,更容易配置,兼容性好与arduinoIDE
非官方的克隆版,价格低,可能需要单独安装驱动程序
搭建电路
请看视频
数码管上下各5个引脚
电阻一端插在上面5个引脚中间的那个引脚,另一端插在靠近蓝色的一排
找四个红色面包板跳线
电阻左右相邻的孔插入跳线一端,另一端引出
下方两个跳线插入到中间的孔的相邻两个孔,如图所示,中间下方的引脚留空
在拿出4根长的黄色跳线
插入如下
黄色线一端3号引脚,一端短红色线一端,如图
黄色线一端4号引脚,一端长橘黄色线一端,如图
绿色5号引脚
紫色6号引脚
黄色7号引脚,
橘黄色长线,8号引脚,
白色9号引脚
按键开关,相连不同侧,同侧不相连
一端2号引脚,一端按键开关
黑色导线,一端在接地引脚,另一端蓝色导轨一排
电路搭建好后,可通过太极创客网站测试程序测试
显示从0到9,
当按下按键开关显示是一个b字
此时电路搭建成功
相关程序
程序若是上方直接运行,显示效果并不好,程序与结果不一致
原因显示完1,7号引脚打开,再显示2的时候,没有对前面的如7号引脚进行关闭,这就导致在显示下一个数字的时候会被上方的影响到
如何做加入清理显示内容的函数,将3~9的引脚全部给低电平熄灭
每次显示完一个数字都进行内容清理
修改后效果如下:
完整代码如下:
http://www.taichi-maker.com/homepage/arduino-basic-tutorial-index/arduino-basic-tutorial-20/
/*
20 MC猜数字 - 2 LED数码管
太极创客
www.taichi-maker.com
此程序用于太极创客<<零基础入门学用Arduino教程>>
20 MC猜数字 - 2 LED数码管
课程的演示实验使用。
2017-04-21
*/
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
// 显示 1:
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
delay(500);
// 清理显示内容
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(500);
// 显示 2:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay(500);
// 清理显示内容
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(500);
// 显示 3:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(500);
// 清理显示内容
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(500);
}
思考:
显示到1,2,3,4,5,6,7,8,9,0数字
/*
19 MC猜数字 - 1 电路搭建
太极创客
www.taichi-maker.com
此程序用于太极创客<<零基础入门学用Arduino教程>>中
MC猜数字电路测试使用。
2017-04-21
*/
void setup() {
pinMode(2, INPUT_PULLUP);
for (int i = 2; i <= 9; i++) {
pinMode(i, OUTPUT);
}
}
// the loop function runs over and over again forever
void loop() {
int myNumber = 0;
while(myNumber <= 9){
displayNumber(myNumber);
delay(500);
displayClear();
myNumber++;
while(!digitalRead(2)){
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}
delay(500);
}
}
void displayClear(){
// 清理显示内容
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void displayNumber(int number){
switch(number){
case 1:
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
break;
case 2:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
case 3:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 4:
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
break;
case 5:
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 6:
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
case 7:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
break;
case 8:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
case 9:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 0:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
default:
digitalWrite(5, HIGH);
}
}