我第一次使用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。向上运行一次,但向下继续重置并运行伺服器。
答案 0 :(得分: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
}
}
}