Advice on arduino atmega coding for total beginner.

Good Day everyone :slight_smile:

I would like to ask some help for what we're doing right now.
A smart car park. we are using 2-wire inductive proximity sensor

what we are doing is that when a car stops at the sensor in a designated parking slot
it would trigger a LED light which would light up when occupied and then will trigger also a 7 segment
display which would increment or decrement based on the available slots left.

this is our code right now.
and it has two major problems.

the sensor won't work and the servo motor aswell.

Thank you.

//Initialize the 7 segment pins

int pos = 0;
int A = 3;
int B = 2;
int C = 4;
int D = 5;
int E = 6;
int F = 8;
int G = 9;

//Initialize the push buttons pins, push buttons states, and the counter
int switchUpPin = 12;
int switchDownPin = 13;
int counter = 0;
int buttonUpState = 0;
int lastButtonUpState = 0;
int buttonDownState = 0;
int lastButtonDownState = 0;

void setup() 
{
  
Serial.begin(9600);

pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);

}

void loop() 
{
//Getting the reads from the buttons
buttonUpState = digitalRead(switchUpPin);
buttonDownState = digitalRead(switchDownPin);

//Detecting button press and getting the button status
//Do this for the button up
if (buttonUpState != lastButtonUpState) 
{
if (buttonUpState == HIGH) 
{
//Reset the counter to -1
if(counter == 5)
{
counter = -1;

}
//Increase the counter by 1
counter++;


//Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}

//Do this for the button down
if (buttonDownState != lastButtonDownState) 
{
if (buttonDownState == HIGH) 
{
//Set the counter to 6
if(counter == 0)
{
counter = 6;
}
//Decreases the counter by 1
counter--;


////Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}
//Calling the function changeNumber with the arg counter
changeNumber(counter);
}

//The function to display the numbers
void changeNumber(int buttonPress)
{
switch (buttonPress)
{
//number 0
case 0:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
//number 1
case 1:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 2
case 2:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 3
case 3:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 4
case 4:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 5
case 5:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
}

}

the sensor won't work and the servo motor aswell.

You are not reading the sensor or driving the servo in the code you posted.

Please post what you tried and describe the problems.

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

What you are doing sounds like a big challenge for a “total beginner”. Why not start with something simple?

At the very least start with three separate programs - one to detect the sensor, one to update the display and the third to move the servo. Don’t try to make a composite program until you can do each part separately.

…R
Planning and Implementing a Program

Thank you UKHeliBob and Robin2.

we will try to make it from the start again basing on the "planning and implementing a program".
sorry the code was in mess we were basing on the sample codes from the IDE and what we see from youtube
without any fundamental knowledge on programming and hardware.

Thank you again :slight_smile:

Hi,
Welcome to the forum.

Can you post links/data to your inductive sensor?
Can you post links/data to your output display?
Can you post links/data to your servo?

What does the servo do?
What model Arduino?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

This project seems to be standard school assignment

Functionality description doesn’t mention a servo. What’s that servo got to do with it?

How many of those inductive sensors are you trying to read? There are to be at least two car parks for this to make any sense.

Get it to work in stages.

  1. get the display to work, just have it count 0 to 9 on and on. Create a function for that: call that function with the number as parameter.
  2. get one sensor to work. Just have it switch on and off the internal LED when you detect something. Quickest and easiest feedback that you read the sensor. This is probably the hardest part of the build.
  3. connect all sensors, read them, count the ones that are activated, send the number to the display (use the function from 1)). By the time you’re here you’ll know how to do this.
  4. submit the result to your teacher.