Hello, I am working on a project that uses an ultrasonic sensor, buzzer, vibration motor and a micro servo. The servo and the ultrasonic sensor are supposed to be functioning constantly at the same time. The ultrasonic sensor is supposed to turn on the buzzer and vibration motor as soon as it detects something in range.
Since you cant have two void loops I put the two functions into one loop, shown in my code below in the batch code, the problem I am facing is that when I program it the ultrasonic has an extreme delay in its detection, around 6 sec, and that delay also applies to the buzzer and vibration motor, they keep working for a good 6 sec even when the object has been removed from the sight of the ultrasonic.
However when I program the servo and the ultrasonic along with the buzzer and the vibration motor they work perfectly. The individual codes are shown below along with the combined code.
batch code:
#define trigPin 13
#define echoPin 12
#define motor 7
#define buzzer 6
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{ pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor, OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop(){
Sonic();
Servo();
myservo.write(45); // tell servo to go to position in variable 'pos'
delay(2000);
}
void Sonic()
{ 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;
if (distance < 70) // Checking the distance, you can change the value
{
digitalWrite(motor,HIGH); // When the the distance below 100cm
digitalWrite(buzzer,HIGH);
} else
{
digitalWrite(motor,LOW);// when greater than 100cm
digitalWrite(buzzer,LOW);
} delay(500);
}
void Servo()
{
myservo.write(45); // tell servo to go to position in variable 'pos'
delay(2000);
for(pos = 45; pos <= 135; pos += 3) // goes from 45 degrees to 135 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
delay(1000);
for(pos = 135; pos>=45; pos-=3) // goes from 135 degrees to 45 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
}
Servo Code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(45); // tell servo to go to position in variable 'pos'
delay(2000);
for(pos = 45; pos <= 135; pos += 3) // goes from 45 degrees to 135 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
delay(1000);
for(pos = 135; pos>=45; pos-=3) // goes from 135 degrees to 45 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
}
Ultra sonic:
#define trigPin 13
#define echoPin 12
#define motor 7
#define buzzer 6
void setup()
{ pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor, OUTPUT);
pinMode(buzzer,OUTPUT);
}
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;
if (distance < 70) // Checking the distance, you can change the value
{
digitalWrite(motor,HIGH); // When the the distance below 100cm
digitalWrite(buzzer,HIGH);
} else
{
digitalWrite(motor,LOW);// when greater than 100cm
digitalWrite(buzzer,LOW);
} delay(100);
}