Car Parking lot simulation

Hello
Im doing a small scale parking simulation with 11 parking spots using arduino mega. I have 2 stepper motors (28byj-48) for the gates and 2 buttons to open each one; 13 ultrasonic sensors (HCSR04), 2 for the gates and 11 for the parking spots; and 11 RGB light to show if the spot is available, and a 7 segment display to show the spots available to the outside.

I have already written some of the code but im having issues with realtime working of the simulation, for example the display is flickering and sometimes the motor doesnt work up and down in 90 degrees. If someone could help with the code or have some suggestion or advise i would aprecciate.

Here is the code:

#include <HCSR04.h> //LIBRARY
#include <SevSeg.h>
#include <Stepper.h>

  const int botaoIn = 9; //BUTTON
  const int botaoOut = 4;
  int buttonStateIn;   
  int buttonStateOut;
  
  int RedP1 = 42;    //RGB LED
  int RedP2 = 40;
  int RedP3 = 38;
  int RedP4 = 36;
  int RedP5 = 34;
  int RedP6 = 32;
  int RedP7 = 30;
  int RedP8 = 28;
  int RedP9 = 26;
  int RedP10 = 24;
  int RedP11 = 22;
  int GreenP1 = 43;
  int GreenP2 = 41;
  int GreenP3 = 39;
  int GreenP4 = 37;
  int GreenP5 = 35;
  int GreenP6 = 33;
  int GreenP7 = 31;
  int GreenP8 = 29;
  int GreenP9 = 27;
  int GreenP10 = 25;
  int GreenP11 = 23;
  
  const int stepsPerRevolution = 64; //NUMBER OF STEPS

  int posicao;  //POSITION VARIABLE
  int posicao2; //POSITION VARIABLE
  int leitura;//DISPLAY VARIABLE


  HCSR04 hcIn(46,47); //SENSOR TRIGGER AND ECHO
  HCSR04 hcOut(44,45);
  HCSR04 hcP1(A0,A1);
  HCSR04 hcP2(A2,A3);
  HCSR04 hcP3(A4,A5);
  HCSR04 hcP4(A6,A7);
  HCSR04 hcP5(A8,A9);
  HCSR04 hcP6(A10,A11);
  HCSR04 hcP7(A12,A13);
  HCSR04 hcP8(A14,A15);
  HCSR04 hcP9(52,53);
  HCSR04 hcP10(50,51);
  HCSR04 hcP11(48,49);
  
  SevSeg sevseg; 
  Stepper stepperIn(stepsPerRevolution, 13,12,11,10); //STARTS ENTRY MOTOR
  Stepper stepperOut(stepsPerRevolution, 8,7,6,5); //STARTS EXIT MOTOR

void setup(){

stepperIn.setSpeed(300); 
stepperOut.setSpeed(300); 

byte numDigits = 2;       //4x7 SEGMENT DISPLAY
byte digitPins[] = {17, 18};
byte segmentPins[]={3,2,1,0,14,15,16};
bool resistorsOnSegments = false; 
byte hardwareConfig = COMMON_ANODE; 
bool updateWithDelays = true; 
bool leadingZeros = false; 
bool disableDecPoint = true; 
sevseg.setBrightness(90);
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);

pinMode(botaoIn, INPUT);    
pinMode(botaoOut, INPUT);

pinMode(RedP1, OUTPUT);   
pinMode(RedP2, OUTPUT);
pinMode(RedP3, OUTPUT);
pinMode(RedP4, OUTPUT);
pinMode(RedP5, OUTPUT);
pinMode(RedP6, OUTPUT);
pinMode(RedP7, OUTPUT);
pinMode(RedP8, OUTPUT);
pinMode(RedP9, OUTPUT);
pinMode(RedP10, OUTPUT);
pinMode(RedP11, OUTPUT);
pinMode(GreenP1, OUTPUT);
pinMode(GreenP2, OUTPUT);
pinMode(GreenP3, OUTPUT);
pinMode(GreenP4, OUTPUT);
pinMode(GreenP5, OUTPUT);
pinMode(GreenP6, OUTPUT);
pinMode(GreenP7, OUTPUT);
pinMode(GreenP8, OUTPUT);
pinMode(GreenP9, OUTPUT);
pinMode(GreenP10, OUTPUT);
pinMode(GreenP11, OUTPUT);

posicao=0;
leitura = 11;

digitalWrite (RedP1,LOW);               //INITIAL RGB VALUES
digitalWrite (GreenP1,HIGH);
digitalWrite (RedP2,LOW);
digitalWrite (GreenP2,HIGH);
digitalWrite (RedP3,LOW);
digitalWrite (GreenP3,HIGH);
digitalWrite (RedP4,LOW);
digitalWrite (GreenP4,HIGH);
digitalWrite (RedP5,LOW);
digitalWrite (GreenP5,HIGH);
digitalWrite (RedP6,LOW);
digitalWrite (GreenP6,HIGH);
digitalWrite (RedP7,LOW);
digitalWrite (GreenP7,HIGH);
digitalWrite (RedP8,LOW);
digitalWrite (GreenP8,HIGH);
digitalWrite (RedP9,LOW);
digitalWrite (GreenP9,HIGH);
digitalWrite (RedP10,LOW);
digitalWrite (GreenP10,HIGH);
digitalWrite (RedP11,LOW);
digitalWrite (GreenP11,HIGH);

}

void loop(){ //MAIN LOOP
loopleituradisplay();
buttonStateIn = digitalRead(botaoIn);
buttonStateOut = digitalRead(botaoOut);
loopmotorIn();
loopledsrgb();

}
void loopleituradisplay(){ //DISPLAY LOOP

     sevseg.setNumber(leitura);
     sevseg.refreshDisplay();

}
void loopmotorIn(){
buttonStateIn = digitalRead(botaoIn);
if (buttonStateIn==HIGH && hcP1.dist()>1000 && posicao==0 && leitura!=0){
leitura--;
posicao=1;

(int i = 0; i < 10; i++){
stepperIn.step(stepsPerRevolution);
}
}

else if (hcP1.dist()<15 && posicao==1){
delay (2000);
posicao=0;

    for(int i = 0; i < 10; i++){
            stepperIn.step(-stepsPerRevolution); 
             
      }

}
}
void loopmotorOut(){
buttonStateOut = digitalRead(botaoOut);
if (buttonStateOut==HIGH && hcP2.dist()>1000 && posicao2==0){
leitura++;
posicao=1;

      for(int i = 0; i < 10; i++){
            stepperOut.step(stepsPerRevolution);
       }

}

else if (hcP2.dist()<15 && posicao==1){
delay(2000);
posicao2=0;

    for(int i = 0; i < 10; i++){
            stepperOut.step(-stepsPerRevolution);
             
      }

}
}
void loopledsrgb(){
if (hcP1.dist()>1000 ){
digitalWrite (RedP1,HIGH);
digitalWrite (GreenP1,LOW);
}
if (hcP2.dist()>1000 ){
digitalWrite (RedP2,HIGH);
digitalWrite (GreenP2,LOW);
}
if (hcP3.dist()>1000 ){
digitalWrite (RedP3,HIGH);
digitalWrite (GreenP3,LOW);
}
if (hcP4.dist()>1000 ){
digitalWrite (RedP4,HIGH);
digitalWrite (GreenP4,LOW);
}
if (hcP5.dist()>1000 ){
digitalWrite (RedP5,HIGH);
digitalWrite (GreenP5,LOW);
}
if (hcP6.dist()>1000 ){
digitalWrite (RedP6,HIGH);
digitalWrite (GreenP6,LOW);
}
if (hcP7.dist()>1000){
digitalWrite (RedP7,HIGH);
digitalWrite (GreenP7,LOW);
}
if (hcP8.dist()>1000 ){
digitalWrite (RedP8,HIGH);
digitalWrite (GreenP8,LOW);
}
if (hcP9.dist()>1000 ){
digitalWrite (RedP9,HIGH);
digitalWrite (GreenP9,LOW);
}
if (hcP10.dist()>1000 ){
digitalWrite (RedP10,HIGH);
digitalWrite (GreenP10,LOW);
}
if (hcP11.dist()>1000 ){
digitalWrite (RedP11,HIGH);
digitalWrite (GreenP11,LOW);
}
}

Read up on arrays.
It will make your code a lot shorter - and easier to maintain.
The project is fun, and isn’t too hard, but you’ll learn a lot doing it properly.

That two second delay() isn’t doing you any favours.

1 Like

Thank you for your answer. I have a question, can i do an array on the sensors and LED's even thought they have the same type of programming, they work independently. Can i still use the array on this case?

Yes. That's the point of arrays really.

1 Like

Please read the instructions for posting and edit your first post to show your code correctly. :sunglasses:

Yes. in fact, the next step is to implement an array of structs for the collection of ‘Bays’

Then you could have references to…

Bay[n].sensor
    and
Bay[n].LED

and so on.

1 Like

Another problem you may face is in the HCSR04 library. Like most ultrasonic examples, it uses the blocking function pulseIn() function to measure the echo pulse width and appears to use the default timeout of 1-second. If no car is present in a spot the dist() method could take up to a second to return; this will play havoc with your display.

Since you don't need to measure distance per se but rather just need a "yes/no" maybe you could modify the library to add a timeout parameter to the pulseIn call. Since you need to check for an object within, say, 1 metre a 2-metre round trip time would be ~5200uS. If you don't see a return within this time, you assume no car is present.

It's still 5mS which is non-trivial when muxing seven-segs but you could interleave segment mux calls in between each distance checks.

I'd be very inclined to use servos for the gates, not steppers.