Hello, I would like to use the ultrasonic sensor to detect when something is within a 25cm distance, trigger the dfplayer mini and led at the same time. If the object has moved out of that distance the music stops and the led goes out as well. when something enters the distance again the music and led would go off again. The longer its in the sensors path the music will continue to play. I currently have everything wired up. Trig-6 echo-7
dfplayer tx-10 rx-11 I have nothing done with the led.
(post deleted by author)
no idea really
What is a "trigger led"?
Original topic merged with new one
I have a working code for everything except the led. I have tried attaching it to pin 10 which is for the dfplayer as well but the led stays on all the time. I need it to only come on when the music is playing
[code]
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int trig = 6;
int echo = 7;
SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
void setup ()
{
Serial.begin (9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) //Use softwareSerial to communicate with mp3.
{
Serial.println(F("DFPlayer error."));
while (true);
}
Serial.println("Setting up software serial —- done");
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(30);
myDFPlayer.setTimeOut(200);
}
//
void loop ()
{
// If you re-trigger the sonar too soon you
// might receive echoes from the previous trigger
delay(30);
// Send a sonar ping
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Measure the echo pulse which represents the
// echo time. 0 = no echo
unsigned long duration = pulseIn(echo, HIGH, 30000UL);
int distance = (duration / 2) / 21.9;
Serial.print(distance);
Serial.println("cm");
// Is there something in range?
bool isInRange = distance != 0 && distance < 30;
static bool wasInRange = false; // 'static' sticks around, like a global
if (isInRange != wasInRange)
{
// The state has changed
wasInRange = isInRange;
if (isInRange)
{
// Just entered range so start playing
myDFPlayer.play(0001); // WARNING: 0001 == 1 but 0010 == 8 because the leadin 0 means OCTAL.
}
else
{
// Just left range
myDFPlayer.pause();
Serial.println("pause");
}
}
}
[/code]
@jdkitzmiller1988
I am imagining that "trig" is an LED?? I wasn't sure so I added a ledPin.
Try this code:
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int trig = 6;
int echo = 7;
int ledPin = 13;
SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
void setup ()
{
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) //Use softwareSerial to communicate with mp3.
{
Serial.println(F("DFPlayer error."));
while (true);
}
Serial.println("Setting up software serial —- done");
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(30);
myDFPlayer.setTimeOut(200);
}
//
void loop ()
{
// If you re-trigger the sonar too soon you
// might receive echoes from the previous trigger
delay(30);
// Send a sonar ping
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Measure the echo pulse which represents the
// echo time. 0 = no echo
unsigned long duration = pulseIn(echo, HIGH, 30000UL);
int distance = (duration / 2) / 21.9;
Serial.print(distance);
Serial.println("cm");
// Is there something in range?
bool isInRange = distance != 0 && distance < 30;
static bool wasInRange = false; // 'static' sticks around, like a global
if (isInRange != wasInRange)
{
// The state has changed
wasInRange = isInRange;
if (isInRange)
{
// Just entered range so start playing
myDFPlayer.play(0001); // WARNING: 0001 == 1 but 0010 == 8 because the leadin 0 means OCTAL.
digitalWrite(ledPin, HIGH); // turn LED ON
}
else
{
// Just left range
myDFPlayer.pause();
Serial.println("pause");
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
}
I initialized the LED pin as 13, but you can change it to whatever pin you require. If you want the LED to turn off after a delay, try this:
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int trig = 6;
int echo = 7;
int ledPin = 13;
int ledState = LOW;
unsigned long previousMillis = 0;
SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
void setup ()
{
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) //Use softwareSerial to communicate with mp3.
{
Serial.println(F("DFPlayer error."));
while (true);
}
Serial.println("Setting up software serial —- done");
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(30);
myDFPlayer.setTimeOut(200);
}
//
void loop ()
{
// If you re-trigger the sonar too soon you
// might receive echoes from the previous trigger
delay(30);
// Send a sonar ping
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Measure the echo pulse which represents the
// echo time. 0 = no echo
unsigned long duration = pulseIn(echo, HIGH, 30000UL);
int distance = (duration / 2) / 21.9;
Serial.print(distance);
Serial.println("cm");
// Is there something in range?
bool isInRange = distance != 0 && distance < 30;
static bool wasInRange = false; // 'static' sticks around, like a global
if (isInRange != wasInRange)
{
// The state has changed
wasInRange = isInRange;
if (isInRange)
{
// Just entered range so start playing
myDFPlayer.play(0001); // WARNING: 0001 == 1 but 0010 == 8 because the leadin 0 means OCTAL.
digitalWrite(ledPin, HIGH); // turn LED ON
}
else
{
// Just left range
myDFPlayer.pause();
Serial.println("pause");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
}
}
I'm not 100% sure the delay code is perfect, but it is a good start.
@jdkitzmiller1988 Is it working now?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.