Combining 2 different programs - LCD display and sensors

These are the summary of the programs:

  1. There are 2 IR sensors, one for the count up and one for the count down. The output is 1 seven segment display.
  2. LCD to print duration and cost with the user input. (Eg. Duration: 8, LCD will display "Duration: 8 hrs. Cost: 140").

Both are working separately but I wanted to combine these programs for only 1 arduino. I used exactly 12 pins for all the data inputs and outputs.

When I tried to combine these progs, LCD works fine but the IR sensors won't count up and down. It doesn't also print that the IR has been set to high. Here are the programs.

  1. IR + 7-segment
//Pin connected to DS of 74HC595 DATA
int inputPinForCounter = 2;

//Pin connected to ST_CP of 74HC595 CLOCK
int regPinForCounter = 3;

//Pin connected to SH_CP of 74HC595 LATCH
int shiftPinForCounter = 4;

//Pin connected to Master_Reclear of 74HC595 CLEAR
int clearPinForCounter = 5;

int ir1Pin = 6; //entrance -1
int ir2Pin = 7; //exit +1


//array of nos. 0-9 goes here
const byte numbers[10]= {
  B10111110, // 0
  B00001100, // 1
  B01110110, // 2
  B01011110, // 3
  B11001100, // 4
  B11011010, // 5
  B11111010, // 6
  B00001110, // 7
  B11111110, // 8
  B11011110  // 9
};





int availableSpace = 9;
String availableSpaceString;
char availableSpaceChar[1];
String availableSpaceStringArr[1];
int availableSpaceIntArr[1];

int onesDigit;
boolean cycle = false;

void setup() {
  pinMode(regPinForCounter, OUTPUT);
  pinMode(shiftPinForCounter, OUTPUT);
  pinMode(inputPinForCounter, OUTPUT);
  pinMode(clearPinForCounter, OUTPUT);
  pinMode(ir1Pin, INPUT);
  pinMode(ir2Pin, INPUT);

  Serial.begin(9600);
  //show 9 in available space
  digitalWrite(clearPinForCounter, HIGH);
  digitalWrite(regPinForCounter, LOW);
  shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[9]);
  Serial.println("test");
  //shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[5]);
  digitalWrite(regPinForCounter, HIGH);
}

void loop() {
  //if a car enters
  if(digitalRead(ir1Pin) == HIGH) {
    Serial.println("high ir1");
    delay(400);
    availableSpace-=1;
    Serial.println(availableSpace);
    if(availableSpace<0) {
      Serial.print("parking already full");
      availableSpace+=1;
    } else {
      checkingAndDisplaying(availableSpace);
    }
  }

  //if a car leaves
  else if(digitalRead(ir2Pin) == HIGH) {
    Serial.println("high ir2");
    delay(400);
    availableSpace+=1;
     Serial.println(availableSpace);
    if(availableSpace > 10) {
      Serial.println("invalid no. of space");
      availableSpace-=1;
      Serial.println(availableSpace);
    } else {
      checkingAndDisplaying(availableSpace);
    }
  }
  else {
    //do nothing
  }
}

void checkingAndDisplaying(int availableSpace) {
  if(availableSpace <10) {
      for(int i=0; i<10; i++) {
        if(availableSpace == i) {
            digitalWrite(regPinForCounter, LOW);
			shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[availableSpace]);
			digitalWrite(regPinForCounter, HIGH);
        }
      }
    }
    else {
      //do nothing
    }
}
  1. LCD
#include <LiquidCrystal.h>

LiquidCrystal lcd(13,12, 11, 10, 9, 8);
int duration = 0;
int cost = 0;
void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600);


}

void loop()
{
  boolean newId = false;
  boolean check = false;
  while(newId == false){

      Serial.println("Duration in hours: ");
      while(Serial.available() == 0);
      {

        duration = Serial.parseInt();
        
      }
      if(duration <= 4){
      newId = true;
      delay(100);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Duration");
      lcd.setCursor(10,0);
      lcd.print("Cost");
      lcd.setCursor(0,1);
      Serial.println("Processing...");
        if(newId == true){
            String sduration=String(duration);
            lcd.print(sduration + " hrs");
            lcd.setCursor(10,1);
            lcd.write("30php");
            check = true;
        }
      
      Serial.println("Displayed");
      }
      else if (duration>4){
      newId = true;
      
      delay(100);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Duration");
      lcd.setCursor(10,0);
      lcd.print("Cost");
      lcd.setCursor(0,1);
      Serial.println("Processing...");
       if(newId == true){
            cost= ((duration-4)*10)+30;
             String sduration=String(duration);
             String scost = String(cost);
            lcd.print(sduration + " hrs");
            lcd.setCursor(10,1);
            lcd.print(scost +"php");
            check = true;
        }
      
      Serial.println("Displayed");

        
      }
      else{
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Error");
      }

    }
    
    if(check == true){
      newId = false;
    }
}

When I tried to insert the program for the sensor to LCD, the LCD works fine, but the sensors won't read. Help me with this. I'm kinda not good at 'while' statements. I suspect I made mistakes in the 'while'. Thanks!

This is my try when I combined. But as I said, the sensors won't count up and down.

#include <LiquidCrystal.h>


LiquidCrystal lcd(13,12, 11, 10, 9, 8);
int duration = 0;
int cost = 0;

//Pin connected to DS of 74HC595 DATA
int inputPinForCounter = 2;

//Pin connected to ST_CP of 74HC595 CLOCK
int regPinForCounter = 3;

//Pin connected to SH_CP of 74HC595 LATCH
int shiftPinForCounter = 4;

//Pin connected to Master_Reclear of 74HC595 CLEAR
int clearPinForCounter = 5;

int ir1Pin = 6; //entrance -1
int ir2Pin = 7; //exit +1

const byte numbers[10]= {
  B10111110, // 0
  B00001100, // 1
  B01110110, // 2
  B01011110, // 3
  B11001100, // 4
  B11011010, // 5
  B11111010, // 6
  B00001110, // 7
  B11111110, // 8
  B11011110  // 9
};


int availableSpace = 9;
String availableSpaceString;
char availableSpaceChar[1];
String availableSpaceStringArr[1];
int availableSpaceIntArr[1];

int onesDigit;
boolean cycle = false;


void setup(){
  lcd.begin(16, 2);
  pinMode(regPinForCounter, OUTPUT);
  pinMode(shiftPinForCounter, OUTPUT);
  pinMode(inputPinForCounter, OUTPUT);
  pinMode(clearPinForCounter, OUTPUT);
  pinMode(ir1Pin, INPUT);
  pinMode(ir2Pin, INPUT);
  Serial.begin(9600);
  digitalWrite(clearPinForCounter, HIGH);
  digitalWrite(regPinForCounter, LOW);
  shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[9]);
  Serial.println("test");
  digitalWrite(regPinForCounter, HIGH);
  
}

void loop()
{
  boolean countcheck = false;
  boolean countstart = false;
  
       if(digitalRead(ir1Pin) == HIGH) {
        Serial.println("high ir1");
        delay(400);
        availableSpace-=1;
        Serial.println(availableSpace);
        if(availableSpace<0) {
          Serial.print("parking already full");
          availableSpace+=1;
        } else {
          checkingAndDisplaying(availableSpace);
        }
      }
      else if(digitalRead(ir2Pin) == HIGH) {
      Serial.println("high ir2");
      delay(400);
      availableSpace+=1;
       Serial.println(availableSpace);
        if(availableSpace > 10) {
          Serial.println("invalid no. of space");
          availableSpace-=1;
          Serial.println(availableSpace);
        } else {
          checkingAndDisplaying(availableSpace);
        }
     }


  boolean newId = false;
  boolean check = false;

  
  if(newId == false){
      while(Serial.available() == 0);
      {

        duration = Serial.parseInt();
        
      }

    Serial.println("Duration in hours: ");



     if(duration <= 4){
      newId = true;
      delay(100);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Duration");
      lcd.setCursor(10,0);
      lcd.print("Cost");
      lcd.setCursor(0,1);
      Serial.println("Processing...");
        if(newId == true){
            String sduration=String(duration);
            lcd.print(sduration + " hrs");
            lcd.setCursor(10,1);
            lcd.write("30php");
            check = true;
        }
      
      Serial.println("Displayed");
      }
      else if (duration>4){
        newId = true;
        
        delay(100);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Duration");
        lcd.setCursor(10,0);
        lcd.print("Cost");
        lcd.setCursor(0,1);
        Serial.println("Processing...");
         if(newId == true){
              cost= ((duration-4)*10)+30;
               String sduration=String(duration);
               String scost = String(cost);
              lcd.print(sduration + " hrs");
              lcd.setCursor(10,1);
              lcd.print(scost +"php");
              check = true;
          }
        
        Serial.println("Displayed");

        
      }
      else{
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Error");
      }

    }
    
    if(check == true){
      newId = false;
    }
}

  


void checkingAndDisplaying(int availableSpace) {
  if(availableSpace <10) {
      for(int i=0; i<10; i++) {
        if(availableSpace == i) {
            digitalWrite(regPinForCounter, LOW);
      shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[availableSpace]);
      digitalWrite(regPinForCounter, HIGH);
        }
      }
    }
    else {
      //do nothing
    }
}

I've only had a brief look through your loop. I've made some minor changes, it's unlikely i've solved your problem yet.

In the meantime, I wonder if you'd explain what your checkingAndDisplaying function is supposed to display exactly. It looks overcomplicated for what I think it's attempting to achieve.

//Pin connected to DS of 74HC595 DATA
int inputPinForCounter = 2;
//Pin connected to ST_CP of 74HC595 CLOCK
int regPinForCounter = 3;

//Pin connected to SH_CP of 74HC595 LATCH
int shiftPinForCounter = 4;
//Pin connected to Master_Reclear of 74HC595 CLEAR
int clearPinForCounter = 5;
int ir1Pin = 6; //entrance -1
int ir2Pin = 7; //exit +1

//array of nos. 0-9 goes here
const byte numbers[10]= {
  B10111110, // 0
  B00001100, // 1
  B01110110, // 2
  B01011110, // 3
  B11001100, // 4
  B11011010, // 5
  B11111010, // 6
  B00001110, // 7
  B11111110, // 8
  B11011110  // 9
};

int availableSpace = 9;
String availableSpaceString;
char availableSpaceChar[1];
String availableSpaceStringArr[1];
int availableSpaceIntArr[1];

int onesDigit;
boolean cycle = false;

void setup() {
  pinMode(regPinForCounter, OUTPUT);
  pinMode(shiftPinForCounter, OUTPUT);
  pinMode(inputPinForCounter, OUTPUT);
  pinMode(clearPinForCounter, OUTPUT);
  pinMode(ir1Pin, INPUT);
  pinMode(ir2Pin, INPUT);

  Serial.begin(9600);
  //show 9 in available space
  digitalWrite(clearPinForCounter, HIGH);
  digitalWrite(regPinForCounter, LOW);
  shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[9]);
  Serial.println("test");
  //shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[5]);
  digitalWrite(regPinForCounter, HIGH);
}

void loop() {
  //if a car enters
  if(digitalRead(ir1Pin) == HIGH) {
    Serial.println("high ir1");
    delay(400);
    availableSpace-=1;
   // Serial.println(availableSpace);//printing an int as if it's a string
     Serial.println(availableSpace,DEC);//should be this
    
   if(availableSpace<0){
      Serial.print("parking already full");
      availableSpace+=1;
    } else {
      checkingAndDisplaying(availableSpace);
    }
  }

  //if a car leaves
  //ELSE! WHY ELSE.  Can't cars leave as others approach?
  //else 
  if(digitalRead(ir2Pin) == HIGH) {
    Serial.println("high ir2");
    delay(400);
    availableSpace+=1;
     //Serial.println(availableSpace);//see above
     Serial.println(availableSpace,DEC);
    if(availableSpace > 10) {
      Serial.println("invalid no. of space");
      availableSpace-=1;
      //Serial.println(availableSpace);//and again
      Serial.println(availableSpace,DEC);
    } else {
      checkingAndDisplaying(availableSpace);
    }
  }
  
  //totally redundant 
  //else {
  //  //do nothing
  // }
}

  void checkingAndDisplaying(int availableSpace) {
  if(availableSpace <10) {
      
    //here you're going through a loop until you find
    for(int i=0; i<10; i++) {
        if(availableSpace == i) {
            digitalWrite(regPinForCounter, LOW);
			shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[availableSpace]);
			digitalWrite(regPinForCounter, HIGH);
        }
      }
    }
    else {
      //do nothing
    }
}

I used Shift register + 7 segment display.

What it does it it will just loop each segment with no delay.

shiftOut(inputPinForCounter, shiftPinForCounter, MSBFIRST, numbers[availableSpace]);

So depending on the number of space available, it will display the number in the seven segment.

In the upper portion, there's a availableSpace+=1; or availableSpace-=1; Whatever the value the availableSpace holds, it will insert in the function checkingAndDisplaying, and look for the array numbers[availableSpace], and display it in the 7-seg.

EDIT:
The programs I put in the very first post, are both working. But when I tried to combined the two, only the sensors won't work.

Could I just ask what this line doeslcd.write("30php");

KenF:
Could I just ask what this line doeslcd.write("30php");

the project is actually a parking lot system. The 1st IR is for the entrance and the 2nd IR is for the exit. So it counts up and down when detected by these IR.

In the exit, there's a counter in which we used LCD to display the duration and cost. so in our rate, for the first 4 hours, the cost is 30 php, the succedding hours is +10php. The duration will be typed in the serial monitor.

Have a look at this demo about merging programs. The methodology keeps the parts in separate functions so it might be easier to debug.

...R

jnrnl:

KenF:
Could I just ask what this line doeslcd.write("30php");

the project is actually a parking lot system. The 1st IR is for the entrance and the 2nd IR is for the exit. So it counts up and down when detected by these IR.

I think you should be using lcd.print Write could be giving you unpredictable results.