#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.