Arduino MP3 Project
d. bodnar revised 1-05-2015
10-16-16 1-17-18
| Here is a fix to provide lower power
consumption and no more clicks on power on / off http://work-now-dammit.blogspot.com/2016/08/dfplayer-mp3-module-power-onoff-clicks.html |
| Order from eBay
or http://www.banggood.com/DFPlayer-Mini-MP3-Player-Module-For-Arduino-p-969191.html They are also available on Amazon: https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=dfplayer good info here: http://forum.arduino.cc/index.php?topic=241021.15#main_content_section and here: http://translate.google.com/translate?u=http%3A%2F%2Famperka.ru%2Fproduct%2Fdf-player-mp3-module&hl=en&langpair=auto|en&tbb=1&ie=UTF-8 (translated to English)
|
![]() |
| Pin 1 - 5 volts Pin 2 - Arduino txd via 1K resistor Pin 3 - Arduino rxd via 1K resistor Pins 6 & 8 - 8 ohm speaker Pins 4 & 5 - line out (r & l) Pin 7 - ground |
Test Software for Arduino/*
Working well from 5 volt supply - still have to work on how the files play but it
works rather well
d. bodnar
12-28-14
*/
/*******************************************************************************
* DFPlayer_Mini_Mp3, This library provides a quite complete function for *
* DFPlayer mini mp3 module. *
* www.github.com/dfrobot/DFPlayer_Mini_Mp3 (github as default source provider)*
* DFRobot-A great source for opensource hardware and robot. *
* *
* This file is part of the DFplayer_Mini_Mp3 library. *
* *
* DFPlayer_Mini_Mp3 is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or any later version. *
* *
* DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with DFPlayer_Mini_Mp3. If not, see *
* <http://www.gnu.org/licenses/>. *
* *
******************************************************************************/
/*
* Copyright: DFRobot
* name: DFPlayer_Mini_Mp3 sample code
* Author: lisper <lisper.li@dfrobot.com>
* Date: 2014-05-30
* Description: sample code for DFPlayer Mini, this code is test on Uno
* note: mp3 file must put into mp3 folder in your tf card
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
//
void setup () {
Serial.begin (9600);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_set_volume (25); // 15 is low for unpowered speaker
// 30 good for unpowered speaker - requires power off to reset volume
}
//
void loop () {
mp3_play (1);
delay (6000);
mp3_next ();
delay (6000);
mp3_next ();
delay (6000);
mp3_next ();
delay (6000);
mp3_next ();
delay (6000);
mp3_next ();
delay (6000);
}
/*
mp3_play (); //start play
mp3_play (5); //play "mp3/0005.mp3"
mp3_next (); //play next
mp3_prev (); //play previous
mp3_set_volume (uint16_t volume); //0~30
mp3_set_EQ (); //0~5
mp3_pause ();
mp3_stop ();
void mp3_get_state (); //send get state command
void mp3_get_volume ();
void mp3_get_u_sum ();
void mp3_get_tf_sum ();
void mp3_get_flash_sum ();
void mp3_get_tf_current ();
void mp3_get_u_current ();
void mp3_get_flash_current ();
void mp3_single_loop (boolean state); //set single loop
void mp3_DAC (boolean state);
void mp3_random_play ();
*/
|
Program with LCD output - 2 line x 16 characters /*
Working well from 5 volt supply - still have to work on how the files play but it
works rather well
d. bodnar
12-28-14
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int potPin = A3; // select the input pin for the potentiometer
int potValue = 0; // variable to store the value coming from the pot
byte buffer[10];
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//
void setup () {
Serial.begin (9600);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_reset();
delay (400);
mp3_set_volume (10); // 15 is low for unpowered speaker
delay (400); // 30 good for unpowered speaker - requires power off to reset volume
Serial.println(" mp3-test");
lcd.begin (16,2); // <<----- My LCD was 16x2
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" MP3 Player");
// delay(1000);
lcd.setCursor(0,1);
lcd.print(" Version 1.0 ");
delay (1500);
lcd.home (); // go home
lcd.clear();
lcd.print("trainelectronics");
delay(1000);
lcd.setCursor(0,1);
lcd.print(" 2014 d. bodnar");
delay (2500);
//lcd.home (); // go home
//lcd.clear();
//lcd.print(" LAPS STATUS");
}
//
void loop () {
mp3_play (1);
Serial.println(" mp3-test -1");
delay (1000);
mp3_play (3);
Serial.println(" mp3-test -3");
delay (1000);
mp3_play (7);
Serial.println(" mp3-test -7");
delay (1000);
mp3_play (12);
Serial.println(" mp3-test -12");
delay (1000);
mp3_play (10);
Serial.println(" mp3-test -10");
delay (61000);
/*
Serial.println(" mp3-test - next -1");
mp3_next ();
delay (1500);
Serial.println(" mp3-test - next -2");
mp3_next ();
delay (1500);
Serial.println(" mp3-test - next -3");
mp3_next ();
delay (1500);
Serial.println(" mp3-test - next -4");
mp3_next ();
delay (1500);
Serial.println(" mp3-test - next -5");
mp3_next ();
delay (1500);
Serial.println(" mp3-test - next - last");
mp3_next ();
delay (1500);
*/
}
/*
mp3_play (); //start play
mp3_play (5); //play "mp3/0005.mp3"
mp3_next (); //play next
mp3_prev (); //play previous
mp3_set_volume (uint16_t volume); //0~30
mp3_set_EQ (); //0~5
mp3_pause ();
mp3_stop ();
void mp3_get_state (); //send get state command
void mp3_get_volume ();
void mp3_get_u_sum ();
void mp3_get_tf_sum ();
void mp3_get_flash_sum ();
void mp3_get_tf_current ();
void mp3_get_u_current ();
void mp3_get_flash_current ();
void mp3_single_loop (boolean state); //set single loop
void mp3_DAC (boolean state);
void mp3_random_play ();
*/
|
|
|
DFPlayer_MP3_2lineLCD_18b20 /*
Working well from 5 volt supply - still have to work on how the files play but it
works rather well
d. bodnar
12-28-14
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int potPin = A3; // select the input pin for the potentiometer
int potValue = 0; // variable to store the value coming from the pot
byte buffer[10];
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//
void setup () {
Serial.begin (9600);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_reset();
delay (400);
mp3_set_volume (10); // 15 is low for unpowered speaker
delay (400); // 30 good for unpowered speaker - requires power off to reset volume
Serial.println(" mp3-test");
lcd.begin (16,2); // <<----- My LCD was 16x2
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" MP3 Player");
// delay(1000);
lcd.setCursor(0,1);
lcd.print(" Version 1.0 ");
delay (1500);
lcd.home (); // go home
lcd.clear();
lcd.print("trainelectronics");
delay(1000);
lcd.setCursor(0,1);
lcd.print(" 2014 d. bodnar");
delay (2500);
//lcd.home (); // go home
//lcd.clear();
//lcd.print(" LAPS STATUS");
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
float xtemp = DallasTemperature::toFahrenheit(tempC);
lcd.setCursor (8,0);
lcd.print(xtemp);
delay(2000);
}
//
void loop () {
lcd.home (); // go home
lcd.clear();
lcd.print(" Output");
// delay(1000);
lcd.setCursor(0,1);
lcd.print(" test - 1");
mp3_play (1);
Serial.println(" mp3-test -1");
lcd.setCursor(0,1);
lcd.print(" test - 1");
delay (1000);
mp3_play (3);
Serial.println(" mp3-test -3");
lcd.setCursor(0,1);
lcd.print(" test - 3");
delay (1000);
mp3_play (7);
Serial.println(" mp3-test -7");
lcd.setCursor(0,1);
lcd.print(" test - 7");
delay (1000);
mp3_play (12);
Serial.println(" mp3-test -12");
lcd.setCursor(0,1);
lcd.print(" test - 12");
delay (1000);
mp3_play (10);
Serial.println(" mp3-test -10");
lcd.setCursor(0,1);
lcd.print(" test - 10");
for (int i=0; i <= 5; i++){
lcd.setCursor (10,0);
lcd.print(i);
delay(1000);
}
//delay (61000);
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
|
WORKING! Says temperature (as integer!) DFPlayer_MP3_2lineLCD_18b20_Ver2 /*
Working well from 5 volt supply - still have to work on how the files play but it
works rather well
d. bodnar
12-28-14
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int value = 0; //
float xtemp=0;
int potPin = A3; // select the input pin for the potentiometer
int potValue = 0; // variable to store the value coming from the pot
byte buffer[10];
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char buf[12];
//
void setup () {
Serial.begin (9600);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_reset();
delay (400);
mp3_set_volume (10); // 15 is low for unpowered speaker
delay (400); // 30 good for unpowered speaker - requires power off to reset volume
/* // Serial.println(" mp3-test");
// char buf[12]; // "-2147483648\0"
xtemp = 9812;
String thisString = itoa(xtemp, buf, 10);
Serial.println(thisString);
int lgth = thisString.length();
Serial.println(thisString.length());
Serial.println(lgth);
Serial.println(itoa(xtemp, buf, 10));
Serial.println("start");
for (int i=0; i <= lgth; i++){
Serial.println(thisString.substring(i,i+1));
}
*/
lcd.begin (16,2); // <<----- My LCD was 16x2
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" MP3 Player");
// delay(1000);
lcd.setCursor(0,1);
lcd.print(" Version 1.0 ");
delay (1500);
lcd.home (); // go home
lcd.clear();
lcd.print("trainelectronics");
delay(1000);
lcd.setCursor(0,1);
lcd.print(" 2014 d. bodnar");
delay (2500);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}
else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
xtemp = DallasTemperature::toFahrenheit(tempC);
lcd.setCursor (8,0);
lcd.print(xtemp);
delay(2000);
}
//
void loop () {
lcd.home (); // go home
lcd.clear();
lcd.print(" Output");
lcd.setCursor (8,0);
lcd.print(xtemp);
// xtemp = 9812;
String thisString = itoa(xtemp, buf, 10);
Serial.println(thisString);
int lgth = thisString.length();
Serial.println(thisString.length());
Serial.println(lgth);
// Serial.println(itoa(xtemp, buf, 10));
Serial.println("start");
for (int i=0; i <= lgth-1; i++){
int say = (thisString.substring(i,i+1)).toInt();
Serial.println(thisString.substring(i,i+1));
Serial.print("saying ");
Serial.println(say);
if (say == 0 ){
mp3_play(10); //zero
}
else{
mp3_play(say);
}
delay(1200);
lcd.setCursor(0,1);
}
mp3_play(13); //degrees
delay (1200);
mp3_play(14); //fahrenheit
delay (1200);
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
|
Working well - delays after talking stops till busy pin clears--1-1-15 DFPlayer_MP3_2lineLCD_18b20_Ver3_3 /*
Working well from 5 volt supply - still have to work on how the files play but it
works rather well
d. bodnar
12-28-14
TODO:
add pot for volume
add button for start
remove first play (does zero)
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int value = 0; //
float xtemp=0;
int potPin = A3; // select the input pin for the potentiometer
int potValue = 0; // variable to store the value coming from the pot
byte buffer[10];
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char buf[12];
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
//int potPin = A3; // select the input pin for the potentiometer
//int potValue = 0; // variable to store the value coming from the pot
int buusyPin = 10;// buusyPin = 10; // sound player busy
int bsy = 0;
int z=0;
//
void setup () {
pinMode(buusyPin, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin (9600);
// 30 good for unpowered speaker - requires power off to reset volume
/* // Serial.println(" mp3-test");
// char buf[12]; // "-2147483648\0"
xtemp = 9812;
String thisString = itoa(xtemp, buf, 10);
Serial.println(thisString);
int lgth = thisString.length();
Serial.println(thisString.length());
Serial.println(lgth);
Serial.println(itoa(xtemp, buf, 10));
Serial.println("start");
for (int i=0; i <= lgth; i++){
Serial.println(thisString.substring(i,i+1));
}
*/
lcd.begin (16,2); // <<----- My LCD was 16x2
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" MP3 Player");
// delay(1000);
lcd.setCursor(0,1);
lcd.print(" Version 3.3 ");
delay ( 500);
lcd.home (); // go home
lcd.clear();
lcd.print("trainelectronics");
delay(100);
lcd.setCursor(0,1);
lcd.print(" 2015 d. bodnar");
delay ( 500);
// Start up the library
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_reset();
delay (400);
mp3_set_volume (10); // 15 is low for unpowered speaker
delay (400);
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}
else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
xtemp = DallasTemperature::toFahrenheit(tempC);
lcd.setCursor (8,0);
lcd.print(xtemp);
delay(2000);
}
//
void loop () {
potValue = analogRead(potPin);
Serial.print("pot= ");
Serial.println(potValue);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
lcd.home (); // go home
lcd.clear();
lcd.print(" Output");
lcd.setCursor (8,0);
lcd.print(xtemp);
// xtemp = 9812;
String thisString = itoa(xtemp, buf, 10);
Serial.println(thisString);
int lgth = thisString.length();
Serial.println(thisString.length());
Serial.println(lgth);
// Serial.println(itoa(xtemp, buf, 10));
Serial.println("start");
for (int i=0; i <= lgth-1; i++){
int say = (thisString.substring(i,i+1)).toInt();
Serial.println(thisString.substring(i,i+1));
Serial.print("saying ");
Serial.println(say);
if (say == 0 ){
mp3_play(10); //zero
}
else{
mp3_play(say);
}
dlayPrint();// delay(1200);
lcd.setCursor(0,1);
}
mp3_play(13); //degrees
dlayPrint();
//delay (1200);
mp3_play(14); //fahrenheit
dlayPrint();//delay (1200);
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// routine to stay here till busy pin goes low once then goes high after speach item completes
void dlayPrint()
{
int bsyflag=0;
for( z=0; z<=300; z++){
bsy = digitalRead(buusyPin);
Serial.print("buusypin ");
Serial.println(bsy);
delay(20);
if (bsyflag==1 && bsy==1){
break;
}
if(bsy==0){
bsyflag=1;
}
}
Serial.println("done");
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
|
Working with decimal degrees F being spoken DFPlayer_MP3_2lineLCD_18b20_Lasers_Ver4_4_F_decimal /*
d. bodnar
1-5-15
WORKING with decimal speaking of F temperature
Hardware wired and tested for laser & phototransistor sensors (2) on fasttrack for HO
TODO:
1. write & test routines to do axle count (only with sensor that first "sees" an axle)
2. consider flashing lasers to make them less visible
3. write & test routine to determine speed
4. write & test routine to determine length (first wheel to last, not body to body)
5. trigger audio report after train passes
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int value = 0; //
float xtemp=0;
int potPin = A3; // select the input pin for the potentiometer
int potValue = 0; // variable to store the value coming from the pot
byte buffer[10];
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char buf[12];
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int buusyPin = 10;// buusyPin = 10; // sound player busy
int bsy = 0;
int z=0;
int PTState;
int Laser = 11; // laser control
int PhotoTransistor = 9;
int PTState2;
int Laser2 = 12; // laser control
int PhotoTransistor2 = 8;
int xCount =0;
long tempF=0;
char tempFString(12);
String thisString(12);
//
void setup () {
pinMode(Laser, OUTPUT);
pinMode(PhotoTransistor, INPUT);
pinMode(Laser2, OUTPUT);
pinMode(PhotoTransistor2, INPUT);
pinMode(buusyPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin (9600);
// 30 good for unpowered speaker - requires power off to reset volume
lcd.begin (16,2); // <<----- My LCD was 16x2
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);// Switch on the backlight
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" MP3 Player");
delay(200);
lcd.setCursor(0,1);
lcd.print(" Version 4.4c ");
delay ( 500);
lcd.home (); // go home
lcd.clear();
lcd.print("trainelectronics");
delay(100);
lcd.setCursor(0,1);
lcd.print(" 2015 d. bodnar");
delay ( 500);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3_reset();
delay (400);
mp3_set_volume (30); // 15 is low for unpowered speaker
delay (400);
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
for(int i=0;i<numberOfDevices; i++)
{
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}
else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
sensors.requestTemperatures(); // Send the command to get temperatures
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
void loop () {
xCount = ++xCount;
digitalWrite(Laser, HIGH);
delay(10); // wait for a bit
PTState = digitalRead(PhotoTransistor);
Serial.print (xCount);
Serial.print(" ");
Serial.print(PTState);
Serial.print(" ");
digitalWrite(Laser2, HIGH);
delay(10); // wait for a bit
PTState2 = digitalRead(PhotoTransistor2);
Serial.println(PTState2);
/*
if(PTState == 1 || PTState2 ==1){
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite (ledPin, LOW);
}
*/
if(PTState == 1 && PTState2 ==1){
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite (ledPin, LOW);
}
potValue = analogRead(potPin);
Serial.print("pot= ");
Serial.println(potValue);
// delay(2000);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
//// digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
//// digitalWrite(ledPin, LOW);
}
lcd.home (); // go home
lcd.clear();
lcd.print(" Output");
lcd.setCursor (8,0);
lcd.print(xtemp);
// String thisString =String tempFString;
// String thisString = itoa(xtemp, buf, 10);
// convertFahrenheittoString();
Serial.print(" From Convert Routine Temp F: ");
Serial.println(xtemp); // Converts tempC to Fahrenheit
int xtemp2 = xtemp * 100;
int xtempWhole = xtemp2 / 100;
int xtempDecimal = xtemp2 - (xtempWhole * 100);
Serial.print(" AGAIN.....From Convert Routine Temp F: ");
Serial.print(xtempWhole); // Converts tempC to Fahrenheit
Serial.print(".");
Serial.println(xtempDecimal);
String tempFStringW = itoa((xtempWhole), buf, 10);
String tempFStringD = itoa((xtempDecimal), buf, 10);
String thisString = tempFStringW + "." + tempFStringD;
Serial.print(" ONCE AGAIN.....From Convert Routine Temp F: ");
Serial.println(thisString); // Converts tempC to Fahrenheit
Serial.println(thisString);
int lgth = thisString.length();
Serial.println(thisString.length());
Serial.println(lgth);
Serial.println("start");
if(buttonState==0){
Serial.println("SKIPPING!!!");
// goto SkipSpeaking;
}
else {
for (int i=0; i <= lgth-1; i++){
int say = (thisString.substring(i,i+1)).toInt();
Serial.println(thisString.substring(i,i+1));
String isDecPoint=(thisString.substring(i,i+1));
Serial.print("saying ");
Serial.print(isDecPoint );
//if (isDecPoint=="."){
// Serial.println("FOUND ONE ...... THAT IS!");
// }
//Serial.print(" ");
//Serial.println(say);
delay(100); // needed to prevent audio "pops" at start
if(isDecPoint=="."){
Serial.println("FOUND SECOND ONE ...... THAT IS!");
delay(200);
mp3_play(12);
delay(200);
}
else if (say == 0 ){
mp3_play(10); //zero
}
/*else if(isDecPoint=="."){
Serial.println("FOUND SECOND ONE ...... THAT IS!");
mp3_play(12);
}
*/
else{
mp3_play(say);
}
dlayPrint();// delay(1200);
lcd.setCursor(0,1);
}
mp3_play(13); //degrees
dlayPrint();
mp3_play(14); //fahrenheit
dlayPrint();//delay (1200);
}
//SkipSpeaking:
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// routine to stay here till busy pin goes low once then goes high after speach item completes
void dlayPrint()
{
int bsyflag=0;
Serial.println(" ");
Serial.print("buusypin ");
for( z=0; z<=300; z++){
bsy = digitalRead(buusyPin);
Serial.print(bsy);
delay(20);
if (bsyflag==1 && bsy==1){
break;
}
if(bsy==0){
bsyflag=1;
}
}
Serial.println(" ");
Serial.println("done");
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
xtemp = DallasTemperature::toFahrenheit(tempC);
lcd.setCursor (8,0);
lcd.print(xtemp);
//delay(2000);
convertFahrenheittoString();
}
void convertFahrenheittoString()
{
Serial.print(" From Convert Routine Temp F: ");
Serial.println(xtemp); // Converts tempC to Fahrenheit
int xtemp2 = xtemp * 100;
int xtempWhole = xtemp2 / 100;
int xtempDecimal = xtemp2 - (xtempWhole * 100);
Serial.print(" AGAIN.....From Convert Routine Temp F: ");
Serial.print(xtempWhole); // Converts tempC to Fahrenheit
Serial.print(".");
Serial.println(xtempDecimal);
String tempFStringW = itoa((xtempWhole), buf, 10);
String tempFStringD = itoa((xtempDecimal), buf, 10);
String thisString = tempFStringW + "." + tempFStringD;
Serial.print(" ONCE AGAIN.....From Convert Routine Temp F: ");
Serial.println(thisString); // Converts tempC to Fahrenheit
}
//String thisString = itoa(xtemp, buf, 10);
|