极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10967|回复: 1

制作一个电子骰子(1)

[复制链接]
发表于 2014-7-3 09:03:56 | 显示全部楼层 |阅读模式
有一种说法,说赌场的英文单词 CASINO 是中国人发明的:

“据说这也是中国人发明的。十八世纪,美国从中国广东引进了大批的劳工修铁路。美国的西部牛仔就开设赌局,赚这帮广东人的血汗钱。他们白天干活,晚上赌博消磨时光。一到傍晚赌局开始的时候,这帮劳工就争相吆喝“开始了”___石毓智:也谈华人为什么好赌 http://t.cn/zYOPz3e”。

这样的解释听上去有些道理,“开始喽”发音也和 Casino 很类似.......经过考证非常遗憾实际上并不是这样,具体的来源是意大利。【参考1】

骰子是一种简单方便的赌博工具,具有体积小便于携带,低噪音,节能环保,便于广大人民群众学习裂解等等特点,记得高中时代,电视热播《赌神》《赌圣》系列,我们班上的几个同学放课后便去学校边上的小公园,模仿电影中的样子猜大小,赌注不大也觉得刺激。当然,作为一个好孩子是不会直接参加赌局的,通常只是提供用具偶尔抽水而已.......



这里介绍用 arduino 做一个电子的骰子,材料清单如下:

Arduino Uno x1
Max7219 + 8x8 LED点阵 x1
按键开关 x1
杜邦线 若干

Max7219 + 8x8 LED点阵是下面这个样子,用来显示当前的点数。当然,还以用数码管,只是那样看起来太不专业了。



电路方面,连接如下:

按钮三个Pin很简单,分别连到Andruino上的 GND,3.3V 和 D2

MAX7219五个Pin连接如下

VCC  <-----> 5V
GND  <-----> GND
DIN    <-----> D12
CS     <-----> D10
CLK   <-----> D11

下面的程序核心有2部分,一部分是用来处理按下按钮时,让LED看起来在不断跳动;另外一部分是直接生成最终的结果。这样做的原因是为了保证概率上的公平以及代码的简洁。

<code>

/*
日期:2014-7-2
功能:MAX7219驱动8*8点阵
作者:Z.t
IDE:1.0.5
硬件:最小系统UNO
说明:本代码主要参考了官方的示例程序
*/

//官方的库
#include "LedControl.h"

/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
第一个参数:pin 12 is connected to the DataIn
第二个参数:pin 11 is connected to the CLK
第三个参数:pin 10 is connected to LOAD
第四个参数:We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=50;
int ButtonPin=3;
  int Current=1;

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);

  randomSeed(analogRead(0));
  pinMode(ButtonPin, INPUT);   
}


void showNum(int x) {
  /* here is the data for the characters */
  byte one[8]={     
B00000000,
B00000000,
B00000000,
B00111000,
B00111000,
B00000000,
B00000000,
B00000000};

  byte two[8]={   
B00000000,
B00000110,
B00000110,
B00000000,
B00000000,
B01100000,
B01100000,
B00000000};

  byte three[8]={   
B00000000,
B00111000,
B00111000,
B00000000,
B01100110,
B01100110,
B01100110,
B00000000};

  byte four[8]={   
B00000000,
B01100110,
B01100110,
B00000000,
B00000000,
B01100110,
B01100110,
B00000000};

  byte five[8]={   
B00000000,
B01100110,
B01100110,
B00011000,
B00011000,
B01100110,
B01100110,
B00000000};

  byte six[8]={   
B01100110,
B01100110,
B00000000,
B01100110,
B01100110,
B00000000,
B01100110,
B01100110};

switch (x) {
case 1:
  lc.setRow(0,0,one[0]);
  lc.setRow(0,1,one[1]);
  lc.setRow(0,2,one[2]);
  lc.setRow(0,3,one[3]);
  lc.setRow(0,4,one[4]);
  lc.setRow(0,5,one[5]);
  lc.setRow(0,6,one[6]);
  lc.setRow(0,7,one[7]);
  break;
case 2:
  lc.setRow(0,0,two[0]);
  lc.setRow(0,1,two[1]);
  lc.setRow(0,2,two[2]);
  lc.setRow(0,3,two[3]);
  lc.setRow(0,4,two[4]);
  lc.setRow(0,5,two[5]);
  lc.setRow(0,6,two[6]);
  lc.setRow(0,7,two[7]);
  break;
case 3:
  lc.setRow(0,0,three[0]);
  lc.setRow(0,1,three[1]);
  lc.setRow(0,2,three[2]);
  lc.setRow(0,3,three[3]);
  lc.setRow(0,4,three[4]);
  lc.setRow(0,5,three[5]);
  lc.setRow(0,6,three[6]);
  lc.setRow(0,7,three[7]);
  break;

case 4:
  lc.setRow(0,0,four[0]);
  lc.setRow(0,1,four[1]);
  lc.setRow(0,2,four[2]);
  lc.setRow(0,3,four[3]);
  lc.setRow(0,4,four[4]);
  lc.setRow(0,5,four[5]);
  lc.setRow(0,6,four[6]);
  lc.setRow(0,7,four[7]);
  break;
case 5:
  lc.setRow(0,0,five[0]);
  lc.setRow(0,1,five[1]);
  lc.setRow(0,2,five[2]);
  lc.setRow(0,3,five[3]);
  lc.setRow(0,4,five[4]);
  lc.setRow(0,5,five[5]);
  lc.setRow(0,6,five[6]);
  lc.setRow(0,7,five[7]);
  break;

case 6:
  lc.setRow(0,0,six[0]);
  lc.setRow(0,1,six[1]);
  lc.setRow(0,2,six[2]);
  lc.setRow(0,3,six[3]);
  lc.setRow(0,4,six[4]);
  lc.setRow(0,5,six[5]);
  lc.setRow(0,6,six[6]);
  lc.setRow(0,7,six[7]);
  break;
}
}


void loop() {
  int Next;
  boolean MarkStart=false;  //标记是否按键抬起

  if (digitalRead(ButtonPin)==LOW) {
      showNum(Current);
      do {
         Next=random(1,7);
         }  
      while (Current==Next); //因为如果两次出现相同的数字,看起来
                             //会觉得没有变,所以这里要保证生成不同
      Current=Next;
      delay(delaytime);
    MarkStart=true;  
  }

  if ((MarkStart==true) && (digitalRead(ButtonPin)==HIGH)){  //按键抬起,生成实际显示的结果
        MarkStart=false;
        showNum(random(1,7));
        }
}

</code>


&nbsp;

&nbsp;

<span style="line-height: 1.714285714; font-size: 1rem;">程序设计上有个很有趣的地方,</span>

之前我是使用下面的代码来做最后的显示,但是经过考虑这是不妥当的,你能说出为什么要修改吗?答案见最下面。

if ((MarkStart==true) && (digitalRead(ButtonPin)==HIGH)){  //按键抬起,生成实际显示的结果
MarkStart=false;
showNum(Next);
}

要是有个外壳能更好看一点



程序下载

参考:
1.http://blog.sina.com.cn/s/blog_507de1780102eyec.html 程阳:赌场的英文单词 CASINO 来源自哪?
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;





























因为这样做导致结果不公平,比如我在抬起按键之前看到点数是 6,那么最后出现的结果肯定是 1-5 之间,无形之中导致了不公平.......

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-7-3 11:11:51 | 显示全部楼层
因为这样做导致结果不公平,比如我在抬起按键之前看到点数是 6,那么最后出现的结果肯定是 1-5 之间,无形之中导致了不公平.......


那就把按钮引出来,然后扣上一个真正的骰盅再按。你的担心就不存在了,还增加了真实感。
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 09:06 , Processed in 0.066365 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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