简体   繁体   中英

Servo motor issue

I have a problem in my project. If there is an available parking lot, I want the servo to rotate 90 degrees. I used two sensors, one for the car's entry and one if cars want to leave.

This is my code:

for(int i=0;i<11;i++)
{
    if (parks[i]!='0' && parks[i]!=' ')// if there is available park
    {
        aPark=1;
    }
    else
    {
        aPark=0;
    }
}
if(analogRead(A0)>200 && aPark==1) // if there is available park and car want to enter
{
    myservo.write(90);
    delay1=millis()+5000;
}

if(analogRead(A1)>200) // for leaving cars.
{
    myservo.write(90);
    delay1=millis()+5000;
}
if(delay1<millis())
{
    myservo.write(0);
}

When I connect everything and upload the code the servo is not rotating. Is there a problem in my code? Or is it because the sensor is not not detected?

i would verify the connections on the servo and even if the arduino is not sending any commands to the servo you should hear a buzz or some electric noise when its first powered on, next i would try adding serial.println statements to know if your code functions correctly and if the sensors are well connected, don't forget to Serial.begin(115200); in the setup code, for example ;

for(int i=0;i<11;i++) {
if (parks[i]!='0' && parks[i]!=' ')// if there is available park
{
    aPark=1;
    Serial.println("aPark=1");
}
else
{
    aPark=0;
    Serial.println("aPark=0");
} }  
 if(analogRead(A0)>200 && aPark==1) // if there is available park and car want to 
enter
{
myservo.write(90);
delay1=millis()+5000;
Serial.println("1.servo 90 delay mil+5000");
}

  if(analogRead(A1)>200) // for leaving cars.
{
myservo.write(90);
delay1=millis()+5000;
Serial.println("2.servo 90 delay mil+5000");
}
   if(delay1<millis())
 {
myservo.write(0);
Serial.println("servo 0");
}

Have you included servo library? #include <Servo.h> should be at the top of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM