1

我第一次使用 Arduino 来控制我的模型铁路上的平交道口,使用 LDR 和两个伺服系统。我想让火车通过光敏电阻 (LDR)。在这种情况下,大门应该关闭。当火车经过时,应该有一个延迟,然后大门应该打开。此处提供的代码已简化为只需打开和关闭大门,而无需延迟。当 LDR 发送恒定读数时,伺服系统应该只对初始变化做出响应,因为我不想管理伺服系统一旦移动就拉动或推动门。

我尝试使用全局来保持门状态。我现在更改了代码以传递一个变量。

#include <Servo.h>
Servo myservo;
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
int pos = 1;
int barState;

void setup() {
  Serial.begin(9600); //sets serial port for communication
  myservo.attach(9);
  barState = 0;
}

int upMove(int bs)

bs = 0;
for (pos = 0; pos <= 60; pos += 1) { // goes from 0 degrees to 60 degrees
  // in steps of 1 degree
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(30);                       // waits 30ms for the servo to reach the position

}
return bs;
}

int downMove(int bs)
{

  bs = 1;
  for (pos = 60; pos >= 0; pos -= 1) { // goes from 60 degrees to 0 degrees
    // in steps of 1 degree
    { myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(30);                    // waits 30ms for the servo to reach the position
    }
    return bs;
  }

  int up(int bs)

  {
    if ( bs = 1) {
      bs =  upMove(bs);
    }
    return bs;
  }

  int down(int bs)

  {
    bs = downMove(bs);
  }
  return bs;
}

void loop() {
  { sensorValue = analogRead(sensorPin);                    //define photocellReading as pin 0 input from LDR
    sensorValue = map(sensorValue, 0, 1023, 0, 179);      //map the LDR input to a value between 1-180 so the servo can understand it
    if (sensorValue > 90)         //if the LDR is showing less than half light intensity
    {
      up(barState);
    }
    else if (sensorValue <= 90)   //if the LDR is showing more than half light intensity
    {
      down(barState);   //then tell the servo to rotate forwards at a steady rate
    }
  }
}

该代码适用于 Up 但不适用于 Down。向上工作一次,但向下不断重置并操作伺服。

4

1 回答 1

0

我已经磨练了代码以使其更简单。它现在按预期运行,只是它继续运行伺服而不是每次 LDR 更改时只运行一次

#include <Servo.h>
Servo myservo;
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
int pos = 1;
int barState;

void setup() {
  Serial.begin(9600); //sets serial port for communication
  myservo.attach(9);
  barState = 0;
}

void upMove()
{
Serial.print( " In upMove ");Serial.println(barState);

for (pos = 0; pos <= 60; pos += 1) { // goes from 0 degrees to 60 degrees
  // in steps of 1 degree
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(30);                       // waits 30ms for the servo to reach the position
barState = 0;
}
}

void downMove()
{
Serial.print( " In downMove ");Serial.println(barState);

for (pos = 60; pos >= 0; pos -= 1) { // goes from 0 degrees to 60 degrees
  // in steps of 1 degree
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(30);                       // waits 30ms for the servo to reach the position
barState = 0;
}


}

void up()

  {
    Serial.print( " In UP 1 ");Serial.println(barState);
    {
     if (barState = 1) upMove();
    }
    Serial.print( " In UP 2 ");Serial.println(barState);
  }

void down()
{
    Serial.print( " In DOWN 1 ");Serial.println(barState);
 {
     if (barState = 1) downMove();
  }

    Serial.print( " In DOWN 2 ");Serial.println(barState);

}

void loop() {
  { sensorValue = analogRead(sensorPin);                    //define photocellReading as pin 0 input from LDR
    sensorValue = map(sensorValue, 0, 1023, 0, 179);      //map the LDR input to a value between 1-180 so the servo can understand it
    if (sensorValue > 90)         //if the LDR is showing less than half light intensity
    {
      up();
    }
    else if (sensorValue <= 90)   //if the LDR is showing more than half light intensity
    {
      down();   //then tell the servo to rotate forwards at a steady rate
    }
  }
}
于 2019-05-07T18:54:09.383 回答