What's wrong with this coding

#include <LiquidCrystal.h>

// Inisialisasi LCD dengan pin yang digunakan
LiquidCrystal lcd(6, 7, 8, 9, 10, 11);

// Pin sensor dan output
const int coin10Pin = 2;
const int coin20Pin = 3;
const int coin50Pin = 4;
const int colorSensorPin = A0; // Sensor warna
const int buzzerPin = 5;
const int resetButtonPin = 12;

// Pembolehubah untuk jumlah
float totalAmount = 0.0;

// Fungsi untuk bunyikan buzzer
void beepBuzzer() {
tone(buzzerPin, 1000, 200); // Bunyi buzzer pada frekuensi 1000 Hz selama 200 ms
}

// Fungsi untuk tambah nilai berdasarkan syiling yang dikesan
void checkCoin(int coinPin, float coinValue) {
if (digitalRead(coinPin) == LOW) { // Syiling dikesan
totalAmount += coinValue; // Tambah nilai syiling pada jumlah
beepBuzzer(); // Bunyi buzzer
delay(200); // Debounce
while (digitalRead(coinPin) == LOW); // Tunggu sehingga dilepaskan
}
}

// Fungsi untuk mengesan duit kertas berdasarkan warna
void checkColorSensor() {
int colorValue = analogRead(colorSensorPin); // Baca nilai dari sensor warna

if (colorValue < 300) { // Contoh: Nilai untuk warna biru (RM1)
totalAmount += 1.00;
beepBuzzer();
delay(200);
} else if (colorValue >= 300 && colorValue < 600) { // Contoh: Nilai untuk warna hijau (RM5)
totalAmount += 5.00;
beepBuzzer();
delay(200);
} else if (colorValue >= 600) { // Contoh: Nilai untuk warna merah (RM10)
totalAmount += 10.00;
beepBuzzer();
delay(200);
}
}

// Fungsi untuk memaparkan jumlah terkini pada LCD
void displayTotalAmount() {
lcd.setCursor(0, 1);
lcd.print("Jumlah: RM ");
lcd.print(totalAmount, 2); // Papar jumlah dengan dua tempat perpuluhan
lcd.print(" "); // Pastikan baris bersih dari sisa teks lama
}

// Fungsi untuk reset jumlah jika butang ditekan
void checkResetButton() {
if (digitalRead(resetButtonPin) == LOW) { // Jika butang reset ditekan
totalAmount = 0; // Tetapkan jumlah semula ke 0
displayTotalAmount(); // Paparkan jumlah yang telah ditetapkan semula
delay(500); // Debounce untuk elakkan reset berulang
}
}

void setup() {
// Tetapkan mod pin
pinMode(coin10Pin, INPUT_PULLUP);
pinMode(coin20Pin, INPUT_PULLUP);
pinMode(coin50Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(resetButtonPin, INPUT_PULLUP);

// Tetapkan LCD
lcd.begin(16, 2); // Mulakan LCD dengan 16 lajur dan 2 baris
lcd.print("Jumlah: RM 0.00"); // Paparkan jumlah permulaan
}

void loop() {
// Semak syiling
checkCoin(coin10Pin, 0.10);
checkCoin(coin20Pin, 0.20);
checkCoin(coin50Pin, 0.50);

// Semak duit kertas berdasarkan warna
checkColorSensor();

// Paparkan jumlah pada LCD
displayTotalAmount();

// Semak jika butang reset ditekan
checkResetButton();
}

Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

What happens when you upload the code ?

If you get any errors please post them in full, using code tags when you do

Do you see this button on your screen
image

Use it to copy the full error messages then post them here in code tags

As it happens I can see the problem in your screenshot, or at least one problem

You don't have an opening brace for your code in the setup() function

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

where should i put the opening brace

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Uno"

6:24:3: error: expected initializer before 'pinMode'

pinMode(coin10Pin, INPUT_PULLUP);

^~~~~~~

6:25:10: error: expected constructor, destructor, or type conversion before '(' token

pinMode(coin20Pin, INPUT_PULLUP);

      ^

6:26:10: error: expected constructor, destructor, or type conversion before '(' token

pinMode(coin50Pin, INPUT_PULLUP);

      ^

6:27:10: error: expected constructor, destructor, or type conversion before '(' token

pinMode(buzzerPin, OUTPUT);

      ^

6:28:10: error: expected constructor, destructor, or type conversion before '(' token

pinMode(resetButtonPin, INPUT_PULLUP);

      ^

6:31:3: error: 'lcd' does not name a type

lcd.begin(16, 2);

^~~

6:32:3: error: 'lcd' does not name a type

lcd.print("Jumlah: RM 0.00");

^~~

6:33:1: error: expected declaration before '}' token

}

^

exit status 1

expected initializer before 'pinMode'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Before the code in the setup() function

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Uno"

6:21:1: error: expected unqualified-id before '{' token

{ void setup()

^

C:\Users\user\OneDrive\Documents\Arduino\6\6.ino: In function 'void loop()':

6:70:6: error: redefinition of 'void loop()'

void loop() {

  ^~~~

C:\Users\user\OneDrive\Documents\Arduino\6\6.ino:34:6: note: 'void loop()' previously defined here

void loop() {

  ^~~~

exit status 1

expected unqualified-id before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

still can't

Can't what ?

still can't

Please post your sketch in full as it is now in code tags

Please post the full error message in code tags

#include <LiquidCrystal.h>

// Define pin numbers
const int coin10Pin = 2; // Pin for 10 cent coin
const int coin20Pin = 3; // Pin for 20 cent coin
const int coin50Pin = 4; // Pin for 50 cent coin
const int buzzerPin = 5; // Pin for buzzer
const int resetButtonPin = 6; // Pin for reset button

// Create LCD object
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Adjust pins according to your setup

// Variables to keep track of total amount
float totalAmount = 0.0;

void setup()
{
// Set pin modes
pinMode(coin10Pin, INPUT_PULLUP);
pinMode(coin20Pin, INPUT_PULLUP);
pinMode(coin50Pin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(resetButtonPin, INPUT_PULLUP);

// Initialize LCD
lcd.begin(16, 2);
lcd.print("Jumlah: RM 0.00");
}

void loop() {
// Check for coins
checkCoin(coin10Pin, 0.10);
checkCoin(coin20Pin, 0.20);
checkCoin(coin50Pin, 0.50);

// Check for reset button
if (digitalRead(resetButtonPin) == LOW) {
resetCounter();
}

// Small delay to prevent bouncing
delay(100);
}

void checkCoin(int pin, float value) {
static unsigned long lastPressTime = 0; // For debounce

if (digitalRead(pin) == LOW) {
unsigned long currentTime = millis();
if (currentTime - lastPressTime >= 200) { // Debounce time
totalAmount += value;
lcd.clear();
lcd.print("Jumlah: RM ");
lcd.print(totalAmount, 2); // Display amount with 2 decimal places
tone(buzzerPin, 1000, 200); // Sound buzzer
lastPressTime = currentTime; // Update last press time
}
}
}

void resetCounter() {
totalAmount = 0.0;
lcd.clear();
lcd.print("Jumlah: RM 0.00");

Software

  • For readability, place { and } on separate lines by themselves, place each line of code on a separate line.
  • In the Arduino IDE, use Ctrl T or CMD T to format your code, then copy the complete sketch.
  • Use the < CODE / > icon from the ‘posting menu’ to attach your copied sketch.

That is not the complete sketch and you did not use code tags anyway

Follow these instructions to post the code

The easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hi, @mirul_hfz389
Welcome to the forum.

Please take the time to read the above link.
It will show you how to post your code in a posting which is easily readable.

Thanks.. Tom... :smiley: :+1: :coffee: :australia: