Smart Parking

Good morning guys,
I am creating this thread for a reason. I am finishing my 12th grade at the moment and for my final project, I am doing a Smart Parking Lot controled by Arduino UNO. Basicly my doubt is I can’t finish my program. I am using a LDR to count how many cars enter the parking lot, using a barried to open and close the parking space. I will be using LEDs (5 LEDs) to mark their presence in the parking lots (5 Parking Spaces) but I can’t do the program on my own and my teachers can’t really help me with this project since they’re not specialized in Arduino and I don’t really know someone that can help me trough my project. For that matter, I am posting this here searching for help finishing my Arduino code. I can post the one I downloaded from internet, but I can’t really change it since I can’t work with Arduino very well. Can someone help me? I have posted this in many threads searching for help. I can show you guys everything you want so that you can help me. you can contact me via this thread or my e-mail, if you can help me. Thanks for your attention, sincerely yours,
Miguel Costa

Estacionamento_Inteligente_MIGUEL_COSTA.ino (1.85 KB)

const int led1 = 2;
const int sens1 = 0;
const int led2 = 3;
const int sens2 = 1;
const int led3 = 4;
const int sens3 = 2;
const int led4 = 5;
const int sens4 = 3;
const int led5 = 6;
const int sens5 = 4;
const int led6 = 7;
const int sens6 = 5;

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
}

void loop()
{
  val1 = analogRead(sens1);
  val2 = analogRead(sens2);
  val3 = analogRead(sens3);
  val4 = analogRead(sens4);
  val5 = analogRead(sens5);
  val6 = analogRead(sens6);
    
  Serial.print("Sensor 1: ");
  Serial.print(val1);
  
  Serial.print(" | Sensor 2: ");
  Serial.print(val2);
  
  Serial.print(" | Sensor 3: ");
  Serial.print(val3);
  
  Serial.print(" | Sensor 4: ");
  Serial.print(val4);
  
  Serial.print(" | Sensor 5: ");
  Serial.print(val5);
  
  Serial.print(" | Sensor 6: ");
  Serial.println(val6);
  
  if (val1 < 500) {
    digitalWrite(led1, LOW);
    delay(100);
  } else {
    digitalWrite(led1, HIGH);
    delay(100);
  }
  
  if (val2 < 400) {
    digitalWrite(led2, LOW);
    delay(100);
  } else {
    digitalWrite(led2, HIGH);
    delay(100);
  }
  
  if (val3 < 400) {
    digitalWrite(led3, LOW);
    delay(100);
  } else {
    digitalWrite(led3, HIGH);
    delay(100);
  }
  
  if (val4 < 400) {
    digitalWrite(led4, LOW);
    delay(100);
  } else {
    digitalWrite(led4, HIGH);
    delay(100);
  }
  
  if (val5 < 400) {
    digitalWrite(led5, LOW);
    delay(100);
  } else {
    digitalWrite(led5, HIGH);
    delay(100);
  }
  
 
  
  delay(1000);
  
}

You shouldn't post your email address, or it will soon be clogged with spam.

aarg:
You shouldn't post your email address, or it will soon be clogged with spam.

Fixed.

It's ok mate, I am just really desperate..

AWOL:
Fixed.

But can you help me MOD AWOL?

Uncompiled, untested.

const byte leds [] = {2, 3, 4, 5, 6, 7};
const byte sens [] = [0, 1, 2, 3, 4, 5};
const byte nSens = sizeof (leds) / sizeof (leds [0]);

void setup()
{
  Serial.begin(9600);
  for (int i = 0 i < nSens; i++)
  {
    pinMode(led [i], OUTPUT);
    digitalWrite(led [i], HIGH);
  }  
}

void loop()
{
  for (int i = 0; i < nSens; i++)
  {
    int val = analogRead(sens [i]);
    Serial.print("Sensor ");
    Serial.print (i + 1);
    Serial.print(": ");
    Serial.println(val);
    digitalWrite(led [i], val ? 500 ? LOW : HIGH);
  }  
  delay(1000);  
}

At some point, you’ll want to get rid of the “delay()”

can you give me a brief explanation about the first lines of the code?

AWOL:
Uncompiled, untested.

const byte leds [] = {2, 3, 4, 5, 6, 7};

const byte sens = [0, 1, 2, 3, 4, 5};
const byte nSens = sizeof (leds) / sizeof (leds [0]);

void setup()
{
 Serial.begin(9600);
 for (int i = 0 i < nSens; i++)
 {
   pinMode(led [i], OUTPUT);
   digitalWrite(led [i], HIGH);
 }  
}

void loop()
{
 for (int i = 0; i < nSens; i++)
 {
   int val = analogRead(sens [i]);
   Serial.print("Sensor “);
   Serial.print (i + 1);
   Serial.print(”: ");
   Serial.println(val);
   digitalWrite(led [i], val ? 500 ? LOW : HIGH);
 }  
 delay(1000);  
}


At some point, you'll want to get rid of the "delay()"

I think that rather than this:-digitalWrite(led[i], val ? 500 ? LOW : HIGH);you meant this:-digitalWrite(led[i], val < 500 ? LOW : HIGH);
And I just noticed, this:-const byte sens [] = [0, 1, 2, 3, 4, 5};needs changing to this:-const byte sens [] = {0, 1, 2, 3, 4, 5};
I’ll leave the code explanation about the first lines to the author.

Abissum:
can you give me a brief explanation about the first lines of the code?

You're right - I should've used an array of "struct", or a 2D array.
Sorry.

@OldSteve : - that's why it says "uncompiled, untested" - it was posted from my tablet, apologies.

AWOL:
@OldSteve : - that's why it says "uncompiled, untested" - it was posted from my tablet, apologies.

I make those minor errors, (and far worse), all the time too, when I'm in a hurry. No biggie. :slight_smile:

The first lines look OK as they are to me.

@Abissum, the first two lines are arrays holding the pin numbers for the LEDs and LDRs. The elements can be accessed by index.
e.g. Using 'leds[0]' will access the first element in the leds array, so 'digitalWrite(leds[0]);' would make the LED output on pin 2 HIGH.

The third line is the correct way to get the size of the arrays, (number of elements they hold).
'sizeof(leds)' returns the size of the array in bytes.
Dividing it by 'sizeof (leds [0])' divides the size of the array in bytes by the size of an element in bytes, resulting in the number of elements.

@OldSteve thank you for your help, I think I got it, thanks for your help too @MOD