hi guys,
my project is complete but sometimes it gives a problem. so can anyone help me in improving it?
my project:
it has two ultrasonic sensors which detect whether there is a person in the range.
if detected the servo motor rotates by 180 degrees and the df player mini (DFPlayer Mini Mp3 Player - DFRobot Wiki) plays the audio1 saved on the micro sd card. then the 3 LEDs are turned one by one depending on whether the person is in range along with the audio.
if the person is in range the LEDs should turn on continuously,
as soon the person is out of range the servo turns back to zero degrees and plays the audio 5.
but the problem is that sometimes even when the person is range the servo turns to zero degrees and again starts from the beginning.
[/#include <NewPing.h>2
#include <TMRpcm.h>
#include <SPI.h>
TMRpcm tmrpcm;
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
int step = 0;
#include <Servo.h>;
Servo myservo;
int pos = 0;
int dist1;
int dist2 ;
NewPing sonar1(5, 6, 100);
NewPing sonar2(10, 11, 100);
int led1 = 13;
int led2 = 12;
int led3 =9;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
myservo.attach(3);
Serial.begin (9600);
mp3_set_serial (Serial);
delay(1);
mp3_set_volume (30);
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS1 = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
dist1 = (uS1 / US_ROUNDTRIP_CM); // Convert ping time to cm and print result.
Serial.print(dist1); //print the distance.
Serial.println("dist1cm");
unsigned int uS2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
dist2 = (uS2 / US_ROUNDTRIP_CM); // Convert ping time to cm and print result.
Serial.print(dist2); //print the distance.
Serial.println("dist2cm");
switch(step) {
case 0: do_step_0(); break;
case 1: do_step_1(); break;
case 2: do_step_2(); break;
case 3: do_step_3(); break;
case 4: do_step_4(); break;
case 5: do_step_5(); break;
}
}
void do_step_0() {
if (((dist1>0)&&(dist1<40))||((dist2>0)&&(dist2<40)))
{
step = 1;
}
}
void do_step_1()
{
for (pos = 0; pos <= 180; pos += 10)
{
myservo.write(pos);
delay(15);
}
mp3_play (1);
delay (6000);
mp3_stop ();
step = 3;
}
void do_step_2() {
mp3_play (5);
delay (2200);
mp3_stop ();
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos);
}
step = 0;
}
void do_step_3() {
if (((dist1>0)&&(dist1<50))||((dist2>0)&&(dist2<50)))
{
delay (500);
digitalWrite(led1,HIGH);
mp3_play (2);
delay (6000);
mp3_stop ();
digitalWrite(led1,LOW);
step = 4;
}
else {
step = 2;
}
}
void do_step_4() {
if (((dist1>0)&&(dist1<50))||((dist2>0)&&(dist2<50)))
{
digitalWrite(led2,HIGH);
mp3_play (3);
delay (6000);
mp3_stop ();
digitalWrite(led2,LOW);
step = 5;
}
else {
step = 2;
}
}
void do_step_5() {
if (((dist1>0)&&(dist1<50))||((dist2>0)&&(dist2<50)))
{
digitalWrite(led3,HIGH);
mp3_play (4);
delay (5000);
mp3_stop ();
digitalWrite(led3,LOW);
step = 3;
}
else {
step = 2;
}
}
]
shramik:
sometimes even when the person is range the servo turns to zero degrees and again starts from the beginning.
How are you powering the servo? If it is drawing power from the Arduino it may cause the voltage to drop and the Arduino to reset. Servos should have their own power supply with a common GND with the Arduino.
I have only glanced through your program. You have several delay()s - some of them long. The Arduino can do nothing during a delay(). If you need a responsive program you should not use delay(). Have a look at how millis() is used to manage timing without blocking in several things at a time
shramik:
the long delays are for the audio to play full and stop.
I can understand that you need to wait for the song to play - but that does not mean you have to use the delay() function.
i am also facing the problem of Arduino reset whenever there is a voltage fluctuation in the main ac supply.
That suggests to me that you have an electrical problem rather than a programming problem. What sort of AC supply do you have that has noticeable fluctuations?
What are you using to derive the Arduino power from the AC supply?
Maybe you need to power your Arduino with a battery that is recharged from the AC supply?
Robin2:
I can understand that you need to wait for the song to play - but that does not mean you have to use the delay() function.
That suggests to me that you have an electrical problem rather than a programming problem. What sort of AC supply do you have that has noticeable fluctuations?
What are you using to derive the Arduino power from the AC supply?
Maybe you need to power your Arduino with a battery that is recharged from the AC supply?
...R
the song is of 10sec so how to stop the program for the song to finish and do the next task?
i am powering Arduino from a 5v dc adapter . motor and LEDs are powered from separate 5v adapter with common ground.
Robin2:
That's why I gave you the link in Reply #1 - have you studied it?
How much current can the 5v adapter provide?
Does it provide a regulated 5v output?
You did not answer the more important question "What sort of AC supply do you have that has noticeable fluctuations?"
...R
I will go through it.
regulated switching 5v 1a adapter.
230v AC mains
Robin2:
I should have asked earlier ... how are you connecting the 5v adapter to your Arduino, and what Arduino board are you using?
If you are using the 230v AC mains in the UK I very much doubt that there are fluctuations in it that would have any impact on your Arduino.
What leads you to believe that the Arduino is resetting due to fluctuations in the AC supply?
I ask, because if that is not the cause there may a different problem that you are not investigating.
Have you tried temporarily running your Arduino from a pack of AA cells?
...R
I am using Arduino nano and powering it with mini USB port.
I tried running Arduino from my laptop it works well.
I had kept my project on for a full day and whenever there was voltage fluctuation ,the motor turned on automatically and starts playing the audio and then stop.
it was like someone has triggered it.
I am struggling to get you to tell me how you know there was a fluctutation - how did you measure it?
Clearly if the mains voltage falls to zero, even briefly, so will the output from your 5v adapter. Why would you expect anything else? But I doubt if that happens very often in the UK - maybe if there is a thunderstorm.
If your Nano works OK from your laptop and not from your 5v adapter I would assume there is a problem with the adapter.
Image from Reply #10 so we don't have to download it. See this Image Guide
Please make a pencil drawing of the circuit and post a photo of the drawing. It will be much easier to understand.
Robin2:
If your Nano works OK from your laptop and not from your 5v adapter I would assume there is a problem with the adapter.
robin2
now it is working fine
the problem might be with the PC ups or adapter.
I have changed it and now all good.
about the program, i need help on changing from delay to mills.
I went to the thread you gave .
can u help me with this 1 example, then I will do the rest.
how to change delay(6000) to mills? from the below code .
also when there is a person in range it stops and starts again.
can u suggest a better program for this project so that when a person is in range the servo never close and the loop of LEDs blinking one after another goes on continuously while checking if the person is in range and stop as soon the person is not in range and servo turns back to zero degree
shramik:
how to change delay(6000) to mills? from the below code .
Let's work on one thing at a time and leave the question in Reply #13 for later.
Try this - I think it should do what you want.
I have omitted the line delay(100); because I don't know what it is used for. If it is really needed that should also be replaced with millis().
wow sir ,
thank you so much .
i have to declare musicPlaying = false and musicStartMillis = 0 ?
and for rest of step, i can just change the time and replace musicPlaying as musicplaying1 and so on.
Robin2:
That sounds right - but you need to actually do it to be certain you have all the details correct.
...R
s, sir, i have replaced all delay with mills but still there is a problem with the project.
is there a better way of a program so that when there is a person in range the servo is on and led keeps on blinking and the audio is on simultaneously.
as soon the person is out of range it stops.