Can't use 2 servo in tinkercad, why?

i recently tried to use a PIR sensor and ultrasonic sensor that if activated at the same time move the servos as the given specs. Can you guys find what's wrong with my code?

#include <Servo.h>

Servo servo1, servo2;
#define pirPin 2
#define pinServo1 3
#define pinServo2 9

int cm;
int pirStat;
int pos1, pos2;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  servo1.attach(pinServo1);
  servo2.attach(pinServo2);
}
    
void loop(){
  
 pirStat = digitalRead(pirPin); 
 cm = 0.01723 * readUltrasonicDistance(5, 6);
  if (pirStat == HIGH ){
    if (cm <= 10){
      pos1 = 90;
      pos2 = -90;
      servo1.write(pos1);
      servo2.write(pos2);
      delay(1);
    }
    if(cm > 10){
      pos1 = 0;
      pos2 = 0;
      servo1.write(pos1);
      servo2.write(pos2);
      delay(1);
    }
  }
  if (pirStat == LOW){
      pos1 = 0;
      pos2 = 0;
      servo1.write(pos1);
      servo2.write(pos2);
      delay(1);
  }
}
1 Like

Quoting You: "Can't use 2 servos". Why? What stops You? Are You too young or what?
Please present facts, information, that helpers can hook on to.

1 Like

Your code looks like didn't go wrong have you checked your wiring? Please tell us.

1 Like

Read about the range for the angle for the Servo.write() function: https://www.arduino.cc/reference/en/libraries/servo/write/.

1 Like

yes I actually had checked my wiring and it didn't had any mistake. Then I simulated it on tinkercad but my code only move 1 servo with the desired specs.

1 Like

maybe this simulation will help you guys figure out what's wrong

1 Like

The angle. It is about the angle. Read about the angle.

Your link to Tinkercad simulation did not work.
This link to Wokwi simulation does work : it-is-about-the-angle.ino - Wokwi Arduino and ESP32 Simulator

1 Like

so basicly (-90) degree doesn't work on the Servo.write function?

1 Like

I don't know if -90 works. I don't care if -90 or +1000000 works. That is not important.
The parameter for the angle can be 0 ... 180. That's all you have to know.

Do you see what I did here ? Use the library function as described in the documentation. Sometimes wrong values are ignored, sometimes wrong values are replaced by a limit and sometimes it can cause undefined behaviour. Let's not go there, follow the documenation.

When the project is powered on, and the Arduino sketch does a servo.attach(), then it defaults to its middle position. That is 90 degrees. So it defaults to 90.

1 Like

@andrewtim I haven't used this software before, but you can try to change servo1.write(90); to servo1.write(180); maybe it will work. I've done something similar before, I use actual motors and try servo1.write(180); and it works.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.