Friday, May 25, 2018

ARDUINO - DFPLAYER WITH DS3231

#include <SPI.h>                   
#include <Wire.h>
#include <DS3231.h>
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Button.h>
DS3231  rtc(SDA, SCL);
Time  t;
uint8_t hh = 0, mm = 0, ss = 0;
SoftwareSerial mySerial(11, 10); // Declare pin RX & TX

#define BUTTON8 8 // tombol ON
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 20
Button btn1(BUTTON8, PULLUP, INVERT, DEBOUNCE_MS);

int b1 = 3; //tombol on/off
const int busyPin = 5; //Dfplayer -busypin will be HIGH if mp3 stop play
int busyStatus =0;
int led = 13;
void setup() {
  Serial.begin(57600);
  pinMode (busyPin, INPUT);
  pinMode(b1, INPUT_PULLUP);
  rtc.begin();
  mySerial.begin (9600);
  mp3_set_serial (mySerial); //set softwareSerial for DFPlayer
  delay(10);
  mp3_reset();
  delay(10);   //wait 1ms for respon command
  mp3_set_volume (30); //Set volume value. From 0 to 30
  delay(500);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  //rtc.setTime(8, 38, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(25, 5, 2018);   // Set the date to January 1st, 2014
}


void loop() {

  t = rtc.getTime();  // gate all data time & date from RTC
  hh = t.hour, DEC; //pengambilan data jam
  mm = t.min, DEC; //pengambilan data menit
  ss = t.sec, DEC; //pengambilan data detik
 
    Serial.print(hh);
    Serial.print(":");
    Serial.print(mm);
    Serial.print(":");
    Serial.println(ss);
    delay(1000);
  Serial.println(digitalRead(busyPin));

  if (btn1.read() == 1 ) { // nyalain mp3
    digitalWrite(led, HIGH);
    mp3_play(1);
    delay(1000);
    digitalWrite(led, LOW);
    //delay(56000);
 }



  if (hh >= 6 && hh <= 22 ) {
    busyStatus = digitalRead(busyPin);
    if ( busyStatus == HIGH ) {
      mp3_play(1);
      digitalWrite(led, HIGH);
    }
  }
 
  else {
    mp3_stop();
    digitalWrite(led, LOW);
  }
 

}