This is the code I have taken from a YouTube tutorial. I am very new to Arduino and not to mention my coding skills are poor.
I have already checked all the connection and as well as the components, everything is good!
The problems are:
At the started the space is set to 5. If any car goes in it does decrease! But in the car comes out the count does not increases, it decreases only....
The servo motor runs continuously regardless of what is happening.
I believe something is wrong with the code!
PLEASE HELP ME! I HAVE UPLOADED THE CIRCUIT DIAGRAM AS WELL.
#include <LiquidCrystal.h>// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
#include <Servo.h> //includes the servo library
Servo myservo1;
int ir_s1 = 2;
int ir_s2 = 4;
int Total = 5;
int Space;
int flag1 = 0;
int flag2 = 0;
void setup() {
pinMode(ir_s1, INPUT);
pinMode(ir_s2, INPUT);
myservo1.attach(3);
myservo1.write(100);
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print(" Car Parking ");
lcd.setCursor (0,1);
lcd.print(" System ");
delay (2000);
lcd.clear();
Space = Total;
}
void loop(){
if(digitalRead (ir_s1) == LOW && flag1==0){
if(Space>0){flag1=1;
if(flag2==0){myservo1.write(0); Space = Space-1;}
}else{
lcd.setCursor (0,0);
lcd.print(" Sorry not Space ");
lcd.setCursor (0,1);
lcd.print(" Available ");
delay (1000);
lcd.clear();
}
}
if(digitalRead (ir_s2) == LOW && flag2==0){flag2=1;
if(flag1==0){myservo1.write(0); Space = Space+1;}
}
if(flag1==1 && flag2==1){
delay (1000);
myservo1.write(100);
flag1=0, flag2=0;
}
lcd.setCursor (0,0);
lcd.print("Total Space: ");
lcd.print(Total);
lcd.setCursor (0,1);
lcd.print("Have Space: ");
lcd.print(Space);
}
Look at the State Change Detection example in the IDE (File->examples->02.Digital->StateChangeDetection) and use that to detect when a car comes in or out. You could also include a few Serial.println("....") statements into your code to monitor what is going on when you trip the sensors.
Also, what type of sensors are those? Do they always drive their output pin HIGH/LOW? Some sensors only drive the signal LOW and allow it to float otherwise. If that is the case, you need to declare your pins as INPUT_PULLUP.
J-M-L:
seems like school homework... did you try cheating by copying someone else's code?
can you explain what flag1 and flag2 are used for?
Yes as I have mentioned, it from a YouTube tutorial.
Flag 1 is will be high when space is available and IR 1 detects a car at that time flag 2 will be zero.
Flag 2 is will be high when space is available and IR 2 detects a car at that time flag 1 will be zero.
It is like. the car will enter from ir 2, barrier opens, car goes to ir 2, barrier shuts down, lcd prints a value.
blh64:
Look at the State Change Detection example in the IDE (File->examples->02.Digital->StateChangeDetection) and use that to detect when a car comes in or out. You could also include a few Serial.println("....") statements into your code to monitor what is going on when you trip the sensors.
Also, what type of sensors are those? Do they always drive their output pin HIGH/LOW? Some sensors only drive the signal LOW and allow it to float otherwise. If that is the case, you need to declare your pins as INPUT_PULLUP.
It is an basic IR sensor. I do not think that has some specific model number because I have checked the datasheets.
Hello,
The best thing you can do is start with a little piece of code, test it and then extend the program.
Another thing that could be of interest: monitor your variables while your sketch is running.
You might wan to take a look at my website sylvesersolutions.com: there is software that you can download and then you can read and write all your variables in your Arduino just like in an excel sheet.
So you mean I should not give power to servo motor from arduino and give it power from a battery or some other source ?
But how will it make any difference?