“触碰传感器”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{| style="width: 800px;" |- | ==目的== 本教程将教大家如何使用Microduino的触摸开关。 ==设备== *'''Microduino-CoreUSB/zh''' *'''[[Microduino-TOUCH/zh]...")
 
实验五:控制彩灯
 
(未显示3个用户的22个中间版本)
第2行: 第2行:
 
|-
 
|-
 
|
 
|
==目的==
+
==概述==
  
本教程将教大家如何使用Microduino的触摸开关。
+
用来检测是否发生碰撞,因此也可称为碰撞信号传感器。由于碰撞位置不同,分成左碰撞传感器和右碰撞传感器。
 +
<br />
 +
碰撞开关是将机械量转换为电量。当碰撞传感器与前方物体发生碰撞后导致电路开关闭合,电路回路导通,LED被点亮,碰撞传感器两端的电压由高电平转变成低电平。
 +
<br />
 +
碰撞传感器通过检测碰撞传感器两端的电平的高低来判断是否有碰撞。可用于开关、限位、防碰撞等,如3D打印机电机限位开关,机器人防碰撞等。
  
==设备==
+
==规格==
*'''[[Microduino-CoreUSB/zh]]'''
 
*'''[[Microduino-TOUCH/zh]]'''
 
*'''[[Microduino-BUZZER/zh]]'''
 
  
 +
*电器规格
 +
**工作电压:5V
 +
**输入设备
 +
*技术参数
 +
**引脚说明:GND、VCC、信号输入、NC(空)。
 +
**数字输入
  
 +
*尺寸
 +
**开关大小:12mm*6mm,
 +
**板子大小:20mm*20mm
 +
**1.27mm间距的4Pin接口与sensorhub相连
 +
*接法
 +
**有左右之分
 +
==开发==
 +
===设备===
 +
{|class="wikitable"
 +
|-
 +
|模块||数量||功能
 +
|-
 +
|[[mCookie-CoreUSB/zh]]||1||核心板
 +
|-
 +
|[[mCookie-Hub/zh]]||1||传感器转接板
 +
|-
 +
|[[Microduino-Crash/zh]]||1||碰撞开关
 +
|-
 +
|[[Microduino-Color led/zh]]||1||彩灯
 +
|}
  
 
*其他硬件设备
 
*其他硬件设备
**USB数据连接线   一根
+
**USB数据连接线*1
 +
**传感器接线*2
  
 +
[[File:Crash-module.jpg|center|600px]]
  
==程序==
+
===准备===
 +
*Setup 1:将Crash背面接口和Hub的数字口(D6)接起来,这个就是控制碰撞开关的引脚,用户可自己更改。
 +
[[file:mCookie-Crash-sensor.JPG|600px|center]]
 +
*Setup 2:将CoreUSB,Hub,Crash连接在一起。通过USB数据线将接入电脑。
 +
[[file:mCookie-Crash-pc.JPG|600px|center]]
  
 +
==调试==
 +
===实验一:检测按键===
 +
*打开Arduino IDE,将下列代码复制到IDE中。
 +
<source lang="cpp">
 +
#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
 +
}
 +
</source>
 +
*选择正确的板卡和COM端口
 +
[[file:upload.JPG|600px|center]]
 +
*编译通过后直接下载。
 +
[[file:upload-crash.JPG|600px|center]]
 +
*打开串口监视器。可以看到不按时“buttonState”的值为1,按下的值为0;
 +
[[file:Crash-one-res.JPG|600px|center]]
 +
'''*采用“digitalRead(XX)”函数来读取按键信号,该信号为数字信号,只有“0”和“1”两种状态。'''
 +
 +
===实验二:按下、松开===
 +
*打开Arduino IDE,将下列代码复制到IDE中。
 +
<source lang="cpp">
 +
#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
 +
  }
 +
}
 +
</source>
 +
*按下的时候是从“1”变到“0”,松开的时候是从“0”变到“1”,采用数据有变化的时候进行判断,然后检测当前值就可判断是按下还是松开。
 +
*“!=”表示不等于,当按下值有变化时候才执行。
 +
*结果:打开串口监视器,按下按键时串口打印“Control”,松开时串口打印“Loosen”。
 +
[[file:Crash-two-res.JPG|600px|center]]
 +
 
 +
===实验三:按下状态翻转===
 +
*打开Arduino IDE,将下列代码复制到IDE中。
 +
<source lang="cpp">
 +
#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);
 +
  }
 +
}
 +
</source>
 +
*“= !”表示等于非,一个布尔量它的值只有“真”和“假”,这样每运行一次“str”值翻转一次。
 +
*按一次“str”值为真(1),再按一次“str”值为假(0),反复循环。
 +
[[file:Crash-three-res.JPG|600px|center]]
 +
 
 +
===实验四:短按、长按功能===
 +
*打开Arduino IDE,将下列代码复制到IDE中。
 +
<source lang="cpp">
 +
#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)
把触摸开关插到Microduino-SensorHub的D6引脚,D5引脚插一个Microduino-BUZZER蜂鸣器
+
  {
[[File:MicroduinoTOUCH.png|600px|center|thumb]]
+
    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;
 +
  }
 +
}
 +
</source>
 +
*当按键按下时,检测按键是否松开,如果在一定时间内没松开则认为长按,否则短按。
 +
*短按是串口打印“short”;长按时串口打印“long”。
 +
[[file:Crash-long-short-res.JPG|600px|center]]
  
步骤二:
+
===实验五:控制彩灯===
用USB线连接电脑,下载代码并烧录到Microduino-CoreUSB中。
+
*打开Arduino IDE,将下列代码复制到IDE中。
[[File:MicroduinoTOUCH1.png|600px|center|thumb]]
+
<source lang="cpp">
 +
#define PIN A0
 +
#define PIN_key 6
 +
#define NUMPIXELS      2        //级联彩灯数量
  
 +
boolean status=false;
  
步骤三:
+
#include <Microduino_ColorLED.h> //引用彩灯库
用手触摸开关,看看有什么反应
+
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
[[File:MicroduinoTOUCH2.png|600px|center|thumb]]
 
  
 +
void setup()
 +
{
 +
  pinMode(PIN_key, INPUT);
 +
  pinMode(PIN, OUTPUT);
  
步骤四:
+
  strip.begin();  //初始化LED
再次触摸开关。
+
  strip.setBrightness(60);      //设置彩灯亮度
[[File:MicroduinoTOUCH3.png|600px|center|thumb]]
+
  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);
 +
  }
 +
}
 +
</source>
 +
*将碰触开关接到D6,彩灯接到A0引脚。
 +
[[File:Crash-sensor.jpg|600px|center]]
 +
*按一次开灯,再按一次关灯,反复循环。
  
 
==视频==
 
==视频==
  
 
|}
 
|}

2017年8月3日 (四) 06:36的最新版本

概述

用来检测是否发生碰撞,因此也可称为碰撞信号传感器。由于碰撞位置不同,分成左碰撞传感器和右碰撞传感器。
碰撞开关是将机械量转换为电量。当碰撞传感器与前方物体发生碰撞后导致电路开关闭合,电路回路导通,LED被点亮,碰撞传感器两端的电压由高电平转变成低电平。
碰撞传感器通过检测碰撞传感器两端的电平的高低来判断是否有碰撞。可用于开关、限位、防碰撞等,如3D打印机电机限位开关,机器人防碰撞等。

规格

  • 电器规格
    • 工作电压:5V
    • 输入设备
  • 技术参数
    • 引脚说明:GND、VCC、信号输入、NC(空)。
    • 数字输入
  • 尺寸
    • 开关大小:12mm*6mm,
    • 板子大小:20mm*20mm
    • 1.27mm间距的4Pin接口与sensorhub相连
  • 接法
    • 有左右之分

开发

设备

模块 数量 功能
mCookie-CoreUSB/zh 1 核心板
mCookie-Hub/zh 1 传感器转接板
Microduino-Crash/zh 1 碰撞开关
Microduino-Color led/zh 1 彩灯
  • 其他硬件设备
    • USB数据连接线*1
    • 传感器接线*2

准备

  • Setup 1:将Crash背面接口和Hub的数字口(D6)接起来,这个就是控制碰撞开关的引脚,用户可自己更改。
  • Setup 2:将CoreUSB,Hub,Crash连接在一起。通过USB数据线将接入电脑。

调试

实验一:检测按键

  • 打开Arduino IDE,将下列代码复制到IDE中。
#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
}
  • 选择正确的板卡和COM端口
Upload.JPG
  • 编译通过后直接下载。
  • 打开串口监视器。可以看到不按时“buttonState”的值为1,按下的值为0;

*采用“digitalRead(XX)”函数来读取按键信号,该信号为数字信号,只有“0”和“1”两种状态。

实验二:按下、松开

  • 打开Arduino IDE,将下列代码复制到IDE中。
#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
  }
}
  • 按下的时候是从“1”变到“0”,松开的时候是从“0”变到“1”,采用数据有变化的时候进行判断,然后检测当前值就可判断是按下还是松开。
  • “!=”表示不等于,当按下值有变化时候才执行。
  • 结果:打开串口监视器,按下按键时串口打印“Control”,松开时串口打印“Loosen”。

实验三:按下状态翻转

  • 打开Arduino IDE,将下列代码复制到IDE中。
#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);
  }
}
  • “= !”表示等于非,一个布尔量它的值只有“真”和“假”,这样每运行一次“str”值翻转一次。
  • 按一次“str”值为真(1),再按一次“str”值为假(0),反复循环。

实验四:短按、长按功能

  • 打开Arduino IDE,将下列代码复制到IDE中。
#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;
  }
}
  • 当按键按下时,检测按键是否松开,如果在一定时间内没松开则认为长按,否则短按。
  • 短按是串口打印“short”;长按时串口打印“long”。

实验五:控制彩灯

  • 打开Arduino IDE,将下列代码复制到IDE中。
#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);
  }
}
  • 将碰触开关接到D6,彩灯接到A0引脚。
  • 按一次开灯,再按一次关灯,反复循环。

视频