LED's stop working when servo is on

Hello guys first of all i want to say that i’m new to the arduino world i’ve start learning arduino programming a month ago and today i’ve built my first project if a HC-SR04 mounted on a servo and if an object get cloder to the sensor the LED’s turn on one by one so i’ve wired everything and i wrote the code
and when i tried to run it the servo start turning but all the LED’s stays off sometime they turn all on and stay like that . i’ve removed the serovo and i tested only the LED’s and the sensor and it works but when i add the servo it dosen"t i hope you guys can help me solve this problem thanks

#include <Servo.h>;
Servo myservo;
int servopin = 9;
int x = 10;
int distance;
long duration;
int trig;
int echo;
int distance1;
int distance2;
int distance3;
int distance4;
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 11;


void setup() {

  myservo.attach(servopin);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}

void loop() {
  distance1 =  realdistance(x += 15);
  distance2 =  realdistance(x += 15);
  distance3 =  realdistance(x += 15);
  distance4 =  realdistance(x += 15);

  if (x > 179) {
    x = 0;
  }
  analogWrite(LED1, distance1);
  delay(300);
  analogWrite(LED2, distance2);
  delay(300);
  analogWrite(LED3, distance3);
  delay(300);
  analogWrite(LED4, distance4);
  delay(300);
}
int realdistance(int pos) {

  myservo.write(pos);
  delay(600);

  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  if (distance <= 80) {

    distance1 = map(distance, 80, 60, 0, 255);
    distance1 = constrain(distance1, 0, 255);
  }
  else if (distance <= 60 ) {

    distance2 = map(distance, 60, 40, 0, 255);
    distance2 = constrain(distance2, 0, 255);
  }

  else if (distance <= 40) {

    distance3 = map(distance, 40, 20, 0, 255);
    distance3 = constrain(distance3, 0, 255);
  }

  else if (distance <= 20) {

    distance4 = map(distance, 20, 0, 0, 255);
    distance4 = constrain(distance4, 0, 255);
  }

  return distance1;
  return distance2;
  return distance3;
  return distance4;



}
  return distance1;
  return distance2;
  return distance3;
  return distance4;

A function (that you never call) can only return one value. Not four.

 if (x > 179) {
    x = 0;
  }

x will always be 10.

i've removed the serovo and i tested only the LED's and the sensor and it works

Not with this code.

Can you supply the code that works so we can help?

See : there

so i’ve removed the servo and everything works here’s the new code and thank you for your help

int trig = 8;
int echo = 7;
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 11;
long duration;
int distance;
int distance1;
int distance2;
int distance3;
int distance4;


void setup() {


  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

}

void loop() {

  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;


  if ( distance <= 80 ) {

    distance1 = map(distance, 80, 60, 0, 255);
    distance1 = constrain(distance1, 0, 255);
    analogWrite(LED1, distance1);
  } else {

    analogWrite(LED1, LOW);

  }

  if (distance <= 60 ) {

    distance2 = map(distance, 60, 40, 0, 255);
    distance2 = constrain(distance2, 0, 255);
    analogWrite(LED2, distance2);
  } else {

    analogWrite(LED2, LOW);

  }

  if (distance <= 40 ) {

    distance3 = map(distance, 40, 20, 0, 255);
    distance3 = constrain(distance3, 0, 255);
    analogWrite(LED3, distance3);
  } else {

    analogWrite(LED3, LOW);

  }

  if (distance <= 20 ) {
    distance4 = map(distance, 20, 0, 0, 255);
    distance4 = constrain(distance4, 0, 255);
    analogWrite(LED4, distance4);
  } else {

    analogWrite(LED4, LOW);

  }


}

Now we need the code for the servo...

and this is the code for the sevo

#include <Servo.h>;
Servo myservo;
int servopin = 9;
int x = 10;

void setup() {

 myservo.attach(servopin);

}

void loop() {

  myservo.write(x += 15);
  delay(600);
  if (x >= 179) {

    x = 0;
  }


}

Running the servo by itself works?

yes when i run the servo it works and when i run the sensor and LED's it works but when i run them together only the servo works

How do you power your servo?

i use a 9V battery , a 5v regulator and two decoupling capacitors in the end i get a steady 5V output

send the complete sketch now.

Hi,
Hope you are not using one of these 9V batteries?

It will not give you the current needed to run that load.

Tom... :slight_smile:
PS.They are for smoke detectors and DMMs.

Would a 1200 mAh lithium ion be a good choice?

EDIT : and a 600 mAh rechargable lithium ion?

i’ve just finished the code now it works thanks for you help here’s the code

#include <Servo.h>;
Servo myservo;

int trig = 8;
int echo = 7;
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 11;
long duration;
int distance;
int distance1;
int distance2;
int distance3;
int distance4;
int servopin = 9;
int x = 0;

void setup() {

  myservo.attach(servopin);
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

}

void loop() {
  if (x >= 179) {
    x = 0;
  }
  if (LED2 == HIGH) {
    LED1 = HIGH;
  }
  x += 5;
  distance1 = realdistance(x);
  analogWrite(LED1, distance1);

  distance2 = realdistance1(x);
  analogWrite(LED2, distance2);

  distance3 = realdistance2(x);
  analogWrite(LED3, distance2);

  distance4 = realdistance3(x);
  analogWrite(LED4, distance2);

}

int realdistance (int pos) {

  myservo.write(pos);
  delay(200);
  detect;
  distance1 = map(distance, 80, 60, 0, 255);
  distance1 = constrain(distance1, 0, 255);
  return distance1;

}

int realdistance1 (int pos) {

  myservo.write(pos);
  delay(200);
  detect;
  distance2 = map(distance, 60, 40, 0, 255);
  distance2 = constrain(distance2, 0, 255);
  return distance2;

}

int realdistance2 (int pos) {

  myservo.write(pos);
  delay(200);
  detect;
  distance3 = map(distance, 40, 20, 0, 255);
  distance3 = constrain(distance3, 0, 255);
  return distance3;

}

int realdistance3 (int pos) {

  myservo.write(pos);
  delay(200);
  detect;
  distance4 = map(distance, 20, 0, 0, 255);
  distance4 = constrain(distance4, 0, 255);
  return distance4;

}

void detect () {

  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
}

yes i'm using a 9V battery and after runing the code the servo stoped working the problem was from the battery but only the servo is connected to the battery the LED's and the sensor is connected to the arduino
and what can i use instead of the 9V battery ?

The specs on your motor will tell you that. 250mA, 500mA, 1A. Match a regulated power supply to the specs.

PS. You worked hard. Congratulations on your success!

thank you i really appreciate it!! for the battery i'm gonna use 6 2450 mAh Nimh 1.2V AA's batteys they would supply 7.2V and plenty of current

Hi,
Good to hear you have it performing as you need it.

You will need to consider a DC-DC converter rather than a linear type regulator too.

A LM7805 will not provide a regulated 5V if the input volts drops to 7V or lower.

Tom... :slight_smile:

what if i add a decoupling capacitors will it help ?

omarbentaleb:
what if i add a decoupling capacitors will it help ?

It will not help if the problem is insufficient voltage or current.