“触碰传感器”的版本间的差异
853387039@qq.com(讨论 | 贡献) |
853387039@qq.com(讨论 | 贡献) (→实验五:控制彩灯) |
||
第244行: | 第244行: | ||
} | } | ||
</source> | </source> | ||
+ | *将碰触开关接到D6,彩灯接到A0引脚。 | ||
+ | [[File:Crash-sensor.jpg|600px]] | ||
*按一次开灯,再按一次关灯,反复循环。 | *按一次开灯,再按一次关灯,反复循环。 | ||
+ | |||
==视频== | ==视频== | ||
|} | |} |
2015年10月16日 (五) 10:05的版本
目录概述本教程将教大家如何使用Microduino的触摸开关。用来检测是否发生碰撞,因此也可称为碰撞信号传感器。由于碰撞位置不同,分成左碰撞传感器和右碰撞传感器。碰撞开关是将机械量转换为电量。当碰撞传感器与前方物体发生碰撞后导致电路开关闭合,电路回路导通,LED被点亮,碰撞传感器两端的电压由高电平转变成低电平。碰撞传感器通过检测碰撞传感器两端的电平的高低来判断是否有碰撞。可用于开关、限位、防碰撞等,如3D打印机电机限位开关,机器人防碰撞等。 规格
开发设备
准备
调试实验一:检测按键
#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);
}
}
视频 |