How to delay the sound of the piezo buzzer.in arduino - arduino

I'm trying to implement a car warning sound using a piezo buzzer and an ultrasonic sensor.If an object is 50 to 30 cm away, it will sound for 1 second and try to turn off the sound for 1 second, but I can't think of a way to implement this code. Can you help me? Here is my code.
#define echoPin 4
#define trigPin 3
#define buzPin 5
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzPin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
digitalWrite(echoPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
float distance = ((float)(340 * duration) / 10000) / 2;
Serial.print(distance);
Serial.println("cm");
if (distance <= 50 && distance > 31)
{
tone(buzPin, 391, 1000);
}
else if (distance <= 30 && distance > 21)
{
tone(buzPin, 391, 500);
}
else if (distance <= 20 && distance > 11)
{
tone(buzPin, 391, 100);
}
else if (distance <= 10)
{
tone(buzPin, 391);
}
else {
noTone(buzPin);
}
}

After each time you tell the arduino to start making a tone (this is the tone() command) add some delay. To do this just follow the instructions bellow:
//code
if(statement)
{
tone(buzPin, 391, 500);
delay(x); //x=with the delay you want the buzzer to sound in ms
noTone(buzPin); //after that the sound stops
delay(y); //y=with the delay you want the buzzer to be silent in ms
}

Related

how to set servo to rotate only once in 1 condition

I want to make a gate using 360 servo and ultrasonic sensor, here I use if else. when ultrasonic conditions >= 10 cm the servo should rotate one time to the right for 5 seconds, and vice versa. but when it reaches the condition >= 10 cm where the servo continues to rotate without stopping, how to make it stop in 1 rotation? and also i need distance data from ultrasonic sensor to display.
I'm a beginner in this, I will be very grateful for the help.
this is my code :
#include <Wire.h>
#include <Servo.h> //Servo library
#define echoPin 3 //triger pin, echo pin
#define triggerPin 4
int waktu;
int jarak;
int hasiljarak;
Servo servo;
void setup()
{
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
waktu = pulseIn(echoPin, HIGH);
jarak = waktu * 0.034 / 2;
Serial.print(jarak);
Serial.print(" CM");
delay(100);
servo.attach(9);
if(jarak<=10)
{
servo.write(2000);
delay(5000);
}
else if(jarak>=30)
{
servo.write(1000);
delay(5000);
}
else
{
servo.write(1500);
delay(1000);
}
servo.detach();
}
i've finish the servo with this code
void loop()
{
int ultra1 = tinggiUltra - sonar.ping_cm();
int ultra2 = tinggiUltra - sonar2.ping_cm();
delay(1000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(ultra1); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
Serial.print(ultra2);
Serial.println("cm");
if (ultra1 >= 11 && ultra1 <= 40 && ultra2 >=11)
{
servo.write(posClose);
delay(2000);
}
else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
{
servo.write(posOpen);
delay(2000);
}
else if (ultra2 <= 4 && ultra2 >= 0)
{
servo.write(pos);
delay(2000);
}
statPin = servo.read();
}
delay(2000);
}
else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
{
servo.write(posOpen);
delay(2000);
}
else if (ultra2 <= 4 && ultra2 >= 0)
{
servo.write(pos);
delay(2000);
}
statPin = servo.read();
}
for the servo I use it works normally as i wanted, and I use the MG955 360 servo. for the project I made using threads to open and close the door. I used if else for the code, and it worked fine by adding a parameter to restrict the movement of the door that I made. and thanks for the answer
if someone have to correct my program code, i'm grateful

How to combine two Adruino Sketches into one

I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs
Program 1:
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
{
digitalWrite(3,HIGH);//Makes the LED glow in Dark.
}
else
{
digitalWrite(3,LOW);//Turns the LED OFF in Light.
}
}
Program 2:
#define trigPin 13
#define echoPin 12
#define led 11
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, 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 < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Thank you!
You can use a timer and each 500 milliseconds run loop2
The first loop run normaly but the second loop run each 500 milliseconds
#define trigPin 13
#define echoPin 12
#define led 11
#define DELAY 500
int ldr = A0; //Set A0(Analog Input) for LDR.
int value = 0;
long timer = millis(); // a timer for timing 500 milliseconds
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
value = analogRead(ldr); //Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if (value < 300)
{
digitalWrite(3, HIGH); //Makes the LED glow in Dark.
}
else
{
digitalWrite(3, LOW); //Turns the LED OFF in Light.
}
if (millis() - timer >= DELAY ) {
timer = millis();//reset timer
loop2();
}
}
void loop2() {
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 < 10)
{ digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print(distance);
Serial.println(" cm");
}

How do i make an ultrasonic sensor trigger an LED to stay on until reset?

So, I know it's probably SUPER simple, but I'm new to arduino and I'm just drawing a blank. I'm making a motion detector with an HC-SRO4 ultrasonic sensor. Right now I have it set so whenever it senses an object within 60 cm, it turns on an LED light, but when the object disappears, the LED turns off. What I would like to happen is that it would stay on until I press a button to reset it. Any help is really appreciated and i thank you in advance.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
for this you have make low pushbutton pin while pressing.once the oobject came near the flag will set once the set it will move to while loop and run until the button pressed
#define LED 8
#define trigPin 12
#define echoPin 13
#define push_button 5
int flag=0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
pinMode(push_button,INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(digitalRead()==0)
{
flag=0;
}
if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
flag=1;
while(flag=1)
{
digitalWrite(LED, HIGH);}
}}

Ultrasonic sensors

Now i am using 2 ultrasonic sensors but i found that they become unstable outdoors and give random readings .....although they act normally inside a room .....how can i solve this problem???
That is the code
it seems like the off Sensor always Reading and i checked the wiring and it is good so i don't really know where is the problem is ??
#define trig 2
#define echo 3
#define led 7
#define relay 4
#define trig2 10
#define echo2 9
#define led2 8
long duration,distance,OnSensor,OffSensor;
void setup() {
// put your setup code here, to run once:
pinMode(echo,INPUT);
pinMode(echo2,INPUT);
Serial.begin(9600);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(trig,OUTPUT);
pinMode(trig2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led,LOW);
digitalWrite(led2,LOW);
digitalWrite(relay,LOW);
SonarSensor(trig2, echo2);
OffSensor = distance;
delay(50);
SonarSensor(trig, echo);
OnSensor=distance;
if(OnSensor<80&&OffSensor>=80){//sensor 1 activated
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
delay(1000);
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor>80){//No sensor activated
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor<80){//sensor2 activated
digitalWrite(led2,HIGH);
digitalWrite(relay,LOW);
delay(5000);
}
else if(OnSensor<80&&OffSensor<80){//Both sensors activated
digitalWrite(led2,HIGH);
digitalWrite(led,HIGH);
digitalWrite(relay,LOW);
delay(3000);
}
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
I think there are 2 problem here
You use 2 sensor
List item
Use use it outdoor
From my experience the ultra sonic wave can bouncing around object many time. depend on your setup, second sensor might read echo form first sensor.
follow these step
1. Try to use 1 sensor per time to see if it error
2. If single sensor work you should add more delay after sensor reading
3. Plot the data to see how it look like you might need digital filter to improve reading
For step 3 if you use sensor as On Off by threshold you can see my example code.
This code work the same way as button de-bouncing so in need to trig continuously for 10 time to prevent false alarm (This code for single sensor)
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define RELAY_PIN 4
/**/
#define ULTRASONIC_INTERVAL_MS 100//50
#define ULTRASONIC_TRIG_COUNT_THRESH 10
#define ULTRASONIC_DISTACE_THRESH_CM 50
/**/
#define RELAY_TRIG__INTERVAL_MS 5000
#define RELAY_TRIG_LOGIC HIGH
void setup()
{
pinMode (RELAY_PIN, OUTPUT);
pinMode (ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode (ULTRASONIC_ECHO_PIN, INPUT);
Serial.begin(115200);
}
void loop()
{
static unsigned long ultrasonic_timer = millis();
static unsigned long relay_trig_timer = millis();
static float us_distance_cm = 0;
static int us_trig_count = 0;
static byte relay_state = !RELAY_TRIG_LOGIC; // initial value as off;
if (millis() - ultrasonic_timer >= ULTRASONIC_INTERVAL_MS) {
ultrasonic_timer += ULTRASONIC_INTERVAL_MS;
us_distance_cm = getDistanceCM();
if (us_distance_cm <= ULTRASONIC_DISTACE_THRESH_CM) {
if (us_trig_count >= ULTRASONIC_TRIG_COUNT_THRESH) {
digitalWrite(RELAY_PIN, RELAY_TRIG_LOGIC);
relay_state = RELAY_TRIG_LOGIC;
relay_trig_timer = millis();
}
else {
us_trig_count++;
}
}
else {
us_trig_count = 0;
}
Serial.print("distance = ");
Serial.print(us_distance_cm);
Serial.print("cm, relay = ");
Serial.print(relay_state);
Serial.print(", count = ");
Serial.println(us_trig_count);
}
if (relay_state == RELAY_TRIG_LOGIC) {
if (millis() - relay_trig_timer > RELAY_TRIG__INTERVAL_MS) {
relay_state = !RELAY_TRIG_LOGIC;
digitalWrite(RELAY_PIN, !RELAY_TRIG_LOGIC);
}
}
}
float getDistanceCM() {
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
unsigned long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH, 50000);
float distance = float(duration * 343) / 20000.0;
delay(5);
return distance;
}

On/Off switch with button on arduino

I'm trying to create an Arduino circuit which runs this code. Basically, whith an ultrasonic sensor module, I set different distance ranges.
Within every range I chose to play a buzzer and light up a RGB led with different combinations.
Everything worked so far until I decided to add a button to act as a switch:
I can't think of a code that "turnes on/off" the circuit. To be more specific I want to light up a led (pin n.11) if the circuit is off, and to work normally when it's on, but now it works in the "on" state only if I keep pressed the button, and if I release it, the circuit "locks itself" on the last set of frequency (for the buzzer) and color (for the RGB led).
//pins for buzzer and ultrasonic sensor
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
//pins for rgb led(color changes as i get closer to an object)
const int VERDE = 3;
const int BLU = 5;
const int ROSSO = 6;
//pin for led representing main state of the circuit
const int accPin = 7;
// pins for button i want to use as an on/off switch
const int buttonPin = 11;
int stato=0;
int old = 0;
const int delayTime = 5;
void setup()
{
pinMode(VERDE, OUTPUT);
pinMode(BLU, OUTPUT);
pinMode(ROSSO, OUTPUT);
pinMode(accPin,OUTPUT);
digitalWrite(VERDE, LOW);
digitalWrite(BLU, LOW);
digitalWrite(ROSSO, LOW);
digitalWrite(accPin,LOW);
pinMode(buttonPin,INPUT);
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop(){
stato=digitalRead(buttonPin);
//if the circuit is off and i turn it on by pressing the button, or if the circuit's already on, then do the things
if(((stato==1)&&(old==0))||((stato==0)&&(old==1))){
digitalWrite(accPin,LOW);
int duration, distance; // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
digitalWrite(trigPin, HIGH); // Output pulse with 1ms width on trigPin
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Measure the pulse input in echo pin
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// the following code is just a test to see get a different frequency from the buzzer and a different color from the rgb led, while moving from the facing object
if (distance <= 10 && distance >= 0) {
// Buzz
tone(buzzPin, 2000);
digitalWrite(ROSSO, HIGH);
}
if (distance <= 20 && distance > 10) {
// Buzz
tone(buzzPin, 1750);
digitalWrite(BLU, HIGH);
digitalWrite(ROSSO, HIGH);
}
if (distance <= 30 && distance > 20) {
// Buzz
tone(buzzPin, 1250);
digitalWrite(BLU, HIGH);
}
if (distance <= 40 && distance > 30) {
// Buzz
tone(buzzPin, 1000);
digitalWrite(BLU, HIGH);
digitalWrite(VERDE, HIGH);
}
if (distance <= 80 && distance > 50) {
// Buzz
tone(buzzPin, 750);
digitalWrite(VERDE, HIGH);
}
if(distance>=81){
noTone(buzzPin);// if out of range then shut up
}
delay(60); // Waiting 60 ms won't hurt any one
digitalWrite(VERDE, LOW);
digitalWrite(BLU, LOW);
digitalWrite(ROSSO, LOW);
old=1;
}
//if the circuit is on and i press the button, turn it off
if((old==1)&&(stato=1)){old=0;}
//if the circuit is off and if i let it like this, don't do nothing, but turn on the a led to make it obvious(pin n.11)
if((old==0)&&(stato==0)){
digitalWrite(VERDE, LOW);
digitalWrite(BLU, LOW);
digitalWrite(ROSSO, LOW);
noTone(buzzPin);
digitalWrite(accPin,HIGH);
}
}
Create a function with your whole code, or with piece that starts your process, than attach interrupt to the button pin (you'll have to replace the button pin). Use booleans to toggle button press.

Resources