These are the summary of the programs:
- There are 2 IR sensors, one for the count up and one for the count down. The output is 1 seven segment display.
- 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.
- 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
}
}
- 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!