Problem integrating code to control a servo into a second sketch

Hi, I am currently creating a project involving a Servo motor, two LED’S and a HCSR04 sensor. The aim of this project is to mount the sensor on the servo in a casing, have the servo rotate forward and back whilst having the sensor detect objects placed in front of it within a certain distance. The two LED’S are red and green. When no object is being detected the green LED is on. When an object is detected the red LED turns on and the green turns off. I was able to create a sketch where the LED’s and sensor worked perfectly together and demonstrated the function I laid out above adequately. However when I add the code to control the servo things go wrong. The green LED remains on when no object is detected, yet when I place an object in front of the sensor the red LED does not light up, instead the green LED stays on. Any help on how I could successfully integrate the servo code and the working sketch would be greatly appreciated. I am only beginning my Arduino journey so apologies if there is a really simple fix to this that I can’t recognize. I have listed both codes below separately. I add the servo code by just putting it at the bottom of where it is in its own sketch. For example the void setup code is placed at the bottom of void setup in the combined sketch. Just thought that might be useful information. Thanks.


#include <Servo.h>
Servo myservo;
int pos = 0;

void setup() {
myservo.attach (4);

}

void loop() {
for (pos = 0; pos <= 150; pos += 1) {
    myservo.write (pos);
    delay (60);
  }
  for (pos = 150; pos <= 0; pos += 1) {
    myservo.write (pos);
    delay (60);
  }

}
const int trig = 12;
const int echo = 13;
int duration = 0;
int distance = 0;
const int LED1 = 7;
const int LED2 = 8;

void setup() {
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);

  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig , HIGH);
  delayMicroseconds(10);
  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 28.5 ;
  Serial.println(distance);


  if ( distance <= 15 )
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);

  }

  }

Why don't you try to put them together. Here are tutorials on merging (combining) codes.
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
https://en.wikiversity.org/wiki/Arduino_Sketch_Merge

If you have trouble, post your code, tell us what the code actually does and how that differs from what you want the code to do.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

How is the servo powered?

Note: If pulseIn() doesn't see an echo pulse it will return 0. You may want to check for that.

Note: You should set a timeout on the pulseIn(). The default timeout is one second but the effective range of the HC-SR04 is about 5 meters (500 cm) or about 28500 microseconds round-trip. A timeout of 30000 microseconds should be more than enough time.

Hi, I seen your message and combined the code but the circuit is not operating as I intend it to. All components; the servo, LED’S and the sensor are connected through a breadboard that is powered by the 5V pin on the Arduino Uno board. The function I desire is for the Red LED to light up when an object comes within a certain distance of the sensor. When no object is detected the green LED is always on. This green LED turns off when an object is detected. The role of the servo motor is simply to rotate between two points. I have attached the combined code below. Based on my own basic knowledge I assume some fumctions in the sketch are contradicting each other I just do not know which.

[code]
const int trig = 12;
const int echo = 13;
int duration = 0;
int distance = 0;
const int LED1 = 7;
const int LED2 = 8;
#include <Servo.h>
Servo myservo;
int pos = 0;

void setup() {
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  Serial.begin(9600);
  myservo.attach (4);
}

void loop() {
  for (pos = 0; pos <= 150; pos += 1) {
    myservo.write (pos);
    delay (60);
  }
  for (pos = 150; pos <= 0; pos += 1) {
    myservo.write (pos);
    delay (60);
  }
  digitalWrite(trig , HIGH);
  delayMicroseconds(10);
  digitalWrite(trig , LOW);
  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 28.5 ;
  Serial.println(distance);
  if ( distance <= 15 )
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
}

[/code]

 for (pos = 150; pos <= 0; pos += 1) {

Shouldn’t that be pos >= 0 and pos -= 1 to go from 150 to 0?

I Attached a servo, a HCSR04 and 2 LEDs and ran your code with the for statement changed to:

for (pos = 150; pos >= 0; pos -= 1) {

Here is what does:
The servo moves from 0 to 150. That takes 9 seconds (150 * 0.06).

The servo moves from 150 to 0. That takes 9 seconds (150 * 0.06).

The range is measured and displayed.

The LED states are set according to the range.

The LED stay in that state until the next measurement 18 seconds later.

If that is not what you want, tell us what it is.

Are you planning on connecting the HCSR04 to the servo to do scans?

OK so I researched online and found another code that would move the servo. This allows the whole project to work as intended. However the response rate for the LED’S/sensor is now very low. For example the light flashed green when no object is in front of it. I place the object in front of the sensor yet it takes at least five seconds for the LED to turn red. With the speed of the servo the object would nearly be out of the sensors peripheral range by the time the LED turns red. Is there any way I can increase the reaction speed of the LED’S without altering the speed of the servo. Just in relation to your questions above. The servo is simply a mount for the HCSR04. All I require of it is to rotate over and back. The sensor whilst on the servo will scan for objects within a certain range. The LED’S allow me to know whether or not an object has been detected or not. I have attached the updated code below. I believe the delay function may be to blame for the slow reaction speeds. Perhaps the Arduino is assigning that function to both the the servo and the LED’S?

[code]

const int trig = 12;
const int echo = 13;
int duration = 0;
int distance = 0;
const int LED1 = 7;
const int LED2 = 8;
#include <Servo.h>
int servoPin = 4;
Servo Servo1;

void setup() {
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  Serial.begin(9600);
  Servo1.attach (4);
}
void loop() {
  digitalWrite(trig , HIGH);
  delayMicroseconds(10);
  digitalWrite(trig , LOW);
  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 28.5 ;
  Serial.println(distance);
  if ( distance <= 15 )
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
  Servo1.write(0);
  delay(10000);
  Servo1.write(90);
  delay(10000);
  Servo1.write(180);
  delay(10000);
}

[/code]

Servo1.write(0);
  delay(10000);
  Servo1.write(90);
  delay(10000);
  Servo1.write(180);
  delay(10000);
}

Are these movements just for visual effect?

I guess, the main function of the servo is just to rotate over and back. I have attached my newest code below that works with the millis() function. Do you know how I could get the servo to rotate again as currently it rotates once anti clockwise then stops. I want it to go anticlockwise to a point, stop then return to the original start point. It must not use the delay function as this completely messes up the whole project. I copied and pasted the code to control the servo into the sketch. I am currently trying to work through it and figure out how it works. Any help would be greatly appreciated.

[code]
const int trig = 12;
const int echo = 13;
int duration = 0;
int distance = 0;
const int LED1 = 7;
const int LED2 = 8;
#include <Servo.h>
Servo myservo;
int pos = 0;
unsigned long MOVING_TIME = 3000;
unsigned long moveStartTime;
int startAngle = 0;
int stopAngle  = 180;

void setup() {
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);

  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  Serial.begin(9600);
  myservo.attach (4);
  moveStartTime = millis();
}

void loop() {
  digitalWrite(trig , HIGH);
  delayMicroseconds(10);
  digitalWrite(trig , LOW);

  duration = pulseIn(echo , HIGH);
  distance = (duration / 2) / 28.5 ;
  Serial.println(distance);


  if ( distance <= 15 )
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
  unsigned long progress = millis() - moveStartTime;

  if (progress <= MOVING_TIME) {
    long angle = map(progress, 0, MOVING_TIME, startAngle, stopAngle);
    myservo.write(angle);
  }
}

[/code]

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