Servo motor keep restart the program

hey…
im try to control 2 position of servo motor and dim the led
but it seem the servo motor is sometime operate and some time it keep restarting the program it self…

#include <Servo.h>
Servo myservo;
int sensor =1; //on analog 1


void setup (){
pinMode(10, OUTPUT); //led on pin 10
Serial.begin(9600);
 myservo.attach(9);
 }
 
 void loop(){
 
 //read the value on A1 and then do move servo motor or dim the led
int val=analogRead(sensor);
//check the condition
if(val <300){
  myservo.write(45);
  analogWrite(10,val);
  delay(100);
}
 

if (val >=300 ){
  if (val < 600){
    myservo.write(90);
    analogWrite(10,val);
    delay(100);
  }
}
if (val >=600){
  if (val<800){
    myservo.write(180);
    analogWrite(10,val);
    delay(100);
    
  }
}

if (val >=900){
  myservo.write(0);
      analogWrite(10,val);
    delay(100);
}
 }

why my code keep restart after it read certain value, then it restart the program…?

How are you powering the servo? There's a small chance that moving or stalling a servo will cause the voltage supply to the Arduino to dip low enough to cause a reset, if you are powering the Arduino with the same power supply as the servo.

my servo Specification:

  • Plastic gears
  • Speed (sec/60deg): 0.16/4.8V, 0.14/6.0V
  • Torque (Kg-cm): 3.5/4.8V, 4.5/6.0V (Maximum 6.0V)
  • Size (mm): 40.8x20.18x36.5
  • Weight (g): 36
  • Rotation angle: 180 degree
  • Pulse width range: 0.546ms to 2.4ms (estimation)
  • Designed for "closed feedback".
  • Able to control the position of the motor
  • C/W plastic servo horn and accessories

im powering it using usb cable from my arduino..my servo work sometime, then it will restart it back in void setup() after turning some degree..then sometime led is not working..

im powering it using usb cable from my arduino..my servo work sometime, then it will restart it back in void setup() after turning some degree..then sometime led is not working..

You need to power the servos from a power supply seperate from the arduino and USB port.

You need to power the servos from a power supply seperate from the arduino and USB port

ok now im using 9v adapter to powered my servo..
it work fine in the fist place.. then when sensor value is >800 it become stuck, then it restart it self and then not function..when i push restart button, it running back..and keep restarting when the motor is rotate to 180 degree..

is there need to refresh my servo..is the servo library is using interrupt routine to operate or something?
do i need to put a delay else

You can combine conditional expressions in your if statements to significantly improve readability:

if(val <300)
{
  myservo.write(45);
  analogWrite(10,val);
  delay(100);
}
else if (val >=300 && val < 600)
{
   myservo.write(90);
   analogWrite(10,val);
   delay(100);
}
else if (val >=600 && val<800)
{
   myservo.write(180);
   analogWrite(10,val);
   delay(100);
}
else if (val >=900)
{
   myservo.write(0);
   analogWrite(10,val);
   delay(100);
}

Now, you can see that you have a lot of redundant code. It looks like you want to write val to pin 10 regardless of the value in val, and you want to delay for 100 milliseconds, regardless of the value in val.

Next, you want to look at the documentation for the analogWrite function, to see what the range of acceptable values is. Your if tests show that you understand the range of values output my the analogRead function, but, it appears that you do not know that the range of valid values for the analogWrite function is not the same.

If the servo is having issues when commanded to go to either extreme (0 or 180), one has to wonder if the servo is capable of getting to those extremes. Try changing the limits that you move the servo to to 30 and 150. If that works, keep making the values smaller and larger until the problem returns. Then, you’ll know what the limits of your servo are.

ok now im using 9v adapter to powered my servo..

Well, most servos are made to use an operational voltage range of 4.8-6.0V, with some a little higher. A 9v adapter may damage your servo or cause erratic operation due to the high voltage.