Hello, can you help me with this code to help me fix it? I'm trying to load it on the LCD and it doesn't work, please thanks in advance

#include <Servo.h> // include biblioteca pentru servo
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD cu 20x4 caractere
Servo myservo;

#define ir_enter 2
#define ir_back 4
#define ir_car1 5
#define ir_car2 6
#define ir_car3 7
#define ir_car4 8
#define ir_car5 9
#define ir_car6 10

int S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;
int flag1 = 0, flag2 = 0;
int slot = 6;

void setup() {
Serial.begin(9600);

pinMode(ir_car1, INPUT);
pinMode(ir_car2, INPUT);
pinMode(ir_car3, INPUT);
pinMode(ir_car4, INPUT);
pinMode(ir_car5, INPUT);
pinMode(ir_car6, INPUT);
pinMode(ir_enter, INPUT);
pinMode(ir_back, INPUT);

myservo.attach(3);
myservo.write(90); // Setăm servomotorul pe poziția 90°

lcd.begin(20, 4);
lcd.setCursor(0, 1);
lcd.print(" Car parking ");
lcd.setCursor(0, 2);
lcd.print(" System ");
delay(20);
lcd.clear();

Read_Sensor(); // Citim senzorii inițial
int total = S1 + S2 + S3 + S4 + S5 + S6;
slot = slot - total; // Calculăm câte locuri de parcare sunt disponibile
}

void loop() {
Read_Sensor(); // Citim starea senzorilor la fiecare ciclu

lcd.setCursor(0, 0);
lcd.print(" Have Slot: ");
lcd.print(slot);
lcd.print(" ");

lcd.setCursor(0, 1);
lcd.print(S1 == 1 ? "S1:Fill " : "S1:Empty");

lcd.setCursor(10, 1);
lcd.print(S2 == 1 ? "S2:Fill " : "S2:Empty");

lcd.setCursor(0, 2);
lcd.print(S3 == 1 ? "S3:Fill " : "S3:Empty");

lcd.setCursor(10, 2);
lcd.print(S4 == 1 ? "S4:Fill " : "S4:Empty");

lcd.setCursor(0, 3);
lcd.print(S5 == 1 ? "S5:Fill " : "S5:Empty");

lcd.setCursor(10, 3);
lcd.print(S6 == 1 ? "S6:Fill " : "S6:Empty");

if (digitalRead(ir_enter) == 0 && flag1 == 0) {
if (slot > 0) {
flag1 = 1;
if (flag2 == 0) {
myservo.write(180); // Deschidem poarta
slot = slot - 1;
}
} else {
lcd.setCursor(0, 0);
lcd.print(" Sorry Parking Full ");
delay(1500);
}
}

if (digitalRead(ir_back) == 0 && flag2 == 0) {
flag2 = 1;
if (flag1 == 0) {
myservo.write(180); // Deschidem poarta
slot = slot + 1;
}
}

if (flag1 == 1 && flag2 == 1) {
delay(1000);
myservo.write(90); // Închidem poarta
flag1 = 0;
flag2 = 0;
}

delay(500); // Adăugăm un delay pentru a face actualizările mai clare
}

void Read_Sensor() {
// Resetăm senzorii
S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;

// Citim fiecare senzor IR
if (digitalRead(ir_car1) == 0) { S1 = 1; }
if (digitalRead(ir_car2) == 0) { S2 = 1; }
if (digitalRead(ir_car3) == 0) { S3 = 1; }
if (digitalRead(ir_car4) == 0) { S4 = 1; }
if (digitalRead(ir_car5) == 0) { S5 = 1; }
if (digitalRead(ir_car6) == 0) { S6 = 1; }
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do

Posting your code using code tags 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.

1 Like

Which Arduino board are you using ?

You say that you are trying to load the code "on the LCD". What do you mean by this and what does "doesn't work" mean ?

Arduino uno

#include <Servo.h> // include biblioteca pentru servo
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD cu 20x4 caractere
Servo myservo;

#define ir_enter 2
#define ir_back 4
#define ir_car1 5
#define ir_car2 6
#define ir_car3 7
#define ir_car4 8
#define ir_car5 9
#define ir_car6 10

int S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;
int flag1 = 0, flag2 = 0;
int slot = 6;

void setup() {
Serial.begin(9600);

pinMode(ir_car1, INPUT);
pinMode(ir_car2, INPUT);
pinMode(ir_car3, INPUT);
pinMode(ir_car4, INPUT);
pinMode(ir_car5, INPUT);
pinMode(ir_car6, INPUT);
pinMode(ir_enter, INPUT);
pinMode(ir_back, INPUT);

myservo.attach(3);
myservo.write(90); // Setăm servomotorul pe poziția 90°

lcd.begin(20, 4);
lcd.setCursor(0, 1);
lcd.print(" Car parking ");
lcd.setCursor(0, 2);
lcd.print(" System ");
delay(20);
lcd.clear();

Read_Sensor(); // Citim senzorii inițial
int total = S1 + S2 + S3 + S4 + S5 + S6;
slot = slot - total; // Calculăm câte locuri de parcare sunt disponibile
}

void loop() {
Read_Sensor(); // Citim starea senzorilor la fiecare ciclu

lcd.setCursor(0, 0);
lcd.print(" Have Slot: ");
lcd.print(slot);
lcd.print(" ");

lcd.setCursor(0, 1);
lcd.print(S1 == 1 ? "S1:Fill " : "S1:Empty");

lcd.setCursor(10, 1);
lcd.print(S2 == 1 ? "S2:Fill " : "S2:Empty");

lcd.setCursor(0, 2);
lcd.print(S3 == 1 ? "S3:Fill " : "S3:Empty");

lcd.setCursor(10, 2);
lcd.print(S4 == 1 ? "S4:Fill " : "S4:Empty");

lcd.setCursor(0, 3);
lcd.print(S5 == 1 ? "S5:Fill " : "S5:Empty");

lcd.setCursor(10, 3);
lcd.print(S6 == 1 ? "S6:Fill " : "S6:Empty");

if (digitalRead(ir_enter) == 0 && flag1 == 0) {
if (slot > 0) {
flag1 = 1;
if (flag2 == 0) {
myservo.write(180); // Deschidem poarta
slot = slot - 1;
}
} else {
lcd.setCursor(0, 0);
lcd.print(" Sorry Parking Full ");
delay(1500);
}
}

if (digitalRead(ir_back) == 0 && flag2 == 0) {
flag2 = 1;
if (flag1 == 0) {
myservo.write(180); // Deschidem poarta
slot = slot + 1;
}
}

if (flag1 == 1 && flag2 == 1) {
delay(1000);
myservo.write(90); // Închidem poarta
flag1 = 0;
flag2 = 0;
}

delay(500); // Adăugăm un delay pentru a face actualizările mai clare
}

void Read_Sensor() {
// Resetăm senzorii
S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;

// Citim fiecare senzor IR
if (digitalRead(ir_car1) == 0) { S1 = 1; }
if (digitalRead(ir_car2) == 0) { S2 = 1; }
if (digitalRead(ir_car3) == 0) { S3 = 1; }
if (digitalRead(ir_car4) == 0) { S4 = 1; }
if (digitalRead(ir_car5) == 0) { S5 = 1; }
if (digitalRead(ir_car6) == 0) { S6 = 1; }
}

Thank you

Now, what "doesn't work" ?

I loaded the program and it is not displayed on the LCD screen

Have you tried the examples that came with the library ?

Are you sure that you are using the correct I2C address for the LCD ?

I suggest that you try using the I2C scanner sketch from the Wire library to ascertain whether the Uno can communicate with the LCD and, if so, what its address is

yes, it is the right address because I tried another program with the same address and it works

@denis1221

Hi

The display works with the sketch you provided

1 Like

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