arduino - 如何让 2 个引脚做同样的事情?
问题描述
我想让 2 个触摸传感器做同样的事情,那就是移动伺服,但我不知道谁能帮忙?第二个触摸传感器应该命名为 TOUCH_SENSOR_PIN2 我试过但只有 1 可以工作。有谁知道如何使此代码与 2 个触摸传感器一起使用?
这是代码:
#include <Servo.h>
const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 141; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); //read new state
if(lastTouchState == LOW && currentTouchState == HIGH) {
// change angle of servo motor
if(angle == 141)
angle = 156;
else
if(angle == 156)
angle = 141;
// control servo motor arccoding to the angle
servo.write(angle);
}
}
解决方案
我还没有尝试过,但这应该可以。请替换<pin number>
为您的第二个触摸传感器所连接的引脚编号。
#include <Servo.h>
const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin
const int TOUCH_SENSOR_PIN2 = <pin number>;
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 141; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
int lastTouchState2; // the previous state of touch sensor 2
int currentTouchState2; // the current state of touch sensor 2
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT);
pinMode(TOUCH_SENSOR_PIN2, INPUT);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); //read new state
lastTouchState2 = currentTouchState2; // save the last state
currentTouchState2 = digitalRead(TOUCH_SENSOR_PIN2); //read new state
if((lastTouchState == LOW && currentTouchState == HIGH) || (lastTouchState2 == LOW && currentTouchState2 == HIGH)) {
// change angle of servo motor
if(angle == 141)
angle = 156;
else
if(angle == 156)
angle = 141;
// control servo motor arccoding to the angle
servo.write(angle);
}
}
推荐阅读
- python - 如何加快从大文件中创建熊猫数据框的速度?
- json - 无法使用 PHP 和 JSON 从网站向 Arduino 请求数据
- r - RStudio Connect 中的包管理
- wordpress - 两个域名,单主机,htaccess
- python - 字典和嵌套字典之间的Python键匹配并将新值写入嵌套字典
- html - 同一个网站的未发布版本完美时,为什么我发布的网站会变形?
- corda - 每个节点的 Corda 帐户限制
- monit - 使用 monit 监控 bash 脚本执行
- python - Python 3.8 上的不和谐机器人
- websocket - 使用 AWS Lambda + API Gateway 的 Websocket 连接