program ECOBIN

 #include "Arduino.h"

#include "SoftwareSerial.h"

#include "DFRobotDFPlayerMini.h"


#define TRIG_PIN 3

#define ECHO_PIN 2

#define DISTANCE_THRESHOLD 50

#define DELAY_TIME 20000


#define SENSOR1_PIN 13  // Sensor E18-D80NK untuk musik 2

#define SENSOR2_PIN 12  // Sensor E18-D80NK untuk musik 3

#define SENSOR3_PIN 8   // Sensor E18-D80NK untuk musik 4


#define BUTTON1_PIN A0  // Push button untuk musik 6

#define BUTTON2_PIN A1  // Push button untuk musik 3

#define BUTTON3_PIN A2  // Push button untuk musik 5


SoftwareSerial mySoftwareSerial(10, 11);

DFRobotDFPlayerMini myDFPlayer;


bool objectNear = false;

unsigned long lastPlayTime = 0;

int lastButtonPressed = 0;

int kosong = 0;

void setup() {

  pinMode(TRIG_PIN, OUTPUT);

  pinMode(ECHO_PIN, INPUT);


  pinMode(SENSOR1_PIN, INPUT_PULLUP);

  pinMode(SENSOR2_PIN, INPUT_PULLUP);

  pinMode(SENSOR3_PIN, INPUT_PULLUP);


  pinMode(BUTTON1_PIN, INPUT_PULLUP);

  pinMode(BUTTON2_PIN, INPUT_PULLUP);

  pinMode(BUTTON3_PIN, INPUT_PULLUP);


  mySoftwareSerial.begin(9600);

  if (!myDFPlayer.begin(mySoftwareSerial)) while (true);

  myDFPlayer.volume(30);

}


void loop() {

  int distance = getDistance();


  // Sensor ultrasonik mendeteksi jarak <= 30 cm

  if (distance <= DISTANCE_THRESHOLD && !objectNear && (millis() - lastPlayTime >= DELAY_TIME)) {

    myDFPlayer.play(8);  // Memutar musik no 1

    objectNear = true;

    lastPlayTime = millis();

    lastButtonPressed = 0;  // Reset status tombol terakhir

  }


  // Cek tombol yang ditekan

  if (digitalRead(BUTTON1_PIN) == LOW) {

    myDFPlayer.play(6);  // Memutar musik no 6

    lastButtonPressed = 1;

    kosong= 1;

    delay(1000);  // Debounce sederhana

  } else if (digitalRead(BUTTON2_PIN) == LOW) {

    myDFPlayer.play(3);  // Memutar musik no 3

    lastButtonPressed = 2;

    kosong= 1;

    delay(1000);

  } else if (digitalRead(BUTTON3_PIN) == LOW) {

    myDFPlayer.play(5);  // Memutar musik no 5

    lastButtonPressed = 3;

    kosong= 1;

    delay(1000);

  }


  // Periksa kombinasi tombol dan sensor

  if (lastButtonPressed > 0) {

    if (digitalRead(SENSOR1_PIN) == LOW && lastButtonPressed == 1) {

      myDFPlayer.play(2);  // Memutar musik no 2

       lastButtonPressed = 0;

       kosong= 0;

      delay(9000);         // Debounce sederhana

     

    } else if (digitalRead(SENSOR2_PIN) == LOW && lastButtonPressed == 2) {

      myDFPlayer.play(2);  // Memutar musik no 2

       lastButtonPressed = 0;

       kosong= 0;

      delay(9000); 

    } else if (digitalRead(SENSOR3_PIN) == LOW && lastButtonPressed == 3) {

      myDFPlayer.play(2);  // Memutar musik no 2

       lastButtonPressed = 0;

       kosong= 0;

      delay(9000); 

    } 

    // jika salah

    else if (digitalRead(SENSOR1_PIN) == LOW && lastButtonPressed == 2) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000);         // Debounce sederhana

      

    } else if (digitalRead(SENSOR1_PIN) == LOW && lastButtonPressed == 3) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000); 

    } else if (digitalRead(SENSOR2_PIN) == LOW && lastButtonPressed == 1) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000); 

    } else if (digitalRead(SENSOR2_PIN) == LOW && lastButtonPressed == 3) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000); 

    } else if (digitalRead(SENSOR3_PIN) == LOW && lastButtonPressed == 1) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000); 

    } else if (digitalRead(SENSOR3_PIN) == LOW && lastButtonPressed == 2) {

      myDFPlayer.play(4);  // Memutar musik no 2

      lastButtonPressed = 0;

      kosong= 0;

      delay(9000); 

    } 


    

  }


  // Periksa jika salah satu sensor langsung aktif

  

   

  if (digitalRead(SENSOR1_PIN) == LOW || digitalRead(SENSOR2_PIN) == LOW || digitalRead(SENSOR3_PIN) == LOW && kosong==0) {

    myDFPlayer.play(7);  // Memutar musik no 7

    delay(9000);          // Debounce sederhana

  }

  

  

  


  // Jika jarak lebih dari threshold

  if (distance > DISTANCE_THRESHOLD) {

    objectNear = false;

  }


  delay(100);

}


// Sub fungsi untuk mendapatkan jarak dari sensor ultrasonik

int getDistance() {

  digitalWrite(TRIG_PIN, LOW);

  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);


  long duration = pulseIn(ECHO_PIN, HIGH);

  return duration * 0.034 / 2; // Konversi durasi ke jarak (cm)

}


Komentar

Postingan Populer