Code not doing what it should

i have a project which is in the attachment if its not clear here.

and this is the code:

#include<Servo.h>
#include<LiquidCrystal.h>
#include<Keypad.h>

LiquidCrystal lcd (10,11,12,A1,A2,A3);
Servo servo;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

/you need to enter in below arrays the corresponding Arduino pin #'s and you need to fill them according to below sequence, i.e; col3, col2, col1 and row1, row2, row3, row4/
/keypad row pins:2 7 6 4/
/row1 row2 row3 row4/
byte rowPins[ROWS] = {7,2, 3, 5}; //Arduino Pins
/keypad column pins:5 1 3/
/col1 col2 col3/
byte colPins[COLS] = {6, A0, 4}; //Arduino Pins

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

int GreenLed = 8;
int RedLed=9;
char presses;
int in,numcar=0;
int ent1=0;
int ent2=0;
int ent3 =0;
int i;
unsigned long StartTime[5];
unsigned long EndTime;
unsigned long t;
double ParkingFee;

void Gate_func()
{
digitalWrite(GreenLed,1);
delay(1000);
lcd.setCursor(0,1);
lcd.print(“The Gate is Opening”);
for (int i=0; i<=90; i++)
{
servo.write(i);
delay(20);
}
lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(“The Gate is open”);
delay(3000);
lcd.setCursor(0,1);
lcd.print(” ");
lcd.setCursor(0,1);
lcd.print(“The Gate is closing”);
for (int i=90; i>=0; i–)
{
servo.write(i);
delay(20);
}
digitalWrite(GreenLed,0);

}
void parkState(int s)
{
if (s==1)
{
digitalWrite(RedLed,1);
delay(1000);
for (int i=0; i<5; i++)
{
lcd.clear();
delay(200);
lcd.setCursor(0,0);
lcd.print(“Sorry,parking is full”);
for (int positionCounter =0; positionCounter<30; positionCounter++)
{
lcd.scrollDisplayLeft();
delay(150);
}
}
delay(1000);
lcd.clear();
digitalWrite(RedLed,0);
}
}
void Num_cars(int numcar)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Numbers of cars inside = ");
lcd.print(numcar);
for (int positionCounter =0; positionCounter<30; positionCounter++)
{
lcd.scrollDisplayLeft();
delay(150);
}
lcd.setCursor(0,1);
lcd.print("Number of Available car slots = ");
lcd.print(5-numcar);
for (int positionCounter =0; positionCounter<30; positionCounter++)
{
lcd.scrollDisplayLeft();
delay(150);
}
}
void setup()
{
servo.attach(A5);
servo.write(0);
lcd.begin(16,2);
Serial.begin(9600);
pinMode(GreenLed,OUTPUT);
pinMode(RedLed,OUTPUT);

}

void loop()
{
in = analogRead(A4);
//Serial.println(in);

presses = keypad.getKey();
if(presses !=NO_KEY)
{
if(i == 0)
{
Serial.println("The entered key code: ");
ent1 = presses;
Serial.print(presses);
}

if(i ==1)
{
Serial.print(presses);
ent2 = (presses-48);
}

if(i == 2)
{
Serial.println(presses);
}

i++;
}

else if(i ==3)
{
Serial.print("The car number is: ");
Serial.println(ent2);

i = 0;
}

if (in > 300 && numcar < 5 && ent1==’*’)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Car Allowed in”);
StartTime[5] = millis();
Gate_func();
lcd.clear();
numcar++;
Num_cars(numcar);

}
else if (in < 100 && numcar > 0 && ent1==’#’)
{
EndTime = millis()-StartTime[numcar-1];
t=EndTime/1000;
ParkingFee= t*0.28;
Serial.print("Pay a Fee of = ");
Serial.print(ParkingFee);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Car Allowed out”);
Gate_func();
numcar–;
Num_cars(numcar);
}
else if (in > 300 && numcar == 5)
{
parkState(1);
}
}

it was working fine until I put the time and the fee in, I also have a problem with the LEDs not lighting up when they should.

can someone help please? because it’s due next week and I’m freaking out.

Can you edit the post to put the code into some code tags please so that it is more readable.

Is this a school project ?

 if(presses !=NO_KEY)
  {

  }

  else if(i ==3)
  {
    Serial.print("The car number is: ");
    Serial.println(ent2);

How does that else if logically go with that if?

Using arrays would cut out about 90% of your code.

Anyway, the code does something. You expect it to do something. You need to explain what those two things are, so we can help correct your expectations.

#include<Servo.h>
#include<LiquidCrystal.h>
#include<Keypad.h>

LiquidCrystal lcd (10,11,12,A1,A2,A3);
Servo servo;

const byte ROWS = 4; //four rows
 const byte COLS = 3; //three columns
 char keys[ROWS][COLS] = {
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'},
   {'*','0','#'}
 };

 /*you need to enter in below arrays the corresponding Arduino pin #'s and you need to fill them according to below sequence, i.e; col3, col2, col1 and row1, row2, row3, row4*/
     /*keypad row pins:2  7  6  4*/
                  /*row1 row2 row3 row4*/
 byte rowPins[ROWS] = {7,2, 3, 5}; //Arduino Pins
  /*keypad column pins:5  1  3*/
                  /*col1 col2 col3*/
 byte colPins[COLS] = {6, A0, 4}; //Arduino Pins

 Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

int GreenLed = 8;
int RedLed=9;
char presses;
int in,numcar=0;
int ent1=0; 
int ent2=0; 
int ent3 =0;
int i;
unsigned long StartTime[5];
unsigned long EndTime;
unsigned long t;
double ParkingFee;


void Gate_func()
{
  digitalWrite(GreenLed,1);
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("The Gate is Opening");
  for (int i=0; i<=90; i++)
  {
    servo.write(i);
    delay(20);
  }
  lcd.setCursor(0,1);
  lcd.print("              ");
  lcd.setCursor(0,1);
  lcd.print("The Gate is open");
  delay(3000);
  lcd.setCursor(0,1);
  lcd.print("              ");
  lcd.setCursor(0,1);
  lcd.print("The Gate is closing");
  for (int i=90; i>=0; i--)
  {
    servo.write(i);
    delay(20);
  }
  digitalWrite(GreenLed,0);
  
}
void parkState(int s)
{
  if (s==1)
  {
    digitalWrite(RedLed,1);
    delay(1000);
    for (int i=0; i<5; i++)
    {
      lcd.clear();
      delay(200);
      lcd.setCursor(0,0);
      lcd.print("Sorry,parking is full");
      for (int positionCounter =0; positionCounter<30; positionCounter++) 
        {
           lcd.scrollDisplayLeft();
           delay(150);
        }
    }
    delay(1000);
    lcd.clear();
    digitalWrite(RedLed,0);
  }
}
void Num_cars(int numcar)
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Numbers of cars inside = ");
  lcd.print(numcar);
  for (int positionCounter =0; positionCounter<30; positionCounter++) 
        {
           lcd.scrollDisplayLeft();
           delay(150);
        }
  lcd.setCursor(0,1);
  lcd.print("Number of Available car slots = ");
  lcd.print(5-numcar);
  for (int positionCounter =0; positionCounter<30; positionCounter++) 
        {
           lcd.scrollDisplayLeft();
           delay(150);
        }
}
void setup()
{
  servo.attach(A5);
  servo.write(0);
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(GreenLed,OUTPUT);
  pinMode(RedLed,OUTPUT);
  
}


void loop()
{
  in = analogRead(A4);
  //Serial.println(in);

  presses = keypad.getKey();
  if(presses !=NO_KEY)
  {
    if(i == 0)
    {
      Serial.println("The entered key code: ");
      ent1 = presses;
      Serial.print(presses);
    }

    if(i ==1)
    {
      Serial.print(presses);
      ent2 = (presses-48);
    }
    
    if(i == 2)
    {
      Serial.println(presses);
    }
    
   i++;
  }

  else if(i ==3)
  {
    Serial.print("The car number is: ");
    Serial.println(ent2);
    
    i = 0; 
  }

  
  if (in > 300 && numcar < 5 && ent1=='*')
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Car Allowed in");
    StartTime[5] = millis();
    Gate_func();
    lcd.clear();
    numcar++;
    Num_cars(numcar);
    
  }
  else if (in < 100 && numcar > 0 && ent1=='#')
  {
    EndTime = millis()-StartTime[numcar-1];
    t=EndTime/1000;
   ParkingFee= t*0.28;
   Serial.print("Pay a Fee of = ");
   Serial.print(ParkingFee);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Car Allowed out");
    Gate_func();
    numcar--;
    Num_cars(numcar);
  }
  else if (in > 300 && numcar == 5)
  {
    parkState(1);
  }
}

it’s a final project for my microprocessors class and it’s worth 30% of the final grade.

it's a final project for my microprocessors class and it's worth 30% of the final grade.

So? We can't help you unless you tell us what the code actually does, and how your expectations differ from that.

the code lets the cars in by using the a flashlight on the LDR and typing '*' as a beginning for the entry code and the time should start from here and it apparently doesn't as it doesn't show anything about time in the Serial monitor .and it allowed cars out by putting a paper on the LDR and typing '#' as a beginning for the exit code before I put the time and parking fee in the condition of the cars exit, also I put the LEDs with the conditions and they don't work. the time and parking fee don't work also as I tried them separately.

Hi HawraT,

Here’s a potential lead. What is this line supposed to be doing: if (in > 300 && numcar < 5 && ent1==’*’)

There is at least one other that checks to see if ent1==’#’

I haven’t completely reviewed your code, but I don’t believe that the ent1 variable will contain those values. You might want to double-check this to make sure its correct.

Pat.

HawraT:
it's a final project for my microprocessors class and it's worth 30% of the final grade.

This post is in "gigs and collaborations". "Gig" means "paid freelance work". Were you looking to pay someone to fix your code for you? Would that constitute academic fraud according to the rules of your institution?

 byte rowPins[ROWS] = {7,2, 3, 5}; //Arduino Pins
 byte colPins[COLS] = {6, A0, 4}; //Arduino Pins
 Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

How is the keypad object supposed to know that colPins[1] is an analog pin A0 and not digital pin 0?

Why is it that when i==0, you set ent1 to presses, but when i==1 you subtract 48? Oh, I see: first digit is a hash or star symbol.

Why is it that all the code relating to raising and lowering the gate does not reference i? And why not use a better name for i? It's not just a little temporary counter - it's important.

HawraT, I'm sorry man, but this is all a bit of a mess. Get it mostly working, submit the damn thing, and content yourself with a C or B. Many of the other students won't be able to do this at all, so winning.

How is the keypad object supposed to know that colPins[1] is an analog pin A0 and not digital pin 0?

The pin is the digital pin that shares space with the analog pin. That is why is it numbered 14 (the VALUE of A0), not 0.

PaulMurrayCbr:
This post is in "gigs and collaborations". "Gig" means "paid freelance work". Were you looking to pay someone to fix your code for you? Would that constitute academic fraud according to the rules of your institution?

 byte rowPins[ROWS] = {7,2, 3, 5}; //Arduino Pins

byte colPins[COLS] = {6, A0, 4}; //Arduino Pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);



How is the keypad object supposed to know that colPins[1] is an analog pin A0 and not digital pin 0?

Why is it that when i==0, you set ent1 to presses, but when i==1 you subtract 48? Oh, I see: first digit is a hash or star symbol.

Why is it that all the code relating to raising and lowering the gate does not reference i? And why not use a better name for i? It's not just a little temporary counter - it's important.

HawraT, I'm sorry man, but this is all a bit of a mess. Get it mostly working, submit the damn thing, and content yourself with a C or B. Many of the other students won't be able to do this at all, so winning.

I got it working to some extent as I changed the code and only the LEDs not working now and I have until next week to submit it so I will continue to work on it until then,and yes I was going to pay someone to do it for me instead as this subject is an elective and not a main subject and I already passed with my grades in the assignment I just wanted to get a good grade to raise my GPA.

HawraT:
I just wanted to get a good grade to raise my GPA.

Getting a better GPA by learning and doing is awesome. Getting a better GPA by paying someone to do your homework for you is dishonest and reprehensible.

PaulS:
The pin is the digital pin that shares space with the analog pin. That is why is it numbered 14 (the VALUE of A0), not 0.

Huh. Just learned something.

So, you can treat those analog pins as digital inputs. In effect, it's just that as it happens the board has a ADC on those particular pins. I wrote some code elsewhere where I went to some trouble to convert an analogRead into a HIGH/LOW - completely unnecessary.

Can you use INPUT_PULLUP on them, or do you have to supply a pullup resistor? (Specifically, for the Arduino Uno).

Delta_G:
Getting a better GPA by learning and doing is awesome. Getting a better GPA by paying someone to do your homework for you is dishonest and reprehensible.

Inefficient, too. Why not simply bribe or blackmail the teacher? It's no more morally bad than having someone else do your coursework for you.

PaulMurrayCbr:
Huh. Just learned something.

So, you can treat those analog pins as digital inputs. In effect, it's just that as it happens the board has a ADC on those particular pins. I wrote some code elsewhere where I went to some trouble to convert an analogRead into a HIGH/LOW - completely unnecessary.

Can you use INPUT_PULLUP on them, or do you have to supply a pullup resistor? (Specifically, for the Arduino Uno).

They'll do anything any other digital pin can do. They really are digital pins first and foremost. That they connect to the multiplexer for the ADC is their secondary function. Kind of like how 10 -13 have a second function as SPI port or 2and 3 as external interrupts.