What is going on
With all those white
Lines spaces in the code?
And why don’t you properly indent the code? press the ctrl-T keys from time to time, your code will read better and make it easier for us to read it.
Why don’t the servo sensors PINs have a small meaningful name ?
Why didn’t you fix the long versus int return value I mentioned in #6 for readsensor()
?
I won’t answer anymore question if your code is not properly presented, and remarks not taken into account. I consider this lack of respect.
To the code itself:
In the first part of the loop():
dist[0] = readsensor(trigPin1, echoPin1);
if (distm[0] < dist_threshold)
// check the entry gate
{
servoEnter.write(90);
delay(3000);
servoEnter.write(0);
delay(15);
}
distm[1] = readsensor(trigPin2, echoPin2);
//check the exit gate
if (distm[1] < dist_threshold ) {
servoExit.write(90);
delay(3000);
servoExit.write(0);
delay(15);
}
what are those supposed to do - letting cars in or out by opening and closing the gate I suppose?
—> the delay will kill the parallel nature of the rest of your code, no one can exit while someone enters for example or status won’t update if someone is parking whilst someone is exiting... not a very smart parking. You Need to build a state machine to handle that, you should have no call to significant delay() in the code.
This part
bool slot[4];
int i=0,j=0;
for(i=0;i<4;i++){
if (dist[i]>8){
slot[i]=false;
}
else{
slot[i]=true;
}
what is j
used for?
Why is 8 not a constant with a meaningful name?
Why isn’t that done directly when acquiring the distance since you don’t use the distance besides building this status true/false table? If you had done exercise 4 above...
also slot[n]
is true when sensor is <= 8 which to me means something is detected near the sensor right? (probably a car is on that spot) -> so slot
tells you if it is busy, not if it if free. The variable name should reflect that isParkingSlotOccupied
would be a much better name (cf my exercise if you had given a bit of attention there, you would have seen the use of parkingIsAvailable
as the array name and learnt a best practice about naming variable with some meaningful name...)
*** Now To your question for why it behaves weirdly ***
In this code
char str[5];
for(i = 0; i < 4 ;i++)
{
if(slot[i])
{
char spot[4];
sprintf(spot, "P%d,", i);
strcat(str, spot);
delay(200);
}
}
How many bytes did you set aside/allocate for the whole str
?
how many bytes are you trying to copy into str
?
is that a big problem?
Also once this is fixed, note that you list BUSY slots with this, not free ones as per the above comment - which seemed the opposite of your requirement
PS: I have seen none of the exercise I had asked in #10... you are rushing to the full thing and don’t take time to build up skills and structure thinking