i'm making distance sensing experiment
if any object comes in the distance less than 50 cm buzzer will buzz
if any object is more than than the distance of 16 cm motor will start
i did this coding in two different loops .
but they are not working . please someone help me fastt
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
int const motorPin = 5;
int const pottrigPin = 4;
int const potechoPin = 3;
void setup()
{
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
pinMode(motorPin, OUTPUT); // this is for motor to vibrate
pinMode(pottrigPin, OUTPUT); // this is another pin for 3rd sensor for pothole detection this sends waves
pinMode(potechoPin, INPUT); // this is another pin for 3rd sensor for pothole detection this receives waves
}
void normal()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW); // Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH); // Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration / 2) / 29.1; // if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) { // Buzz
digitalWrite(buzzPin, HIGH);
} else { // Don't buzz
digitalWrite(buzzPin, LOW);
}
delay(60); // Waiting 60 ms won't create too much problem
}
void pothole() {
int potduration, potdistance;
digitalWrite(pottrigPin, HIGH);
delay(1);
digitalWrite(pottrigPin, LOW);
potduration = pulseIn(potechoPin, HIGH);
potdistance = (potduration / 2) / 29.1;
if (potdistance >= 16 && potdistance <= 0) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
delay(60);
}
void loop() {
normal();
pothole();
}
The processor is only single threaded, it can only do one thing at a time.
For example, while it is delaying(1) or (60) in one function, it can't run the other.
I suggest you pull the delay(60) back into loop(), or better yet use Blink Without Delay (File:Examples:Digital I think) and then not have delay at all.
Keep working and experimenting and researching until you understand every bit of the BlinkWithoutDelay sketch. This is an essential concept for you to understand if you are to have success with Arduino. Once you understand BlinkWithoutDelay break your sketch into two parts and get each one working perfectly without using delay, they you will be ready to combine them. If you run into problems along the way come back with specific questions and code and we'll try to help.
If we write the code for you then you won't learn anything.
coder1212:
do you know if there is any way to fix my code.
I have a mp3 player and lights that play with it but the mp3 player wont play until the lights are done blinking even though it is higher in the code
You should not use delay(). Study File > Examples > 02.Digital > BlinkWithoutDelay and the associated tutorial: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
to understand how to write non-blocking code.
coder1212:
the mp3 player wont play until the lights are done blinking even though it is higher in the code
My guess would be that the first play command to the MP3 player is failing for some reason. It might be that the play command is sent too soon after the "select the TF card" command. To test that, you could increase the delay after sending the "select the TF card" command.