男性成人玩具推荐:Arduino(附3P实拍图和学习笔记)

送礼物给孩子很容易,多少玩具都不嫌多,即使重复没关系。而给成年人就要看对象的喜好了。给女人的选择比较多,黄金指导原则是四个字“华而不实”;给男人可就麻烦多了,需要考虑的核心问题是“力量、速度、光明和需要动手操作,再加一个秩序感”。这样看来,给男人最好的礼物,是一辆装了涡轮增压和氙气大灯的手扶拖拉机。问题是这个东西太占地方,而且不便宜。能替代它的东西,其实可以很轻便很廉价,比如Arduino。这些天玩Arduino,我觉得这货的确是个好的成人玩具,在此推荐给大家一起玩。

Arduino是什么?看看官方介绍吧:

Arduino 是一款便捷灵活、方便上手的开源电子原型平台,包含硬件(各种型号的Arduino板)和软件(Arduino IDE),适用于艺术家、设计师、爱好者和对于“互动”有兴趣的朋友们。Arduino能通过各种各样的传感器来感知环境,通过控制灯光、马达和其他的装置来反馈、影响环境。

读起来如果感觉不知所云,请看插图三件物品(3 pieces)的实拍,中间那个就是我现在玩的型号为 MEGA2560 的Arduino板子。左边的是配套用的数据采集器,右边信用卡大小的蓝色卡片是来对比以展示尺寸的。Arduino的确比拖拉机小多了,40欧元左右。

enter image description here

图一:我的Arduino(中)

我拿它做什么用呢?现在是用来控制我的科研仪器设备。我弄了99个小灯泡,在楼下的地面上摆成了阵。夜幕下打电话给女神,说:“快透过窗子看楼下。”在她探头的时刻,我按个按钮,Arduino开始按照我设定的程序,控制那些灯闪烁,灯拼出的字符先是“祝你生日快乐”,然后换成“吃了吗?”,然后换成“不如下来一起去食堂打饭吧”……当然,我的科研工作比这有趣得多,这只是打个通俗的比方,你懂的。从这个例子可以看出Arduino的用法:先把Arduino连上电脑,写入指令(怎么写?学习笔记附后),再断开电脑;把Arduino连上相应的硬件,它就开始独立工作,控制硬件做一些有趣的事情。工作时只需要个5V的电源。简单来说,Auduino就像个机器人的脑子。我觉得,这玩意儿的拉风程度,不次于汪峰那个求婚用的无人机。

发挥想象力,Arduino能玩出各种花样来。比如,可以装个喇叭,写一段音乐进去,让它演奏;可以装个距离传感器,安到汽车尾部,用来探测到后面障碍物的距离;安装上足够的外围硬件,甚至搭建一个机器人都没问题。只有想不到,没有做不到。事实上,我的科研工作不是控制99个灯泡,而是控制29个电磁阀的开闭。可惜我知道得太晚,已经过了约女神去食堂打饭的岁数了。过几年我一定教儿子玩这个。

Arduino不是新东西,已经有中文论坛,目测上面玩这个的都是年轻一代,让人好生羡慕。Arduino的编程简单得令人发指,而且控制硬件很容易带来成就感,非常适合初学者理解编程语言的逻辑结构。

Arduino官方网站提供了很好的编程教程,跟着走一遍就能学会核心内容,编些小程序玩。只是教程都是实例,回顾查找起来不够便捷。于是,我把这些实例重新做了总结,是为学习笔记,如下。

Structure

script

//variable declaration

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly: 
}

 /*
comment
 */

if

if (a == b) {
} 

while (millis() < 5000) {
}  //可以用在setup{},完成一些初始操作。

   switch (var) {
    case 1:
      //do something when var equals 1
      break;//break is used to exit from a do, for, or while loop, bypassing the normal loop condition. It is also used to exit from a switch statement.
    case 2:
      //do something when var equals 2
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
  }

for

for (n = 0; n < 3; n++) {
} 

Data types

const int i = 1;
float j = 2.2;
long k = 0;
int ledState = LOW;
int redPin = A0;
int noteDurations[] = {
 4, 8, 8, 4,4,4,4,4 };
byte n;
char inChar; //single character
String inputString = "";  
int pixels[8][8];  
stringOne.toInt() //convert the string stringOne into integer
String() // convert a variable into a string object.
stringOne =  String('a');          // converting a constant char into a String
String stringTwo =  String("This is a string");  //  converting a constant string into a String object:
stringOne =  String(13);
stringOne =  String(45, HEX);
stringOne =  String(255, BIN); 
stringOne =  String(millis(), DEC);

Initialize hardware

pinMode(13, OUTPUT); // initial pin 13 as output
pinMode(2, INPUT); // initial pin 2 as input
pinMode(2, INPUT_PULLUP); ////configure pin2 as an input and enable the internal pull-up resistor
Serial.begin(9600); // initial serial port to PC

Play with hardware

Digital pins

digitalWrite(13, HIGH); // turn on pin 13 (LED)
digitalWrite(13, LOW); // turn off pin 13 (LED)
digitalRead(13); // read on pin 13. value HIGH/LOW (1/0).

Analog pins

x = analogRead(A0); // read on pin A0. ranged 0 - 1023.
analogWrite(9, 255); // change the PWM (Pulse Width Modulation, ranged 0 - 255) value of pin 9 as 255 

Serial pins

int x = Serial.read();
Serial.println(2, DEC); // print "2" via serial port in decimal format. 
Serial.println(stringOne);  // prints  "Sensor "

Serial.print("number of button pushes:  ");
while (Serial.available() > 0) { 
//check to see if there is any data in the serial buffer. It will run as long as there is information waiting to be read.
}
Serial.parseInt() //locate values separated by a non-alphanumeric character
////////////SerialCallResponse////////////////////////////
//send a byte to establish contact until receiver responds 
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}
establishContact();
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("0,0,0");   // send an initial string
    delay(300);
  }
}
///////////////////////////////////////////////////////
//////////////////SerialCallResponseASCII//////////////
while (!Serial) {
; // wait for serial port to connect
}
///////////////////////////////////////////////////////
////////////////////SerialEvent/////////////////////
/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

Tone

tone(); // connected to a piezo buzzer or other speaker to play tones.

Time control

delay(1000); // wait for 1000 miliseconds
millis(); // returns the time since the current program is started.

Operators

% modulo 整除的余数。
x = map(value, fromLow, fromHigh, toLow, toHigh) //线性转换。把某个范围内的一个数值转换成另外一个范围内的数值,integer。
logical: ==, !=, &&(and), ||(or), !(not)
x = constrain(x, a, b) // return x if x is a - b, a if x < a, b if x > b

Strings

'\n' //enter
inputString.reserve(200);    // reserve 200 bytes for the inputString

concatenation

nputString += inChar;


  stringThree =  stringOne + 123; // adding a constant integer to a string:
  stringThree = stringOne + 123456789;
  stringThree =  stringOne + 'A';
  stringThree =  stringOne +  "abc";
  stringThree = stringOne + stringTwo;
  stringOne += stringTwo;
  stringTwo.concat(millis()); // using concat() to add a long variable to a string

changing letters to lower or upper case

  stringOne.toUpperCase();
  stringTwo.toLowerCase();

search and replace

  colonPosition = reportString.indexOf(':');  // return the position of ':' in the string reportString. can also search for a string
  colonPosition = reportString.indexOf(':', 5); //search ':' from the 5th character
  colonPosition = reportString.lastIndexOf(':'); //search the last ':'
  reportString.charAt(15); //return the 15th character in reprotString
  reportString.substring(5); //return a string from the 5th character to the last character of reportString
  reportString.substring(5, 8); //return a string from the 5th character to the 8th      
  reportString.setCharAt(colonPosition, '='); //replace the colonPosition-th character in reportString with '='
  reportString.replace(':', '=');
  reportString.startsWith("HTTP/1.1"); //returns true or false
  reportString.startsWith("HTTP/1.1", 5); //starts at the 5th character. stringOne.substring(5) == "HTTP/1.1"
  reportString.endsWith()

length and trim

  reportString.length();//length of a String
  reportString.trim(); // returns a version of the String with any leading and trailing whitespace removed. Whitespace refers to characters that take space but aren't seen. It includes the single space (ASCII 32), tab (ASCII 9), vertical tab (ASCII 11), form feed (ASCII 12), carriage return (ASCII 13), or newline (ASCII 10). 

compare strings. >, <.

  stringOne == stringTwo
  stringOne.equalsIgnoreCase(stringTwo)
  stringOne.compareTo(stringTwo) // returns a negative value if stringOne comes first in alphanumeric order, and a positive value if stringTwo does.

In order to avoid unexpected results, initialize the strings before concatenation (http://arduino.cc/en/Tutorial/StringAdditionOperator) as follows.

 stringThree = String ();

useful codes

 // This loop loops forever and does nothing. See http://arduino.cc/en/Tutorial/ASCIITable
    while(true) { 
      continue; 
    } 
 while(true);

confusing stuff

write() vs print():

write() writes binary data. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Serial.write(33);
Serial.print(33);

println() prints data as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’).

Serial.print("number of button pushes:  ");
Serial.println("number of button pushes:  ");

important examples:

Impressive

An analogy would be warming up a pizza in your microwave, and also waiting some important email. You put the pizza in the microwave and set it for 10 minutes. The analogy to using delay() would be to sit in front of the microwave watching the timer count down from 10 minutes until the timer reaches zero. If the important email arrives during this time you will miss it. (Blink Without Delay)

原文链接

comments powered by Disqus