servo problems

hi,
i have a problem my servo goes up and down once when i type 1 or 0 instead of going up or going down .and my servo makes a steady noise or hum after it goes up or down.this is my code:

#include<Servo.h>

int degrees = 1;//rate of rotation of servo
int GreenLed = 10 ; //Use PWM
int SerPin = 8;//Servo input pin
int DoorClosed = 0;
int DoorOpened =73 ;
int speed = 150;
boolean Closed = true;
String BytesRecieved;
Servo servo;
boolean GotBytes = false;

void setup(){
    Serial.begin(9600);
    servo.attach(SerPin);
    pinMode(GreenLed, OUTPUT);
    pinMode(RedLed, OUTPUT);
    servo.write(DoorClosed);
    digitalWrite(GreenLed,1);
    
}

void loop(){
    ManualSequence();
}

void ManualSequence(){
    while(GotBytes == false){
        if(Serial.available()>0){
            char c = Serial.read();
            BytesRecieved += c;
            GotBytes = true;
        }
    }
    if(BytesRecieved == "0"){
        for(int i = DoorOpened; i>=DoorClosed; i-=degrees){
        servo.write(i);
        digitalWrite(GreenLed, 1);
        digitalWrite(RedLed, 1);
        delay(speed);
        }
        digitalWrite(GreenLed, 1);
        digitalWrite(RedLed, 0);

    }

    if(BytesRecieved == "1"){
        for(int i = DoorClosed; i<=DoorOpened; i+=degrees){
        servo.write(i);
        digitalWrite(GreenLed, 1);
        digitalWrite(RedLed, 1);
        delay(speed);
        }
        digitalWrite(RedLed,1);
        digitalWrite(GreenLed,0);

        
    }
    GotBytes = false;
    BytesRecieved = "";
}

Some servos cannot physically go all the way to 0 (or 180). If yours is like that the hum would be caused by the servo pushing against the end stop. Try the code with a lowest angle of 20 and see does that help.

I don't see any obvious reason for the behaviour you describe. What happens if you comment-out all the code for either option '1' or option '0' ?

If you are powering the servo from the Arduino 5v pin don't! That can cause erratic behaviour. Always give a servo its own power supply with a common GND with the Arduino.

...R

In my experience most hobby servos have a range of 120 to 150 degrees. That's because
they are designed to move a crank attached to a wing control surface on a model aeroplane,
any more range of motion is unnecessary.

Hello
can using of millis() function instead of delay function help.
thnx

HARI6:
can using of millis() function instead of delay function help.

It won't make any difference for your short test program.

For anything other than a demo program I would advise against the use of delay() so that the Arduino would be available for other things during the delay period. The demo Several Things at a Time illustrates the use of millis() for timing.

You have not responded to the comments in earlier Replies?

...R

Hello,

can u pls see the timings here.this code includes HC05 Module .
Here my problem is the servo is not listening to my delay(speed) command below.
pls tell me the problem and making it go according to delay command.

#include<Servo.h>

int degrees = ;//rate of rotation of servo
int GreenLed =  ; //Use PWM
int RedLed = ;//Use PWM
int Light = ;//Lights
int SerPin = ;//Servo input pin
int DoorClosed = ;
int DoorOpened = ;
int GndPin1 = ;
int GndPin2 = ;
int speed = ;
boolean Closed = true;
String BytesRecieved;
Servo servo;
boolean GotBytes = false;

void setup(){
    Serial.begin(9600);
    servo.attach(SerPin);
    pinMode(GreenLed, OUTPUT);
    pinMode(RedLed, OUTPUT);
    pinMode(GndPin1, OUTPUT);
    pinMode(GndPin2, OUTPUT);
    servo.write(DoorClosed);
    digitalWrite(GreenLed,1);
    digitalWrite(GndPin1, LOW);
    digitalWrite(GndPin2, LOW);
}

void loop(){
    ManualSequence();
}

void ManualSequence(){
        while(Serial.available()>0){
            char c = Serial.read();
            BytesRecieved += c;
        }
    if(BytesRecieved == "0"){
        servo.write(DoorClosed);
        delay(speed);
        digitalWrite(GreeenLed, 1);
        digitalWrite(RedLed, 0);
        digitalWrite(Light, 0);
    }
    else if(BytesRecieved == "1"){
        servo.write(DoorOpened);
        delay(speed);
        digitalWrite(RedLed,1);
        digitalWrite(GreenLed,0);
        digitalWrite(Light, 1);
        
    }
    BytesRecieved = "";
}

i have used an external 5v for servo.

Thnx

Hello,
i am making a garage ,whose door goes up and down on command by bluetooth module.When i press 1 or 0 it should go down and up respectively with a desired speed.my problem is that it doesn take delay function at all.

Servos just go immediately to where you tell them. To move them slowly you have to keep telling
them a new position at a given rate, like:

  for (int i = 45 ; i < 135 ; i++)
  {
    servo.write (i) ;
    delay (50) ;
  }

Better still use the techniques of blinkWithoutDelay to avoid using delay for a timed sequence
of events.

HARI6:
can u pls see the timings here.this code includes HC05 Module .

I am not going to do any more until I see some evidence of you engaging with us by responding to the various comments in earlier posts.

If you don't understand what has been said please tell us.

...R

the hum is stopped .my door was heavy a bit.i am powering by a external 5 v.i dont understand your second statement,"I don't see any obvious reason for the behaviour you describe. What happens if you comment-out all the code for either option '1' or option '0' ?"

HARI6:
What happens if you comment-out all the code for either option ‘1’ or option ‘0’ ?"

If you put comments // at the start of each line in a section then that code won’t be included. It is a good way to find out which section of the code is causing a problem. This is what I had in mind.

#include<Servo.h>

int degrees = 1;//rate of rotation of servo
int GreenLed = 10 ; //Use PWM
int SerPin = 8;//Servo input pin
int DoorClosed = 0;
int DoorOpened =73 ;
int speed = 150;
boolean Closed = true;
String BytesRecieved;
Servo servo;
boolean GotBytes = false;

void setup(){
    Serial.begin(9600);
    servo.attach(SerPin);
    pinMode(GreenLed, OUTPUT);
    pinMode(RedLed, OUTPUT);
    servo.write(DoorClosed);
    digitalWrite(GreenLed,1);
    
}

void loop(){
    ManualSequence();
}

void ManualSequence(){
    while(GotBytes == false){
        if(Serial.available()>0){
            char c = Serial.read();
            BytesRecieved += c;
            GotBytes = true;
        }
    }
    if(BytesRecieved == "0"){
        for(int i = DoorOpened; i>=DoorClosed; i-=degrees){
        servo.write(i);
        digitalWrite(GreenLed, 1);
        digitalWrite(RedLed, 1);
        delay(speed);
        }
        digitalWrite(GreenLed, 1);
        digitalWrite(RedLed, 0);

    }

    // if(BytesRecieved == "1"){
        // for(int i = DoorClosed; i<=DoorOpened; i+=degrees){
        // servo.write(i);
        // digitalWrite(GreenLed, 1);
        // digitalWrite(RedLed, 1);
        // delay(speed);
        // }
        // digitalWrite(RedLed,1);
        // digitalWrite(GreenLed,0);
    // }
    GotBytes = false;
    BytesRecieved = "";
}

…R

If i comment out either 1 or 0 the same problem that is the door goes down and up once instead of going only up or down when commands are given.

OK. I have tried you program.

I believe the problem happens when you try to close the door when it is already closed or if you try to open it when it is already open.

If the door is closed and you give it the close command it assumes it is open and instantly moves to the open position and then works through the close process.

You need a variable to record whether the door is open or closed (for example doorState = 'O') and then you only activate the close sequence if it is open.

Assuming (for example) the '1' activates the close sequence your code should be something like this

if(BytesRecieved == "1" && doorState == 'O';){

I think your program would be easier to understand if you send 'c' from the Serial Monitor to close the door and 'o' to open it.

And, please, please, the correct spelling is "Received"

...R