Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 如何使两个引脚做同样的事情?_Arduino - Fatal编程技术网

Arduino 如何使两个引脚做同样的事情?

Arduino 如何使两个引脚做同样的事情?,arduino,Arduino,我想让两个触摸传感器做同样的事情,那就是移动伺服,但我不知道有谁能帮我? 第二个触摸传感器应命名为touch_sensor_PIN2 我试过了,但只有一个有效。是否有人知道如何使该代码与2个触摸传感器一起工作 代码如下: #include <Servo.h> const int TOUCH_SENSOR_PIN = 6; // Arduino pin connected to touch sensor's pin const int SERVO_PIN

我想让两个触摸传感器做同样的事情,那就是移动伺服,但我不知道有谁能帮我? 第二个触摸传感器应命名为touch_sensor_PIN2 我试过了,但只有一个有效。是否有人知道如何使该代码与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);
        
    
          }
      }
#包括
const int TOUCH_传感器_引脚=6;//Arduino引脚连接到触摸传感器引脚
const int SERVO_引脚=9;//Arduino引脚连接至伺服电机引脚
伺服;//创建伺服对象以控制伺服
//变量将发生变化:
内倾角=141;//伺服电机的电流角
int lastTouchState;//触摸传感器的先前状态
int currentTouchState;//触摸传感器的当前状态
无效设置(){
Serial.begin(9600);//初始化Serial
引脚模式(触摸传感器引脚,输入);
servo.attach(servo_引脚);//将引脚9上的伺服连接到伺服对象
伺服写入(角度);
currentTouchState=数字读取(触摸传感器针脚);
}
void循环(){
lastTouchState=currentTouchState;//保存最后一个状态
currentTouchState=digitalRead(触摸传感器针脚);//读取新状态
如果(lastTouchState==低和当前TouchState==高){
//改变伺服电机的角度
如果(角度==141)
角度=156;
其他的
如果(角度==156)
角度=141;
//将伺服电机控制到角度
伺服写入(角度);
}
}

我还没有试过,但这应该可以用。请将
替换为第二个触控传感器所连接的引脚编号

#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);
    

      }
  }
#包括
const int TOUCH_传感器_引脚=6;//Arduino引脚连接到触摸传感器引脚
const int TOUCH_传感器_PIN2=;
const int SERVO_引脚=9;//Arduino引脚连接至伺服电机引脚
伺服;//创建伺服对象以控制伺服
//变量将发生变化:
内倾角=141;//伺服电机的电流角
int lastTouchState;//触摸传感器的先前状态
int currentTouchState;//触摸传感器的当前状态
int lastTouchState2;//触摸传感器2的先前状态
int currentTouchState2;//触摸传感器2的当前状态
无效设置(){
Serial.begin(9600);//初始化Serial
引脚模式(触摸传感器引脚,输入);
pinMode(触摸式传感器PIN2,输入);
servo.attach(servo_引脚);//将引脚9上的伺服连接到伺服对象
伺服写入(角度);
currentTouchState=数字读取(触摸传感器针脚);
currentTouchState2=数字读取(触摸传感器针脚2);
}
void循环(){
lastTouchState=currentTouchState;//保存最后一个状态
currentTouchState=digitalRead(触摸传感器针脚);//读取新状态
lastTouchState2=currentTouchState2;//保存最后一个状态
currentTouchState2=数字读取(触摸传感器针脚2);//读取新状态
如果((lastTouchState==低和当前TouchState==高)| |(lastTouchState2==低和当前TouchState2==高)){
//改变伺服电机的角度
如果(角度==141)
角度=156;
其他的
如果(角度==156)
角度=141;
//将伺服电机控制到角度
伺服写入(角度);
}
}

成功了,非常感谢