触碰传感器
目录概述用来检测是否发生碰撞,因此也可称为碰撞信号传感器。由于碰撞位置不同,分成左碰撞传感器和右碰撞传感器。
规格
开发设备
准备
调试实验一:检测按键
#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 A0
#define PIN_key 6
#define NUMPIXELS 2 //级联彩灯数量
boolean status=false;
#include <Microduino_ColorLED.h> //引用彩灯库
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
void setup()
{
pinMode(PIN_key, INPUT);
pinMode(PIN, OUTPUT);
strip.begin(); //初始化LED
strip.setBrightness(60); //设置彩灯亮度
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);
}
}
视频 |