“触碰传感器”的版本间的差异
853387039@qq.com(讨论 | 贡献) (→概述) |
853387039@qq.com(讨论 | 贡献) (→设备) |
||
第35行: | 第35行: | ||
|[[mCookie-Hub/zh]]||1||传感器转接板 | |[[mCookie-Hub/zh]]||1||传感器转接板 | ||
|- | |- | ||
− | |[[ | + | |[[Microduino-Crash/zh]]||1||碰撞开关 |
|} | |} | ||
第42行: | 第42行: | ||
*其他硬件设备 | *其他硬件设备 | ||
**USB数据连接线 一根 | **USB数据连接线 一根 | ||
+ | |||
===准备=== | ===准备=== | ||
*Setup 1:将Crash背面接口和Hub的数字口(D6)接起来,这个就是控制碰撞开关的引脚,用户可自己更改。 | *Setup 1:将Crash背面接口和Hub的数字口(D6)接起来,这个就是控制碰撞开关的引脚,用户可自己更改。 |
2015年10月16日 (五) 10:07的版本
目录概述用来检测是否发生碰撞,因此也可称为碰撞信号传感器。由于碰撞位置不同,分成左碰撞传感器和右碰撞传感器。
规格
开发设备
准备
调试实验一:检测按键
#define pushButton 6
int buttonState;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
buttonState = digitalRead(pushButton);//读取碰触开关输入的值
// print out the state of the button:
Serial.print("buttonState:");
Serial.println(buttonState); //串口打印碰触开关的值
delay(100); //延时100ms
}
*采用“digitalRead(XX)”函数来读取按键信号,该信号为数字信号,只有“0”和“1”两种状态。 实验二:按下、松开
#define pushButton 6 //定义按键控制引脚
int buttonState, num;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
buttonState = digitalRead(pushButton);//读取碰触开关输入的值
// print out the state of the button:
//按下或松开串口打印碰触开关的值
if (num != buttonState)
{
num = buttonState;
if (num == 1)
Serial.println("Loosen");//松开
else
Serial.println("Control");//按下
delay(100); //延时100ms
}
}
实验三:按下状态翻转
#define pushButton 6
int buttonState;
boolean str;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);//串口通讯波特率
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
buttonState = digitalRead(pushButton);//读取碰触开关输入的值
if (!buttonState)
{
delay(300);
str = !str;
Serial.print("str:");
Serial.println(str);
}
}
实验四:短按、长按功能
#define pushButton 6
int buttonState, num;
unsigned long button_time_cache = 0;
unsigned long button_time = 0;
boolean button_sta = false;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
void loop() {
// read the input pin:
buttonState = digitalRead(pushButton);//读取碰触开关输入的值
if (buttonState == 0)
{
delay(100);
if (digitalRead(pushButton) == 1 && !button_sta)//按键松开并且没进入长按,则认为是短按
{
Serial.println("short");
}
else if (millis() - button_time_cache > 1500) //时间大于1.5S则认为长按
{
button_sta = true;
button_time_cache = millis();
Serial.println("long");
}
}
else if (buttonState == 1)
{
button_time_cache = millis();
button_sta = false;
}
}
实验五:控制彩灯
#define PIN_LED A0
#define PIN_key 6
boolean status=false;
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN_LED, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode(PIN_key, INPUT);
pinMode(PIN_LED, OUTPUT);
strip.begin(); //初始化LED
strip.show(); // Initialize all pixels to 'off'
}
// the loop function runs over and over again forever
void loop()
{
if(!digitalRead(PIN_key))
{
delay(100);
if(!digitalRead(PIN_key))
{
status=!status;
}
}
if(status)
{
colorWipe(strip.Color(0, 0, 0), 10); //关灯
}
else
{
colorWipe(strip.Color(255, 0, 0), 10); //蓝色
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait)
{
for(uint16_t i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
视频 |