Step to UEFI (16) —– CLIB下获得 SystemTable

在引入 CLIB 之前,获得 SystemTable 等等是非常简单的事情,入口参数上直接就能取得。但是引入了 CLIB 之后似乎没有那么直白了。研究了一下发现,用下面的语句可以轻松解决:

extern EFI_SYSTEM_TABLE *gST;

随手写个程序测试

/** @file
    A simple, basic, application showing how the Hello application could be
    built using the "Standard C Libraries" from StdLib.

    Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials
    are licensed and made available under the terms and conditions of the BSD License
    which accompanies this distribution. The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include <wchar.h>

extern EFI_SYSTEM_TABLE             *gST;

/***
  Demonstrates basic workings of the main() function by displaying a
  welcoming message.

  Note that the UEFI command line is composed of 16-bit UCS2 wide characters.
  The easiest way to access the command line parameters is to cast Argv as:
      wchar_t **wArgv = (wchar_t **)Argv;

  @param[in]  Argc    Number of argument tokens pointed to by Argv.
  @param[in]  Argv    Array of Argc pointers to command line tokens.

  @retval  0         The application exited normally.
  @retval  Other     An error occurred.
***/
int
EFIAPI
main (
  IN int Argc,
  IN char **Argv
  )
{
	gST -> ConOut -> OutputString (gST->ConOut,L"Hello,www.lab-z.com\n");
	
  return EFI_SUCCESS;
}

 

运行结果

gst

程序太简单就不上了。

【补充】在 http://biosren.com/viewthread.php?tid=7295&highlight= 有个朋友问了同样的问题,最后他的解决方式是:

加入头文件 #include Library/UefiBootServicesTableLib.h ,这个头文件的内容是

#ifndef __UEFI_BOOT_SERVICES_TABLE_LIB_H__
#define __UEFI_BOOT_SERVICES_TABLE_LIB_H__

///
/// Cache the Image Handle
///
extern EFI_HANDLE gImageHandle;

///
/// Cache pointer to the EFI System Table
///
extern EFI_SYSTEM_TABLE *gST;

///
/// Cache pointer to the EFI Boot Services Table
///
extern EFI_BOOT_SERVICES *gBS;

#endif

所以和前面提到的解决方法还是相同的

使用Arduino直接发声

通常如果想让Arduino发出声音需要额外的配备,比如:Mp3解码器,Wav专用播放器或者语音合成的模块等等。

但是理论上因为Arduino具有模拟输出,所以应该可以直接输出波形给喇叭(这个还是必须的,我随便选了一个8欧1.5瓦的)。

arduinos

随手搜索了一下,国外真有人这样做了。原理上来说就是先用工具将音频转化为WAV, Arduino 的存储空间有限,这里只能使用单声道 8000Hz 采样率,然后通过控制模拟端口将数据发送出来。从我的实验来看,Arduino Uno(Flash Memory 32 KB 【参考1】)可以存放大约4s左右的音频(编译之后在 31K左右)。

具体的做法是:

1.硬件方面,喇叭负极连接到GND,正极连接到Pin11

2.在Arduino程序中使用下面这个库

damellis-PCM-ae3f463

3.选择音频文件,然后转化为 WAV 8000Hz Mono 格式(我Switch Sound File coverter 感觉不错,免费的)

wavcover

软件下载 switchsetup

4.最后用参考2提供工具,将wav转化为数组的定义(需要注意这个工具需要 javaw.exe 支持)

EncodeAudio-windows

5.编译之后 Upload 即可

用Windows自带的录音机录下来的

狼嚎

工具转化为 wav 8K mono

狼嚎

最后生成的程序

playback

做出来的样子

http://www.tudou.com/programs/view/SzuxR6-7oKI/?resourceId=414535982_06_02_99

参考:

1.http://kb.open.eefocus.com/index.php?title=Arduino_Uno Arduino Uno

2.http://highlowtech.org/?p=1963

soundarduino

===============================================================================
2015.2.23 补充:我在编译时发生“error: ‘OCR2A’ undeclared (first use in this function)” 这样一系列的错误,搜索了一下,是因为最近一直在玩 Arduino Pro Micro,它的芯片是 ATmega32u4 ,而这个芯片没有 Timer2 , 只有 0, 1, 3 和 4。IDE中重新切换回Uno再次编译即可。参考 http://forum.arduino.cc/index.php?topic=237770.0

再补充参考2给出来的那个转换工具

EncodeAudio-windows

再给出一个完整的Arduino程序例子
playback1

红外无线温度计

前一段,给Arduino做了一个外壳。材料是有机玻璃再加上 3mm 的铜柱螺丝和螺母的组合,做出来效果还不错。最明显的就是放到论坛上会有很多人关注和讨论。相比OpenCV统计骰子点数的装置,这个盒子的人气高很多。【参考1】

淘宝上有更专业的Arduino外壳,只是价格比较贵。有兴趣的朋友可以像我这样,买一块有机玻璃,再买一把各种螺钉螺母的。

shellarduino

果壳网近期有个手机装置设计大赛,翻翻箱子看看手上有什么能够用起来的东西。最后决定做一个“无线温度计”。除了上面的外壳,还有如下材料:

Arduino Uno
TN901 红外温度传感器
蓝牙模块

简单介绍一下 TN901 ,实物如图(实际我使用的比下面的长一截,多出来一段电路板,不知道是不是考虑到太短了用起来容易焊接不亮,或者容易头重脚轻)

tn901

上图从左到右分别是 A:测试 G:地 C:时钟 D:数据 V:电源【参考】。从另外一个方向看过去,顺序刚好颠倒。用的时候先看一下电路板上哪里是 A 哪里是 V 即可确认方向。刚开始的时候我就接反了,不工作,吓得我一身冷汗(这个小东西要 150元,是我一个月购买材料budget。开发品就是贵啊。相比之下,淘宝上还有50元的测温枪,不同的传感器而已)
论坛上有人之前做过【参考1】,有现成的库可以使用【参考2】,方便啊!
连接上蛮简单,VCC供电(建议选 5V),GND接好之后,剩下三根线,根据库初始化时的提示分别将 Data CLK ACK 连接到 Pin 13 12 11。连接好之后,在串口就能看到数据了。

下面再引入蓝牙模块。入手的是专门Arduino 使用的。简单的说Arduino蓝牙模块=单板+底板。而我刚开始入手的是单板。入手单板之后有点傻眼:小是够小,但是没引出来需要的Pin脚。最后还是买了一个专门 Arduino 使用的了事。

有专门的 TN901的库,因此 Arduino 程序编写要简单的多。

#include <Arduino.h> 
#include <Wire.h>
#include "TN901.h"  //引用库文件
TN901 tn;           //实 例 化
void setup()
{
    Serial.begin(9600);
    tn.Init(13,12,11);  //初 始 化 data clk ack
}
void loop()
{
   tn.Read();
   SerialValue();
   delay(2000);
}

void SerialValue()
{
   String s;
   s=">O"+String(tn.OT, DEC)+"E"+String(tn.ET,DEC)+"<"; 
   Serial.println(s);
   //Serial.print(tn.OT, DEC);
   //Serial.print("ET");
   //Serial.print(tn.ET, DEC);
}

 

出来的两个温度分别是目标温度(tn.OT)和当前的环境温度(tn.ET)。上面的程序将结果放在一个下面这样 >Oxx.xxEyy.yy< 的字符串中发送到串口上。再通过串口蓝牙,我们的电脑就能够收到结果了。

dPsth1-sLLasZ1cLCQ3rMfmbswWAUts6clUKxhVVkLTRAQAAZwIAAFBO

gzpTD5eLLIU02MC-PJLVKXZdGj_FxCdSvfSsH1W16H6QBwAAIAoAAEpQ

供电部分使用的是一个USB充电宝,内部是一节 16580 电池。体积适中,价格也不贵。

FkSGBq2NsJR46BrDWXQG6geid3rNpRCUh42Eeq5dNSqAAgAA4AEAAEpQ

电脑端,使用Delphi 设计了一个简单的界面,用类似任务管理器的界面来展示当前的和部分的历史数据

x4k4h5l-_l7mJqz3KqnSmvqPMmoxITAwhYTGsStQ874BBgAAhAMAAFBO

为了方便起见同时还使用了 CPort VCL 来作为串口通讯。但是个人感觉不是很稳定,因此有机会还是直接使用 API 爱进行通讯比较好。程序下载 IRTemp。Arduino相关库下载:tn9

最后还拍摄了一个应用的演示视频。

http://www.tudou.com/programs/view/dDu_7_c58TA/?resourceId=414535982_06_02_99

参考:
1. http://www.geek-workshop.com/thread-10726-1-1.html用有机玻璃做个arduino外壳

2. http://www.geek-workshop.com/thread-8727-1-1.html DIY红外非接触式温度计

3. http://www.geek-workshop.com/thread-1884-1-1.html如何自己编写Arduino支持的C++类库

4. http://www.zytemp.com/products/tn901.asp Tn9 usermanual

Arduino接收串口字符的问题

随手写了一个小程序,目标是串口收到:‘b’关闭 Pin 13 的LED;当收到 ‘s’ 是打开 LED.

程序如下,很奇怪的现象是:板子上 PIN 13 的LED一直是熄灭的。有兴趣的朋友在看答案之前可以先自己琢磨一下。

#define LED_PIN 13

void setup()
{
    Serial.begin(9600);
    pinMode(LED_PIN, OUTPUT);  
}

void loop()
{
    while (Serial.available() > 0)  
    {
        if ('b'==Serial.read();) 
          {
            digitalWrite(LED_PIN, LOW);
          }
        if ('s'==Serial.read();) 
          {
            digitalWrite(LED_PIN, HIGH);            
          }
    }
}

 

1022186_12845416716A28

真相只有一个,当 Serial Port Available之后,如果你取走了字符,下一个判断就是空了,所以第二个条件永远无法满足……

#define LED_PIN 13

void setup()
{
    Serial.begin(9600);
    pinMode(LED_PIN, OUTPUT);  
}

void loop()
{
  char  c;
  
    while (Serial.available() > 0)  
    {
        c=Serial.read();
        if ('b'==c) 
          {
            digitalWrite(LED_PIN, LOW);
          }
        if ('s'==c) 
          {
            digitalWrite(LED_PIN, HIGH);            
          }
    }
}

 

带有自动提示功能的手机座

起因:有网友抱怨,因为陪着孩子在家玩,漏接了公司的重要电话,被罚款500(如果真的是人民币的话,我相信他一定乐于看到这个产品)

看到这个需求,我忽然想是否可以用 Arduino来打造一个能够提醒的盒子呢? 当然,解决方法还有很多,严小姐听了我的目标之后第一个反应是为什么不换一个山寨机,大屏幕啊有没有,超长待机啊有没有,八个喇叭啊有没有,只要499啊有没有。我回答有很多人喜欢老物件有感情而已。她又在问为什么,终于被我意味深长的眼神说服了。

下面就开始动手实做了。从触发的角度来说最稳妥的办法还是安装APP,Android和iOS都是可行的,只是…….默默的轻抚了一下自己的 Nokia E72,放弃了这个方案。翻翻元件箱,里面有振动开关,那就凑合着用吧。提示方面,最近入手了一个具有录音放音功能的模块ISD1820,使用非常简单,GPIO足以搞定(多说一句,上面有麦克风和一个录音按钮,按下这个按钮对着它高歌一曲即记录在芯片中。时间比较短,只能记录10秒)。选用了 8×8 LED模块作为提示灯光,它在之前的《电子骰子系列》登场过。

原理上很简单,就是检测连续振动确定是振铃–>触发装置,每隔10秒提示一次–>如果有人按下Stop按钮就停止提示。

用到的配件

IMG_2553

用面包板来验证电路

IMG_2502

 

侧面有一个喇叭,一个按钮

IMG_2517

 

正面俯视,有一个 8×8点阵

 

IMG_2518

 

振动传感器

IMG_2550

 

内部线路看起来有点复杂

IMG_2551

 

另外一边做出一个洞,给 Arduino供电

 

IMG_2516

 

#include "LedControl.h"

const int ShockTestPin = 4; //Pin for vibration sensor
const int AlartPin = 5;     //LED Alert
const int StopPin = 6;      //Stop button
int sta=0;

//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);

void showface() {
/* here is the data for the characters */
byte face[8]={
B00000100,
B00100010,
B01000110,
B01000000,
B01000000,
B01000110,
B00100010,
B00000100};

lc.setRow(0,0,face[0]);
lc.setRow(0,1,face[1]);
lc.setRow(0,2,face[2]);
lc.setRow(0,3,face[3]);
lc.setRow(0,4,face[4]);
lc.setRow(0,5,face[5]);
lc.setRow(0,6,face[6]);
lc.setRow(0,7,face[7]);

}

void setup() {

  Serial.begin(9600);

  pinMode(ShockTestPin, INPUT);   
  pinMode(AlartPin,OUTPUT);
  digitalWrite(AlartPin,LOW),
  pinMode(StopPin,INPUT);  //Stop pin for input

  //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);

} // End of Setup

void loop() {
 long int start;
  
  if (0==sta) {       //Status One: Test status
     boolean shock=false;
     
     start=millis();
     while (millis()-start<=1000L) { //Test vibration for 1 second 
        if (LOW==digitalRead(ShockTestPin)) {  //If there is virbration signal
          shock=true;                          //Mark true
          break;
        } //if (LOW==digitalRead(ShockTestPin))
     }   
     if (false==shock) {sta=0; goto Sta1End;}    //If in the one seconds there is no vibration, it should break
     delay(500);
     
     start=millis();
     while (millis()-start<=1000L) { //Test vibration for 1 second 
        if (LOW==digitalRead(ShockTestPin)) {  //If there is virbration signal
          shock=true;                          //Mark true
          break;
        } //if (LOW==digitalRead(ShockTestPin))
     }   
     if (false==shock) {sta=0; goto Sta1End;}    //If in the one seconds there is no vibration, it should break
     delay(500);

     start=millis();
     while (millis()-start<=1000L) { //Test vibration for 1 second 
        if (LOW==digitalRead(ShockTestPin)) {  //If there is virbration signal
          shock=true;                          //Mark true
          break;
        } //if (LOW==digitalRead(ShockTestPin))
     }   
     if (false==shock) {sta=0; goto Sta1End;}    //If in the one seconds there is no vibration, it should break
     delay(500);
    
     //It means we have test vibration in 3 seconds 
     sta=1;
  Sta1End:
      ;  
  }
  
  if (1==sta) {
      //Play the voice that we have record
      digitalWrite(AlartPin,LOW);
      digitalWrite(AlartPin,HIGH);
      delay(100);
      digitalWrite(AlartPin,LOW);
      
      //Delay 10 seconds
      start=millis();
     
      while (millis()-start<=15000L)
      {
        if (LOW==digitalRead(StopPin)) { //We will stop the playing ,if we test the stop button is pressed
          sta=0;
          lc.clearDisplay(0);   
          break;
        }
        if (0==(millis()-start)/1000L % 2) {
           showface(); 
        }
       else
        {
            lc.clearDisplay(0);          
        } 
      }//while (millis()-start<=15000L)

  }//if (1==sta)
}

 

具体的使用方法拍了一个简单的视频,本人倾力献

最后放两张靓照

IMG_2544

IMG_2547

这只是很粗糙的原型,如果真想做出来实用的还有很长的路要走。比如:让检测更灵敏更可靠,或者干脆使用手机的声音作为触发条件。提示音量再大一些。或者再加上充电功能就更加实用,也给需要一直插着让Arduino供电找到更好的借口。

然后严小姐的问题是:如果忽然有很强的振动,误触发了怎么办,如果家里确实没有人它又一直在叫怎么办,如果隔壁在装修,有振动有噪音怎么办?好吧,如果你肯再品准500+以上的预算,让我买块蓝牙开发板,我相信一定不是问题。哦,对了还要换一个支持Android的手机才好啊。

Arduino制作一个电子骰子(3)

上面几篇文章介绍的都是虚拟的电子骰子,这篇文章将会介绍用Arduino来玩实体的骰子。相比前面几篇材料更复杂,牵涉到的配备更多。当然,所有的技术问题都能找到合理的解释,暂时无法回答的问题是“这个东西能干什么?”

首先介绍使用到的材料:

1. 电动骰子机(toabao购买,电池驱动,上面有个按钮,按动时自动旋转骰子)
2. 继电器 (我们用它来控制上面骰子机的开关)
3. 电池夹(用来给骰子机供电)
4. Arduino UNO (最基础的型号足够用的,如果你有其他的型号肯定也没问题)
5. 摄像头 (我选用的是 罗技的 U-VQN42,这是很老的型号,以至于目前没有Win7的驱动)
6. 电脑 (我们用摄像头采集图像,然后使用Intel 的OpenCV来进行图像的识别处理)

可以看出这个实验涉及到的内容远远比之前的复杂,设计这个实验大约花费了2周左右,实做将近两天。

从流程上来说,功能过程如下:

Drawing1

硬件部分的介绍:
介绍Arduino的连接:

Untitled Sketch_bb

虽然骰子机内部是要安装2节电池的,但是我发现我用一节五号充电电池也能让它工作的很好,考虑到外壳强度的问题,也就直接选择一节电池了。Arduino在这里就是发挥骰子机开关的功能。

此外的建议是:连接继电器的常断开端口,这样,当断电时骰子机不会转动。

image003

骰子机原来的是亚克力外壳,因为反光的问题,在测试时结果不理想,所以重新用纸做了一个。这里特别感谢严小姐,她用灵巧的双手帮我完成了这个工作。可以看到,目前整个摄像头可以伸进去,视界中只有地盘和白色的区域,这对于识别是非常有利的。还建议使用一个外部固定光源来进行补光之类的设定。

image004

软件部分:

1. Arduino程序。

#define LED_PIN 13

void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
//Default
digitalWrite(LED_PIN, HIGH);
}

void loop()
{
char c;

while (Serial.available() > 0)
{
c=Serial.read();
if (‘b’==c)
{
digitalWrite(LED_PIN, LOW);
}
if (‘s’==c)
{
digitalWrite(LED_PIN, HIGH);
}
}
}

实验中 Arduino 的程序非常简单,测试时可以使用 Putty 这样的工具打开对应串口,直接发送命令测试。

2. PC端程序。

因为要使用OpenCV,所以选择了VS2008,本打算使用Delphi,无奈他的OpenCV不成熟。

这个程序可能分成两部分,一部分是串口通讯,只需要发送部分【参考1】,另外一部分是OpenCV的识别。算法上来自【参考2】,他给出了一种非常简单的算法:首先对图像进行Canny边缘提取,然后使用FloodFill对于包络范围进行填充,同时会返回填充区域的面积,利用面积就可以判断识别出来的是否为骰子的点(例如:1点的时候面积可能在200左右,2点的话,每个点可能在50左右),对于处在合理面积中的识别区域就判断为点数。

#include “stdafx.h”
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include “opencv2/highgui/highgui.hpp”
#include

using namespace cv;

int p1=49, p2=258; //Paramters for Canny
Mat framepic; //framepic 存放摄像头抓取图像
Mat framegray; // framepic 灰度化后的图像
Mat canny; //canny 存放摄像头处理后的图像
VideoCapture cap(0);
int count[7]={0,0,0,0,0,0,0};
int counter=0;
int retry=0;

void on_trackbar(int, void*) {
Canny(framegray, canny, p1, p2);
imshow(“canny”, canny);
}

void delayshow(int dSecond){
time_t sTime=time(NULL);

while (difftime(time(NULL),sTime)<dSecond)
{
cap>>framepic;
cvtColor(framepic,framegray, CV_BGR2GRAY);
GaussianBlur(framegray, framegray,Size(7,7),1.5,1.5);
Canny(framegray, canny, p1, p2);
imshow(“canny”, framegray);
imshow(“Preview”,canny);
int c=waitKey(33);
if(c==27) break;
}
}

int main(int argc, const char** argv)
{
//==============================================
//code for COM port communication Begin
//==============================================
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR SPort[]=L”\\\\.\\COM7″;

char txcharstart[]=”b”; //b char for Start shaking dice
char txcharstop []=”s”; //s char for Stop shaking dice

DWORD iBytesWritten;

//Prepare COM part communication
hCom = CreateFile(SPort,
GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf (“[Error] CreateFile failed with error %d.\n”, GetLastError());
return (1);
}

SetupComm(hCom, 32, 32);

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf (“[Error] GetCommState failed with error %d.\n”, GetLastError());
return (2);
}

// Fill in DCB: 9600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf (“[Errror] SetCommState failed with error %d.\n”, GetLastError());
return (3);
}
//==============================================
//code for COM port communication End
//==============================================

srand(time(NULL));

int dTime;
if(!cap.isOpened())
return -1;

cap>>framepic;
cvtColor(framepic,framegray, CV_BGR2GRAY);
Canny(framegray, canny, p1, p2);
imshow(“canny”, framegray);
imshow(“Preview”, framegray);

//You may have to find the P1 and P2 value for your environment
createTrackbar(“p1″,”canny”,&p1,1000,on_trackbar);
createTrackbar(“p2″,”canny”,&p2,1000,on_trackbar);

//Loop shaking dice and count
for(;;)
{
//Shake start
printf (“[Message] Send start command \n”);
if (FALSE==WriteFile(hCom, &txcharstart, strlen(txcharstart), &iBytesWritten,NULL)) {
printf(“[Error]: %d”, GetLastError());
retry=0;
while ((TRUE==WriteFile(hCom, &txcharstart, strlen(txcharstart), &iBytesWritten,NULL)) ||
(retry>5))
{
printf(“[Error]: %d”, GetLastError());
retry++;
}
}

dTime=rand()%2+1;

//Shake the dice for a random seconds
printf(“Shake delay %d \n”,dTime);
delayshow(dTime);

//Shake stop
printf (“[Message] Send stop command \n”);
if (FALSE==WriteFile(hCom, &txcharstop, strlen(txcharstop), &iBytesWritten,NULL)) {
printf(“[Error]: %d”, GetLastError());
retry=0;
while ((TRUE==WriteFile(hCom, &txcharstop, strlen(txcharstop), &iBytesWritten,NULL)) ||
(retry>5))
{
printf(“[Error]: %d”, GetLastError());
retry++;
}
}

//It delay 5 seconds for dice stopping
delayshow(5);

//==============================================
//code dice point recognize start
//==============================================
cap>>framepic;
cvtColor(framepic,framegray, CV_BGR2GRAY);
GaussianBlur(framegray, framegray,Size(7,7),1.5,1.5);
Canny(framegray, canny, p1, p2);
imshow(“canny”, framegray);
imshow(“Preview”,canny);

int num = 0;
for(int y=0;y<canny.size().height;y++)
{
uchar *row = canny.ptr(y);
for(int x=0;x<canny.size().width;x++)
{
if(row[x] <= 128)
{
int area = floodFill(canny, Point(x,y), CV_RGB(0,0,160));
printf(“filling %d, %d gray, area is %d\n”, x, y, area);
if(area>37 && area < 300) num++;
}
}
}
printf(“number is %d\n”, num);
//==============================================
//code dice point recognize end
//==============================================
counter++;
count[num]++;
printf(“=====================================================\n”);
printf(“Total=%4d| One | Two | Three | Four | Five | Six |\n”,counter);
printf(” | %4d|%4d | %4d | %4d | %4d | %4d|\n”,count[1],count[2],count[3],count[4],count[5],count[6]);
printf(“=====================================================\n”);
printf(“Show result delay 3s\n”);
delayshow(3);
int c=waitKey(33);
if(c==27) break;
}

system(“PAUSE”);
CloseHandle(hCom);

return 0;
}

运行结果就是下面这个样子。Console 显示统计结果,旁边一个显示灰度化后的摄像头内容,另外一个显示边缘提取后的结果。

image005

我只做了一个多小时,收集的结果不多,看起来结果并不平均,不知道如果做个一万次之类的会是什么结果。

运行时的视频(刚开始放在Tudou的时候,审核没有通过,估计是因为名称中有“骰子”。后来申诉了一下)

http://www.tudou.com/programs/view/TtqRy-KRr-8/?resourceId=414535982_06_02_99

其实更好的方案是记录下每次的运行结果,因为我发现这个东西每次之间似乎有相关性。比如说这次出现六点,那么下次出现六点的可能性似乎大于 1/6。猜测和骰子机的摇骰子方式有相关性。

最后的最后,吐槽一下电脑的问题:第一个没想到是前面说过的罗技摄像头居然不支持Windows 7,如果你也想做同样的实验需要购买摄像头不妨考虑一下买个二手的微软摄像头,只是外观差一些,效果和支持应该比罗技的好多了;第二个是梅捷 SY-D2700-U3M 的主板,BIOS中居然没有设计风扇温控功能【参考4 提到的Watchdog就是为他设计的】。最近天气热了,硬盘丢失的问题更是频频出现,鲁大师检测CPU温度50左右,主板温度在60左右就会出现这个现象。当我连接一个风扇到上面的时候,5000RPM让人难以忍受。下一个实验就是如何做一个能够调节转速的风扇了。

参考:
参考1:VS2008串口发送参考代码
http://www.lab-z.com/%E4%B8%87%E4%B8%87%E6%B2%A1%E6%83%B3%E5%88%B0%EF%BC%8C%E4%B8%B2%E5%8F%A3%E7%9A%84%E6%95%85%E4%BA%8B/
参考2:超简单的opencv骰子点数识别,效果居然还不错 http://chen-xiao.com/?p=250
参考3:OpenCV的imshow无法正常显示视频http://haibuo1981.iteye.com/blog/1401764
非常感谢作者解决了困惑我一下午的问题,如果你用 cap >> img 这样之后,想再通过显示imShow到屏幕上(特别是在一个循环中),必须使用waitKey(xx)给程序足够的时间来更新画面。
参考4:使用 Arduino 打造一个硬件的Watchdog
http://www.lab-z.com/%E4%BD%BF%E7%94%A8-arduino-%E6%89%93%E9%80%A0%E4%B8%80%E4%B8%AA%E7%A1%AC%E4%BB%B6%E7%9A%84watchdog/

Arduino制作一个电子骰子(2)

俗话说“小赌怡情,大赌兴业”,经过考证我们能够确信这个叫做”俗话“的名人是赌场老板—-看过余华先生的《活着》对此肯定深有体会,再多说一句,我个人不觉得这部小说讲述的是一个悲剧故事,生活就是这样而已,过去是这样现在依然是这样。小说没有被禁,但是对应的电影在国内是禁片。有这样一种说法”昨晚在暨大演讲,有同学问我《活着》改编电影时什么事情印象深刻?我说这是18年前的事了,还记得当时张艺谋时常说原作里的什么细节要改动,审查才能通过。看他胸有成竹的模样,心想他如此了解xxx,对他十分钦佩。可是张艺谋拍摄完成电影后,审查还是没有通过。我不再钦佩张艺谋,我钦佩xxx了“【参考1】。

从概率的角度来说,哪怕对方比你获胜的概率只高了2%,在你”根本停不下“的情况下,蚀本只是时间问题…….

前面介绍了一个 Arduino 打造的”电子骰子“,我们下面要对它进行改装,让你”稳赢不输“。

我们再添加一个遥控设备,它在我们之前的《用 Arduino 打造PPT遥控器》中出场过,是目很容易买到价格低廉的遥控设备。

remote

和之前的相比,只是多了一个接收器。接收器上有5个Pin,这个接收器和 Arduino 的连接如下:

5V    <——> 5v (我试过如果用 3.3v的话不工作)
GND<——>  GND
D0   <——> D4
D1   <——> D5
D2   <——> D6
D3   <——> D7

按键比较少,所以只支持对应了从3到6个点数。改造之前的程序,加入检查遥控按键是否被按下的代码。

/*
日期: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 PinA=4;
int PinB=5;
int PinC=6;
int PinD=7;
int Current=1;
int Actual=0xFF; 
boolean MarkStart=false;  //标记是否按键抬起

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);    
  pinMode(PinA, INPUT);   
  pinMode(PinB, INPUT);   
  pinMode(PinC, INPUT);   
  pinMode(PinD, 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;

  if (digitalRead(PinA)==HIGH) {Actual=3;}
  if (digitalRead(PinB)==HIGH) {Actual=4;}
  if (digitalRead(PinC)==HIGH) {Actual=5;}
  if (digitalRead(PinD)==HIGH) {Actual=6;}

  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; 
	if (Actual==0xFF) {showNum(random(1,7));}  //如果当前未收到选择,随机生成一个
	else
	  {showNum(Actual);}         //收到过选择,那么就显示
	Actual=0xFF;  
	}

}

和上一个电子骰子相比,这个接线看起来复杂多了,这是视觉差异而已,看起来多了很多线因为5V和GND用的比较多而已,所以导入面包板,都从上面取电而已。

20140703980

 

程序下载 dice2

最后这篇文章告诉大家了一个非常重要的事实就是:不要和电子的赌博机游戏,因为你根本不可能赢。如果真的有人对你这样炫耀,存在的三种可能性是:

1.他在作弊
2.机器坏了
3.他想拉你一起入伙(这个可能性最大)

参考:
1:很早之前听说过这个事情,但是一直没有找到出处。从目前能够搜索到的资料看,来源应该是余华的微博
http://tieba.baidu.com/p/1621656047

2.http://www.lab-z.com/arduinodice1/  本系列的第一篇文章

3.http://www.lab-z.com/%E7%94%A8-arduino-%E6%89%93%E9%80%A0ppt%E9%81%A5%E6%8E%A7%E5%99%A8/   用-arduino-打造ppt遥控器

制作一个电子骰子(1)

有一种说法,说赌场的英文单词 CASINO 是中国人发明的:

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

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

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

dice

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

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

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

max7219

电路方面,连接如下:

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

MAX7219五个Pin连接如下

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

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

/*
日期: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));
}
}

 

 

 

 

 

程序设计上有个很有趣的地方,

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

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

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

20140702977

程序下载 dice1

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

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

制作一个PS2键盘记录器

这是一个能够记录PS2键盘发出的按键信息的演示装置。

使用的元件:

Arduino UNO                          一块
PS2 延长线(一端公头,一端母头)     一根
测试钩                               三根

首先要将PS2延长线剥开,其中有四根线,分别是 Vcc/GND/Clock/Data。我们只需要钩取其中除了Vcc之外的三根线。

psa

特别说一下这几根线的分布,在公头端和母头端看过去的Pin编号是对称的,如下图所示(网上有人说一些资料搞错了,我看了一下,是他本人搞错了公母而已)。选好线之后务必使用万用表确定接线无误。

psb

连接方面,上面图片中白色测试钩钩取的是黄色线 GND;绿色测试钩钩取的是红色Data;白色测试钩钩取的是白色线 Clock。

PS2 头和线色对应关系如下图:

psc

程序调用了PS2Keyboard library 【参考1】,用它来做PS2 协议的解码。调用的方式就是keyboard.begin(DataPin, IRQpin);  DataPin 要给出Arduino上连接到 PS2 Data的Pin脚;IRQpin 要给出Arduino上连接到 PS2 Clock的Pin脚。本例中,分别是 Pin8 和 Pin2。

psd

开始运行后, Setup()中会在串口输出一个菜单,提示如果输入 P ,那么会直接打印出 Arduino板载 EEPROM中的内容(UNO 的EEPROM不大,只有1K【参考2】。前面2个Byte用来存放一个记录当前要写入的位置。其余位置用来存储记录的键值。如果超过1K,那么返回开始处重新覆盖)。如果5秒之内没有输入 P ,那么程序会运行loop() 中的代码。先使用PS2Keyboard library 进行解码,然后每次都要从EEPROM中取出前2个Bytes组成一个指针,指示要将这个键值要存放在EEPROM中的位置。

  
#include "PS2Keyboard.h"
#include <EEPROM.h>

const int DataPin = 8;
const int IRQpin =  2;
const int MaxLength = 1024;
PS2Keyboard keyboard;
int SaveTo;

void setup() {
  boolean TimeOutMark=true;
  keyboard.begin(DataPin, IRQpin);  
  Serial.begin(9600);
  
  Serial.write("Keyboard Logger for Arduino\n");
  Serial.write("www.lab-z.com 2014.5\n");
  Serial.write("   p - for output record \n");
  
  unsigned long _time = millis();  //用于超时
  while ((millis() - _time) < 5000)
  {
  char inChar = Serial.read();
  if ((inChar == 'p') || (inChar == 'P'))
    {
       TimeOutMark=false;
       for (int i=2; i<MaxLength; i++)
         {
              char c = EEPROM.read(i) ;
              // check for some of the special keys
              if (c == PS2_ENTER) {
                    Serial.println();
              } else if (c == PS2_TAB) {
                    Serial.print("[Tab]");
              } else if (c == PS2_ESC) {
                    Serial.print("[ESC]");
              } else if (c == PS2_PAGEDOWN) {
                    Serial.print("[PgDn]");
              } else if (c == PS2_PAGEUP) {
                    Serial.print("[PgUp]");
              } else if (c == PS2_LEFTARROW) {
                    Serial.print("[Left]");
              } else if (c == PS2_RIGHTARROW) {
                    Serial.print("[Right]");
              } else if (c == PS2_UPARROW) {
                    Serial.print("[Up]");
              } else if (c == PS2_DOWNARROW) {
                    Serial.print("[Down]");
              } else if (c == PS2_DELETE) {
                    Serial.print("[Del]");
              } else {
                  // otherwise, just print all normal characters
                    Serial.print(c);
              } // End of else            
         }//End of for
     } //End of if ((inChar = 'p') || (inChar = 'P'))  
  }// End of While 
 if (TimeOutMark==true)  {Serial.write("Timeout occured!\n");  }
 else {Serial.write('\n Output End!');}
 
    EEPROM.write(0,2);
    EEPROM.write(1,0);     

} // End of Setup

void loop() {
  char c;

 if (keyboard.available()) {
     // read the next key
    c = keyboard.read();
   Serial.print(c);
    SaveTo = EEPROM.read(0) + (EEPROM.read(1) <<8);
    EEPROM.write(SaveTo,c);
    SaveTo++;
    if (SaveTo>MaxLength) {SaveTo=2;}
    EEPROM.write(0,SaveTo);
    EEPROM.write(1,(SaveTo >> 8));    
  }

  
}

 

实验中使用的是一个USB键盘,通过USB转PS2转为电脑上能使用的PS2 (资料上说现在的USB键盘内置2种协议,根据当前插入的接口自动选择输出的协议)。该键盘通过上面提到的PS2延长线接入电脑。记录结果如下:

pse

因为尺寸的原因,可以看出来这个东西只有演示的价值,如果真想做一个实用性的东西,还要选择PIC系列的小尺寸单片机加上SPI ROM………

下载
KeyLog

参考:

1.http://playground.arduino.cc/Main/PS2Keyboard

2.Arduino上的单片机自带EEPROM  328P 有 1024字节

http://wiki.geek-workshop.com/doku.php?do=export_xhtml&id=arduino:libraries:eeprom

关于 Arduino Serial Print 和 Write 的一点认识

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.h

class HardwareSerial : public Stream
{
private:
ring_buffer *_rx_buffer;
ring_buffer *_tx_buffer;
volatile uint8_t *_ubrrh;
volatile uint8_t *_ubrrl;
volatile uint8_t *_ucsra;
volatile uint8_t *_ucsrb;
volatile uint8_t *_ucsrc;
volatile uint8_t *_udr;
uint8_t _rxen;
uint8_t _txen;
uint8_t _rxcie;
uint8_t _udrie;
uint8_t _u2x;
bool transmitting;
public:
HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr,
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
void begin(unsigned long);
void begin(unsigned long, uint8_t);
void end();
virtual int available(void);
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
virtual size_t write(uint8_t);
inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
inline size_t write(unsigned int n) { return write((uint8_t)n); }
inline size_t write(int n) { return write((uint8_t)n); }
using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool();
};

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.cpp

size_t HardwareSerial::write(uint8_t c)
{
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;

// If the output buffer is full, there’s nothing for it other than to
// wait for the interrupt handler to empty it a bit
// ???: return 0 here instead?
while (i == _tx_buffer->tail)
;

_tx_buffer->buffer[_tx_buffer->head] = c;
_tx_buffer->head = i;

sbi(*_ucsrb, _udrie);
// clear the TXC bit — “can be cleared by writing a one to its bit location”
transmitting = true;
sbi(*_ucsra, TXC0);

return 1;
}

从上面可以看出来 Serial.Write 是直接把要发的东西送出去,Serial.Print 就复杂多了,会占用大量的内存。因此,如果有可能尽量用 Serial.Write。 如果你的程序用的大量的 Serial.Print,并且出现奇怪的问题,很可能产生的原因是内存不足,不妨删除几个 Print试试。