Hi!
First, why tinkercad didn't explain anything on error page? It only says:
Sorry, it seems like your code has some errors.
Invalid Header Error
It appears after I want to program some LED. Please check this following code with (NEW) label.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//define the LED (NEW)
const int ledG = 2;
const int ledR = 3;
const int ledY = 4;
// Define the LCD I2C address and dimensions
const int lcdAddress = 0x20;
const int lcdColumns = 16;
const int lcdRows = 2;
// Create the LCD instance
LiquidCrystal_I2C lcd(lcdAddress, lcdColumns, lcdRows);
// Names and PINs of the participants
const char* names[] = {"Alif", "Darel", "Gilang", "Jeli", "Valdez"};
const char* pins[] = {"1111", "2222", "3333", "4444", "5555"};
const int numParticipants = sizeof(names) / sizeof(names[0]);
// State variables
int currentParticipantIndex = 0;
bool galonInstalled = false;
bool lcdIdle = true; // Flag to indicate if LCD is in idle state
// Keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize the LCD
lcd.init();
lcd.begin(lcdColumns, lcdRows);
lcd.backlight();
//Initialize led (NEW)
pinMode(ledG, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledY, OUTPUT);
// Write the initial message on the LCD
displayIdleMessage();
}
void loop() {
if (lcdIdle) {
char key = keypad.getKey();
if (key == 'A') {
handleButtonAPress();
delay(500); // Add a small delay for button debounce
} else if (key == 'B') {
handleButtonBPress();
delay(500); // Add a small delay for button debounce
} else if (key == 'C') {
handleButtonCPress();
delay(500); // Add a small delay for button debounce
} else if (key == 'D') {
handleButtonDPress();
delay(500); // Add a small delay for button debounce
}
}
}
void handleButtonAPress() {
if (lcdIdle) {
lcdIdle = false; // LCD is no longer in idle state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan PIN:");
char enteredPin[5];
int pinIndex = 0;
while (pinIndex < 4) {
char key = keypad.getKey();
if (key != NO_KEY) {
enteredPin[pinIndex] = key;
lcd.setCursor(pinIndex, 1);
lcd.print("*");
pinIndex++;
delay(500); // Add a small delay for button debounce
}
}
enteredPin[4] = '\0'; // Add null terminator to the PIN string
if (strcmp(enteredPin, pins[currentParticipantIndex]) == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Galon dicabut");
delay(1000);
lcd.clear();
galonInstalled = false;
displayIdleMessage();
lcdIdle = true; // Return LCD to idle state
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIN salah");
delay(1000);
lcd.clear();
displayIdleMessage();
lcdIdle = true; // Return LCD to idle state
}
}
}
void handleButtonBPress() {
lcdIdle = false; // LCD is no longer in idle state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Galon terpasang");
delay(1000);
lcd.clear();
galonInstalled = true;
updateNextParticipant();
displayIdleMessage();
lcdIdle = true; // Return LCD to idle state
}
void handleButtonCPress() {
lcdIdle = false; // LCD is no longer in idle state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Nama giliran:");
lcd.setCursor(0, 1);
lcd.print(names[currentParticipantIndex]);
delay(3000);
lcd.clear();
displayIdleMessage();
lcdIdle = true; // Return LCD to idle state
}
void handleButtonDPress() {
lcdIdle = false; // LCD is no longer in idle state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BELI DI ALFAMART");
lcd.setCursor(0, 1);
lcd.print("BELAKANG SEKOLAH");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" HARGA ");
lcd.setCursor(0, 1);
lcd.print(" Rp. 12.000 ");
delay(500);
lcd.setCursor(0, 0);
lcd.print("BELI DI ALFAMART");
lcd.setCursor(0, 1);
lcd.print("BELAKANG SEKOLAH");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" HARGA ");
lcd.setCursor(0, 1);
lcd.print(" Rp. 12.000 ");
delay(500);
lcd.setCursor(0, 0);
lcd.print("BELI DI ALFAMART");
lcd.setCursor(0, 1);
lcd.print("BELAKANG SEKOLAH");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" HARGA ");
lcd.setCursor(0, 1);
lcd.print(" Rp. 12.000 ");
delay(500);
lcd.setCursor(0, 0);
lcd.print("BELI DI ALFAMART");
lcd.setCursor(0, 1);
lcd.print("BELAKANG SEKOLAH");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" HARGA ");
lcd.setCursor(0, 1);
lcd.print(" Rp. 12.000 ");
delay(1000);
lcd.clear();
displayIdleMessage();
lcdIdle = true; // Return LCD to idle state
}
void updateNextParticipant() {
currentParticipantIndex++;
if (currentParticipantIndex >= numParticipants) {
currentParticipantIndex = 0;
}
}
void displayIdleMessage() {
const unsigned long interval = 1000; // Interval between text changes (in milliseconds)
unsigned long previousMillis = 0; // Timestamp of the last text change
int textIndex = 0; // Index of the current text being displayed
digitalWrite(ledG, HIGH);
digitalRead(
while (true) {
// Check if the interval has passed
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
lcd.clear();
// Display the next text
switch (textIndex) {
case 0:
lcd.setCursor(0, 0);
lcd.print(" SELAMAT ");
lcd.setCursor(0, 1);
lcd.print(" MENIKMATI ");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print(" FOLLOW IG" );
lcd.setCursor(0, 1);
lcd.print(" @VESCIVEZEN ");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print(" XII MIPA 5 ");
lcd.setCursor(0, 1);
lcd.print(" MS. FITRIANI ");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print(" DO NOT FORGET ");
lcd.setCursor(0, 1);
lcd.print(" TO CHECK WATER ");
break;
}
// Increment the text index
textIndex++;
if (textIndex > 3) {
textIndex = 0;
}
}
// Check for button presses
char key = keypad.getKey();
if (key != NO_KEY) {
// Exit the function if a button is pressed
return;
}
}
}