Servo keep spinning

Im making a segregation for my trash bin with 3 bin spinning at the bottom however the servo in the sensor that supposed to be stopping the trash keep on spinning this is my code.

#include <AccelStepper.h>
#include <MultiStepper.h>

#include<Servo.h>
#include <AccelStepper.h>
Servo flap;
#define trigPin 7
#define echoPin 6
int rain_pin = 3;
int inductive_pin = 4;
int flap_pin = 5;
int homing_pin=13;
int homing;
int rain;
int inductive;
int sound = 250;
int i;
#define FULLSTEP 4
AccelStepper myStepper(FULLSTEP,8,10,9,11);
void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(rain_pin,INPUT);
  pinMode(inductive_pin,INPUT);
  pinMode(homing_pin,INPUT);
  flap.attach(flap_pin);
  homing=digitalRead(homing_pin);
  myStepper.setMaxSpeed(1000.0);
  myStepper.setAcceleration(100.0);
  myStepper.setSpeed(1000);
   for(i=0;homing==LOW;i++){
  myStepper.moveTo(i);
  myStepper.runToPosition();
    homing=digitalRead(homing_pin);
    }
  myStepper.moveTo(i-2);
 myStepper.runToPosition();
  myStepper.setCurrentPosition(0);
 
}

void loop() {
  long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance);
inductive=digitalRead(inductive_pin);
rain=digitalRead(rain_pin);

if(inductive==HIGH)//metallic waste
{ 
  myStepper.moveTo(982);
  myStepper.runToPosition();
 delay(2000);
  flap.writeMicroseconds(179);
  delay(1500);
  flap.writeMicroseconds(0);
  delay(500);
  myStepper.moveTo(0);
  myStepper.runToPosition();
}
if(rain==LOW)//wet waste
{
  myStepper.moveTo(1485);
  myStepper.runToPosition();
 delay(2000);
  flap.writeMicroseconds(179);
  delay(1500);
  flap.writeMicroseconds(0);
  delay(500);
  myStepper.moveTo(0);
  myStepper.runToPosition();
}
if(distance <=8)//dry waste
{
     
//delay(500);
if(rain==HIGH);
{
if(inductive==LOW);

{     
  myStepper.moveTo(510);
  myStepper.runToPosition();
 delay(2000);
  flap.writeMicroseconds(179);
  delay(1500);
  flap.writeMicroseconds(0);
  delay(500);
  myStepper.moveTo(0);
  myStepper.runToPosition();
}}}
if(distance>=9&&inductive==LOW&&rain==HIGH) //No waste
{
 myStepper.moveTo(0);
 myStepper.runToPosition();
  flap.writeMicroseconds(0);
}
delay(4000);
}

When a servo spins, that means it has been modified to act as a "Continuous Rotation Servo", basically a gearmotor with speed control. It no longer has position control.

You need to find a servo that has not been modified. That one will still have position control.

1 Like

may servo is Tower pro SG92R micro servco and is supposed to only spin 0-180 degree.

Sounds like your servo has been modified or is broken.

1 Like

how should I fix that is my code fine??

Get a new servo.

Hard to tell with broken hardware. Does it do what you want except the servo is spinning?

ty for answering the stepper does spin and the sensors does detect object but the problem is the micro serve that i used.


this is my diagram i remove the buzzer because i dont need it

flap.writeMicroseconds();  // μs

I think you meant to use ...

flap.write();  // degrees

i already done that but it didn't work

Sure, but when you get your new (unmodified, 0-180 deg) servo:

  • using servoflap.write(); will work with the values shown in your code (degrees)
  • If you want to use flap.writeMicroseconds(); then you'll need to use values in the range 1000-2000, where 1000 μs = 0 deg , 1500 μs = 90 deg and 2000 μs = 180 deg.

thanks this helps alot
i will test this

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