1

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?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Fatemah
  • 11
  • 2
  • Hi. Instead of using myservo.write(90), i think is better to use for loop to write the position and add at least 500ms on each looping. ` for (int pos = 0; pos <= 90; pos += 1) { myservo.write(pos); delay(500);}` – WenJuan Oct 17 '18 at 02:54

2 Answers2

0

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");
}
  • If i hear electric noise this mean the problem from the code ? – Fatemah Oct 18 '18 at 11:53
  • no if you hear electronic noise coming from the servos themselves it means they are getting power, oh and if you're not powering them from the arduino itself you should always have the servo motor ground connected to arduino ground for the signal to pass from arduino to servo. (you shouldn't try to power a lot of small servos or even one big servo from an arduino the asm1117-5.0 cannot provide enough power in that case) – Gibran Zawahra Nov 02 '18 at 09:35
  • did you try the Serial.println method ? was the result what you were expecting ? – Gibran Zawahra Nov 02 '18 at 09:36
-1

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

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Wessel
  • 1
  • 1