Train stop, Servo Motor Problems

#include <Servo.h>

Servo myservo; 
int pos = 0;
// Constants for ultrasonic sensor
const int trigPin = 2;  // Trig pin of the ultrasonic sensor
const int echoPin = 3;  // Echo pin of the ultrasonic sensor

// Constants for LEDs
const int ledPin1 = 4;  // LED pin 1
const int ledPin2 = 5;  // LED pin 2

// Constants for stepper motor
const int motorPin = 6;  // Motor pin
Servo motor;  // Servo object for the stepper motor

// Constants for distance threshold
const int distanceThreshold = 25;  // Distance threshold in cm

// Variables
long duration;  // Time taken for sound to travel
int distance;   // Calculated distance from the ultrasonic sensor

// Function to make the LEDs flicker back and forth
void flickerLights() {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  delay(500);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(500);
   digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  delay(500);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(500);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  delay(500);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(500);
   digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  delay(500);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(500);
}

// Function to rotate the stepper motor to the right by 90 degrees
void rotateMotor() {
for (pos = 0; pos <= 90; pos += 1)
myservo.write(pos);
delay(15);
}

// Function to rotate the stepper motor back to the original position
void resetMotor() {
 for (pos = 90; pos >= 0; pos -= 1)
 myservo.write(pos);
 delay(15);
}

void setup() {
  // Initialize the pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  motor.attach(motorPin);
}

void loop() {
  // Measure the distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  // If an object is within the distance threshold
  if (distance <= distanceThreshold) {
    flickerLights();   // Flicker the lights
    rotateMotor();     // Rotate the motor to the right
  } else {
    resetMotor();      // Reset the motor to the original position
    digitalWrite(ledPin1, LOW);   // Turn off LED 1
    digitalWrite(ledPin2, LOW);   // Turn off LED 2
  }
}

I am trying to make a mini train stop using two leds, a ultrasonic sensor and a SG90 MicroServo motor. The sensor tells the lights to flicker back and forth and to turn off when it is not detecting something closer than 25 cm, this works as it should. My issue is with the motor. The motor is used as an arm that will rotate 90 degrees to the left to put the arm down (when something is detected) and will go back to its original position by going 90 degrees to the right, upwards (when something is not detected). instead the motor is simply not moving. it is plugged into the right pins and 100% has enough power. I have no error messages either.
How can I make it work?

Any help is appreciated as I am a beginner and really need it.

Maybe that?

And which pin would that be?

Hint, do you think your delays are in your for loops or not?

Please make and post schematics showing the motor driver and power supply.

The motor is not moving, because nothing writes to it...

Out of curiosity, are you using a servo motor or a stepper?

Out of curiosity can you make the motor work with a simpler sketch, ideally one that you did not write but is offered somewhere as an example you can then work from?

a7

How do I write it to?

yup

servo

That's a problem. See my previous question. The loop goes 0-90, then executes the 15 ms delay once. Probably not what you intended.
Try

void rotateMotor() {
  for (pos = 0; pos <= 90; pos++) {
  myservo.write(pos);
  delay(15);
  }
}

And modify the reset function similarly. There are other problems, as well, so I won't be surprised if this changes nothing; it still needs fixing.

You are writing to "myServo" which has not been attached. You have attached "motor" to "motorPin" (6).

1 Like

A post was split to a new topic: Problem with servo

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