Is my code layout correct?

//=================================================
// Proyek Sensor Parkir PJBL Kelompok Abbyan
//=================================================
// Library yang diperlukan
//=================================================
#include <Wire.h>
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobotDFPlayerMini.h>

//=================================================
// Pengenalan Pin dan Jarak
//=================================================
#define TRIGGER 3
#define ECHO 4
#define MAXDISTANCE 250

//=================================================
// Inisialisasi Objek
//=================================================
DFRobotDFPlayerMini myDFPlayer;
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySoftwareSerial(10, 11);
NewPing sonar(TRIGGER, ECHO, MAXDISTANCE);

//=================================================
// Definisi Pin
//=================================================
const int Red = 5;
const int Yellow = 6;
const int Green = 7;
const int Buzzer = 8;

//=================================================
// Variabel Jarak
//=================================================
int jarak = 0;
int distance;

//=================================================
// Variabel dan Konstanta Pengaturan Buzzer
//=================================================
int BuzzerState = LOW;
const long interval = 200;
const long interval2 = 400;
const long interval3 = 800;
const long interval4 = 2000;
unsigned long previousMillis = 0;

//=================================================
// Setup
//=================================================
void setup() {
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) {
}
lcd.begin(16, 2);
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Buzzer, OUTPUT);
Serial.begin(9600);
}

//=================================================
// Loop Utama
//=================================================
void loop() {
distance = sonar.ping_cm();
jarak = distance;

if (jarak <= 0) {
jarak = 0;
LCDkosong();
buzzerAman();
stopWarning();
} else if (jarak >= 1 && jarak <= 5) {
LCDnabrak();
VbuzzerNabrak();
stopWarningNabrak();
} else if (jarak >= 6 && jarak <= 12) {
LCDawas();
buzzerTipis();
VbuzzerAwas();
stopWarningAwas();
} else if (jarak >= 13 && jarak <= 22) {
LCDpelan();
buzzerDekat();
VbuzzerPelan();
stopWarningPelan();
} else if (jarak >= 23 && jarak <= 50) {
LCDaman();
buzzerJauh();
VbuzzerAman();
stopWarningAman();
}
delay(50);
updateLCDStatus();
}

//=================================================
// Fungsi Pembaruan Status LCD
//=================================================
void updateLCDStatus() {
lcd.setCursor(0, 0);
lcd.print("Jarak : ");
lcd.print(sonar.ping_cm());
lcd.print(" cm ");
lcd.setCursor(0, 1);
lcd.print("Status : ");
}

//=================================================
// Fungsi Status LCD Kosong
//=================================================
void LCDkosong() {
lcd.setCursor(9, 1);
lcd.print("KOSONG! ");
}

//=================================================
// Fungsi Status LCD Nabrak
//=================================================
void LCDnabrak() {
lcd.setCursor(9, 1);
lcd.print("NABRAK! ");
}

//=================================================
// Fungsi Status LCD Awas
//=================================================
void LCDawas() {
lcd.setCursor(9, 1);
lcd.print("AWAS! ");
}

//=================================================
// Fungsi Status LCD Pelan
//=================================================
void LCDpelan() {
lcd.setCursor(9, 1);
lcd.print("PELAN! ");
}

//=================================================
// Fungsi Status LCD Aman
//=================================================
void LCDaman() {
lcd.setCursor(9, 1);
lcd.print("AMAN! ");
}

//=================================================
// Fungsi Peringatan Kosong
//=================================================
void stopWarning() {
digitalWrite(Buzzer, LOW);
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Nabrak
//=================================================
void stopWarningNabrak() {
digitalWrite(Buzzer, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, HIGH);
}

//=================================================
// Fungsi Peringatan Awas
//=================================================
void stopWarningAwas() {
digitalWrite(Red, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Pelan
//=================================================
void stopWarningPelan() {
digitalWrite(Red, LOW);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Aman
//=================================================
void stopWarningAman() {
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, HIGH);
}

//=================================================
// Fungsi Peringatan Suara Nabrak
//=================================================
void VbuzzerNabrak() {
myDFPlayer.volume(30);
myDFPlayer.play(1);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Awas
//=================================================
void VbuzzerAwas() {
myDFPlayer.volume(30);
myDFPlayer.play(2);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Pelan
//=================================================
void VbuzzerPelan() {
myDFPlayer.volume(30);
myDFPlayer.play(3);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Aman
//=================================================
void VbuzzerAman() {
myDFPlayer.volume(30);
myDFPlayer.play(4);
delay(1000);
}

//=================================================
// Fungsi Buzzer Tipis
//=================================================
void buzzerTipis() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Dekat
//=================================================
void buzzerDekat() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval2){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Jauh
//=================================================
void buzzerJauh() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval3){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Aman
//=================================================
void buzzerAman() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval4){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Terimakasih Banyak!
//=================================================

To get the best support, apart from reading the forum guidelines for posting….

In the IDE, press CTRL+T to auto format the code, then copy and paste all the code into ‘CODE TAGS’ so it retains the formatting, and is easy for helpers to use in their own systems for assistance,

Generally, it looks ok, but you don’t need the ‘empty’ // comment lines - they just add scrolling through long blocks of code.

Have you compiled it yet ? Any errors ?
Which Arduino are you using? What and how are your other devices connected ?

3 Likes

This verifies something happened, but does nothing.

1 Like

I guess it would help most of us if your comments were also in English? please?

The loop code looks like a good fit for a switch-case structure.

https://forum.arduino.cc/t/beginners-using-the-switch-case-statement/680177/9

1 Like

this can be done just once at the top of loop() with currentMillis made global

these routines: buzzerTipis (), buzzerDekat (), buzzerJauh () and buzzerAman () could all be replaced by a single function with the interval as an argument. same for other functions (e.g. VbusserAwas()) where some variable can be passed as an argument


//=================================================
// Proyek Sensor Parkir PJBL Kelompok Abbyan
//=================================================
// Library yang diperlukan
//=================================================
#include <Wire.h>
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobotDFPlayerMini.h>

//=================================================
// Pengenalan Pin dan Jarak
//=================================================
#define TRIGGER 3
#define ECHO 4
#define MAXDISTANCE 250

//=================================================
// Inisialisasi Objek
//=================================================
DFRobotDFPlayerMini myDFPlayer;
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySoftwareSerial(10, 11);
NewPing sonar(TRIGGER, ECHO, MAXDISTANCE);

//=================================================
// Definisi Pin
//=================================================
const int Red = 5;
const int Yellow = 6;
const int Green = 7;
const int Buzzer = 8;

//=================================================
// Variabel Jarak
//=================================================
int jarak = 0;
int distance;

//=================================================
// Variabel dan Konstanta Pengaturan Buzzer
//=================================================
int BuzzerState = LOW;
const long interval = 200;
const long interval2 = 400;
const long interval3 = 800;
const long interval4 = 2000;
unsigned long previousMillis = 0;

//=================================================
// Setup
//=================================================
void setup() {
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) {
}
lcd.begin(16, 2);
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Buzzer, OUTPUT);
Serial.begin(9600);
}

//=================================================
// Loop Utama
//=================================================
void loop() {
distance = sonar.ping_cm();
jarak = distance;

if (jarak <= 0) {
jarak = 0;
LCDkosong();
buzzerAman();
stopWarning();
} else if (jarak >= 1 && jarak <= 5) {
LCDnabrak();
VbuzzerNabrak();
stopWarningNabrak();
} else if (jarak >= 6 && jarak <= 12) {
LCDawas();
buzzerTipis();
VbuzzerAwas();
stopWarningAwas();
} else if (jarak >= 13 && jarak <= 22) {
LCDpelan();
buzzerDekat();
VbuzzerPelan();
stopWarningPelan();
} else if (jarak >= 23 && jarak <= 50) {
LCDaman();
buzzerJauh();
VbuzzerAman();
stopWarningAman();
}
delay(50);
updateLCDStatus();
}

//=================================================
// Fungsi Pembaruan Status LCD
//=================================================
void updateLCDStatus() {
lcd.setCursor(0, 0);
lcd.print("Jarak : ");
lcd.print(sonar.ping_cm());
lcd.print(" cm ");
lcd.setCursor(0, 1);
lcd.print("Status : ");
}

//=================================================
// Fungsi Status LCD Kosong
//=================================================
void LCDkosong() {
lcd.setCursor(9, 1);
lcd.print("KOSONG! ");
}

//=================================================
// Fungsi Status LCD Nabrak
//=================================================
void LCDnabrak() {
lcd.setCursor(9, 1);
lcd.print("NABRAK! ");
}

//=================================================
// Fungsi Status LCD Awas
//=================================================
void LCDawas() {
lcd.setCursor(9, 1);
lcd.print("AWAS! ");
}

//=================================================
// Fungsi Status LCD Pelan
//=================================================
void LCDpelan() {
lcd.setCursor(9, 1);
lcd.print("PELAN! ");
}

//=================================================
// Fungsi Status LCD Aman
//=================================================
void LCDaman() {
lcd.setCursor(9, 1);
lcd.print("AMAN! ");
}

//=================================================
// Fungsi Peringatan Kosong
//=================================================
void stopWarning() {
digitalWrite(Buzzer, LOW);
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Nabrak
//=================================================
void stopWarningNabrak() {
digitalWrite(Buzzer, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, HIGH);
}

//=================================================
// Fungsi Peringatan Awas
//=================================================
void stopWarningAwas() {
digitalWrite(Red, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Pelan
//=================================================
void stopWarningPelan() {
digitalWrite(Red, LOW);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Aman
//=================================================
void stopWarningAman() {
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, HIGH);
}

//=================================================
// Fungsi Peringatan Suara Nabrak
//=================================================
void VbuzzerNabrak() {
myDFPlayer.volume(30);
myDFPlayer.play(1);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Awas
//=================================================
void VbuzzerAwas() {
myDFPlayer.volume(30);
myDFPlayer.play(2);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Pelan
//=================================================
void VbuzzerPelan() {
myDFPlayer.volume(30);
myDFPlayer.play(3);
delay(1000);
}

//=================================================
// Fungsi Peringatan Suara Aman
//=================================================
void VbuzzerAman() {
myDFPlayer.volume(30);
myDFPlayer.play(4);
delay(1000);
}

//=================================================
// Fungsi Buzzer Tipis
//=================================================
void buzzerTipis() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Dekat
//=================================================
void buzzerDekat() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval2){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Jauh
//=================================================
void buzzerJauh() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval3){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Fungsi Buzzer Aman
//=================================================
void buzzerAman() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval4){
previousMillis = currentMillis;
if (BuzzerState == LOW) {
BuzzerState = HIGH;
} else {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}
}

//=================================================
// Terimakasih Banyak!
//=================================================


Here is the code in code tags.’
Can’t format it now…..
No IDE on mobile.

I might translate it later.
I mostly understand the comments.


//=================================================
// Car Parking sensor project?
//=================================================
// Libraries.
//=================================================
#include <Wire.h>
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobotDFPlayerMini.h>

//=================================================
// define pin numbers and distance
//=================================================
#define TRIGGER 3
#define ECHO 4
#define MAXDISTANCE 250

//=================================================
// initialize objects.
//=================================================
DFRobotDFPlayerMini myDFPlayer;
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySoftwareSerial(10, 11);
NewPing sonar(TRIGGER, ECHO, MAXDISTANCE);

//=================================================
// Define pins.
//=================================================
const int Red = 5;
const int Yellow = 6;
const int Green = 7;
const int Buzzer = 8;

//=================================================
// Variables for distance
//=================================================
int jarak = 0; // jarak means distance....
int distance;

//=================================================
// Variables and contants for buzzer
//=================================================
int BuzzerState = LOW;
const long interval = 200;
const long interval2 = 400;
const long interval3 = 800;
const long interval4 = 2000;
unsigned long previousMillis = 0;

//=================================================
// Setup
//=================================================
void setup() {
  mySoftwareSerial.begin(9600);
  if (!myDFPlayer.begin(mySoftwareSerial)) {
  }
  lcd.begin(16, 2);
  pinMode(Red, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  Serial.begin(9600);
}

//=================================================
// Main Loop
//=================================================
void loop() {
  distance = sonar.ping_cm();
  jarak = distance; // oh.....

  if (jarak <= 0) {
    jarak = 0;
    LCDkosong();
    buzzerAman();
    stopWarning();
  } else if (jarak >= 1 && jarak <= 5) {
    LCDnabrak();
    VbuzzerNabrak();
    stopWarningNabrak();
  } else if (jarak >= 6 && jarak <= 12) {
    LCDawas();
    buzzerTipis();
    VbuzzerAwas();
    stopWarningAwas();
  } else if (jarak >= 13 && jarak <= 22) {
    LCDpelan();
    buzzerDekat();
    VbuzzerPelan();
    stopWarningPelan();
  } else if (jarak >= 23 && jarak <= 50) {
    LCDaman();
    buzzerJauh();
    VbuzzerAman();
    stopWarningAman();
  }
  delay(50);
  updateLCDStatus();
}

//=================================================
// Function for updated the  Status of the LCD
//=================================================
void updateLCDStatus() {
  lcd.setCursor(0, 0);
  lcd.print("Jarak : "); // jarak means distance
  lcd.print(sonar.ping_cm());
  lcd.print(" cm ");
  lcd.setCursor(0, 1);
  lcd.print("Status : ");
}

//=================================================
// Function for when there is no car detected? Prints "empty" to LCD
//=================================================
void LCDkosong() {
  lcd.setCursor(9, 1);
  lcd.print("KOSONG! ");// kosong means empty.
}

//=================================================
// Function for when a object is detected? Prints "crash" to LCD
//=================================================
void LCDnabrak() {
  lcd.setCursor(9, 1);
  lcd.print("NABRAK! ");//Nabrak means crash?
}

//=================================================
// Function for Warning? When it is too close? Prints "warning!" to LCD
//=================================================
void LCDawas() {
  lcd.setCursor(9, 1);
  lcd.print("AWAS! "); // awas means warning.
}

//=================================================
// Function for Slow? Prints "Slow!" to LCD
//=================================================
void LCDpelan() {
  lcd.setCursor(9, 1);
  lcd.print("PELAN! ");// pelan means slow
}

//=================================================
//Function for Safe distance?
//=================================================
void LCDaman() {
  lcd.setCursor(9, 1);
  lcd.print("AMAN! ");// aman means safe
}

//=================================================
// Fungsi Peringatan Kosong (Maybe no car warning function? )
//=================================================
void stopWarning() {
  digitalWrite(Buzzer, LOW);
  digitalWrite(Red, LOW);
  digitalWrite(Yellow, LOW);
  digitalWrite(Green, LOW);
}

//=================================================
// Crash warning? 
//=================================================
void stopWarningNabrak() { 
  digitalWrite(Buzzer, HIGH);
  digitalWrite(Red, HIGH);
  digitalWrite(Yellow, HIGH);
  digitalWrite(Green, HIGH);
}

//=================================================
// Fungsi Peringatan Awas
//=================================================
void stopWarningAwas() {
  digitalWrite(Red, HIGH);
  digitalWrite(Yellow, LOW);
  digitalWrite(Green, LOW);
}

//=================================================
// Fungsi Peringatan Pelan
//=================================================
void stopWarningPelan() {
  digitalWrite(Red, LOW);
  digitalWrite(Yellow, HIGH);
  digitalWrite(Green, LOW);
}

//=================================================
// Safe function? Turns green on......
//=================================================
void stopWarningAman() {
  digitalWrite(Red, LOW);
  digitalWrite(Yellow, LOW);
  digitalWrite(Green, HIGH);
}

//=================================================
// Crash sound.
//=================================================
void VbuzzerNabrak() {
  myDFPlayer.volume(30);
  myDFPlayer.play(1);
  delay(1000);
}

//=================================================
// Warning sound.
//=================================================
void VbuzzerAwas() {
  myDFPlayer.volume(30);
  myDFPlayer.play(2);
  delay(1000);
}

//=================================================
// Slow sound.
//=================================================
void VbuzzerPelan() {
  myDFPlayer.volume(30);
  myDFPlayer.play(3);
  delay(1000);
}

//=================================================
// Safe sound.
//=================================================
void VbuzzerAman() {
  myDFPlayer.volume(30);
  myDFPlayer.play(4);
  delay(1000);
}

//=================================================
// Fungsi Buzzer Tipis(Unclear what this means.....)
//=================================================
void buzzerTipis() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (BuzzerState == LOW) {
      BuzzerState = HIGH;
    } else {
      BuzzerState = LOW;
    }
    digitalWrite(Buzzer, BuzzerState);
  }
}

//=================================================
// Function when near, do something with buzzer.
//=================================================
void buzzerDekat() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval2) {
    previousMillis = currentMillis;
    if (BuzzerState == LOW) {
      BuzzerState = HIGH;
    } else {
      BuzzerState = LOW;
    }
    digitalWrite(Buzzer, BuzzerState);
  }
}

//=================================================
// Function for when far, do something with buzzer.
//=================================================
void buzzerJauh() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval3) {
    previousMillis = currentMillis;
    if (BuzzerState == LOW) {
      BuzzerState = HIGH;
    } else {
      BuzzerState = LOW;
    }
    digitalWrite(Buzzer, BuzzerState);
  }
}

//=================================================
// Function for when safe, do something with buzzer. Um....THis code is really similar.
//=================================================
void buzzerAman() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval4) {
    previousMillis = currentMillis;
    if (BuzzerState == LOW) {
      BuzzerState = HIGH;
    } else {
      BuzzerState = LOW;
    }
    digitalWrite(Buzzer, BuzzerState);
  }
}

//=================================================
// Thank you a lot!


//=================================================

I have translated the comments(not with a translate service…..)

1 Like

Thanks for taking the trouble. Now readable, even though my only comment to the OP is that I like the section headings. Wish my sketches looked as well organised.

P.S: As I have a Nano and DFR player at hand I'd like to have tried the four distance-related tracks and also hear from the OP how the project is performing.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.