Auto parking car

I have 4 ultrasonic sensors
Front back right left
4 motors
Each 2 on the same side are connected together on the same polarity ( left motors move rythemtically alone and so do right ones )
The car is supposed to be park in the garage attached below only in the case of one (P) is empty
And if all occupied it should out
All description are in the pictures -> the garage & project
4 ultrasonic are connected on pins from 13 to 6
H-Bridge is as follows ( which control the motors )
Forward and reverse of the left and right motors on pins from 5 to 2
NB. : RR --> Right reverse LF --> left forward
I'm using an adapter 9 volt 0.6 Amp for H-Bridge

The code <
#include <NewPing.h>
#define TRIGGER_PIN1 13
#define ECHO_PIN1 12
#define MAX_DISTANCE1 200
NewPing sonar1 ( TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1);
int d1;
void Read () ;
#define TRIGGER_PIN2 11
#define ECHO_PIN2 10
#define MAX_DISTANCE2 200
NewPing sonar2 ( TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);
int d2;

#define TRIGGER_PIN3 9
#define ECHO_PIN3 8
#define MAX_DISTANCE3 200
NewPing sonar3 ( TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE3);
int d3;

#define TRIGGER_PIN4 7
#define ECHO_PIN4 6
#define MAX_DISTANCE4 200
NewPing sonar4 ( TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE4);
int d4;

int in1LF = 2;
int in2LR = 3;
int in3RR = 4;
int in4RF = 5;

void setup() {

pinMode(in1LF, OUTPUT);
pinMode(in2LR, OUTPUT);
pinMode(in3RR, OUTPUT);
pinMode(in4RF, OUTPUT);
Serial.begin (9600);
}

void loop()
{
Read();
if ((d1 < 10 && d3 < 15 && d4 < 15) || (d2 < 5 && d3 < 15 && d4 < 15)) {
digitalWrite (in1LF, LOW);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, LOW);
}
else if (d1 > 10) {
digitalWrite (in1LF, HIGH);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, HIGH);
if (d3 < 55 && d3 > 27) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1) {
digitalWrite(in1LF, HIGH);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
Read ();
if (d1 > 20 && d3 > 40)
{ break;
}

}
}
else if (d4 < 55 && d4 > 27) {

digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, HIGH);
digitalWrite(in2LR, HIGH);
Read ();
if (d1 > 20 && d4 > 40)
{ break;
}
}
}
}
else if (d1 < 10 && d2 > 70 && d3 < 15 && d4 < 15)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, HIGH);
Read ();
if (d2 > 10)
{ break;
}
}
}
}
void Read()
{
int uS1 = sonar1.ping();
Serial.print("pingF: ");
Serial.print(uS1 / US_ROUNDTRIP_CM);
Serial.println("cm ");
d1 = uS1 / US_ROUNDTRIP_CM;

int uS2 = sonar2.ping();
Serial.print("pingB ");
Serial.print(uS2 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d2 = uS2 / US_ROUNDTRIP_CM;

int uS3 = sonar3.ping();
Serial.print("pingR: ");
Serial.print(uS3 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d3 = uS3 / US_ROUNDTRIP_CM;

int uS4 = sonar4.ping();
Serial.print("pingL: ");
Serial.print(uS4 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d4 = uS4 / US_ROUNDTRIP_CM;
}

Here are the OP's pix:

khaledbosha:
Void loop makes a problem
I need a function or a way .... without looping

So, keep loop() empty and everything run just once in setup().

khaledbosha:
How can i keep loop () empty

void setup()
{
// have stuff here
}

void loop()
{
}

khaledbosha:
And should i use if or if else condition

How can anyone answer that?

Post the code you have written so far, with specific questions about what's not working properly.

khaledbosha:
Will it works if i put it in void setup normally and does my commands as wanted ?

I'm not going to speculate about your code when you haven't posted any yet. You need to write some code and test it on your car: if it doesn't do what you want, come back with specific questions.

PS: you probably don't really want an empty loop(): you'll need to be continually reading sensors and making subtle adjustments to the motors, and that's exactly why loop() is there.

khaledbosha:
Void loop makes a problem
I need a function or a way where it follows a sequence of orders respectively without looping

The function called loop() is an essential element of almost every program.

Rather than thinking about trying to avoid using loop() you should read up about "State Machine". The idea is that you have one or more variables that keep track of the state of your system and use that information to decide what should happen next.

...R
Planning and Implementing a Program

This is the only thing I can bring to mind right now. It is the control part of a demo system for model railway points (turnouts) and signals

It communicates with another board that contains the points and signals and which also has an LDR to detect the presence of a train on the point. The variable pointOccupied will have the value 'C' if the point is clear.

// python-build-start
// action, upload
// board, arduino:avr:mega:cpu=atmega2560
// port, /dev/ttyACM0
// ide, 1.6.3
// python-build-end


#define rightSignalSwitchPin 2
#define leftSignalSwitchPin 3
#define errorLedPin 4
#define pointSwitchPin 5

byte pointSwitchState = 0b000; // R = 0b000, L = 0b100
char rightSignalSwitchState = 0b00; // N = 0b00 or F = 0b10
char leftSignalSwitchState = 0b0; // N = 0b0 or F = 0b1
char pointOccupied = 'C';

byte psCurrPositions = 0b000;
byte psProposedPositions = 0b000;

bool conflictError = false;

unsigned long currMillis;
unsigned long prevFlashMillis;
unsigned long flashIntervalMillis = 300;
unsigned long prevSendMillis;
unsigned long sendIntervalMillis = 100;

unsigned long debugIntervalMillis = 1000;
unsigned long prevDebugMillis;

const byte numChars = 90;
char receivedChars[numChars];
boolean newData = false;

//====================

void setup() {
	Serial.begin(500000);
	Serial.println("Starting SignalCabin.ino");

	Serial1.begin(9600);

	pinMode(pointSwitchPin, INPUT_PULLUP);
	pinMode(rightSignalSwitchPin, INPUT_PULLUP);
	pinMode(leftSignalSwitchPin, INPUT_PULLUP);
	pinMode(errorLedPin, OUTPUT);
	digitalWrite(errorLedPin, HIGH);
	delay(500);
	digitalWrite(errorLedPin, LOW);
}

//====================

void loop() {
	currMillis = millis();
	flashErrorLed();
	readSwitches();
	checkForConflict();
	transmitData();
	recvWithStartEndMarkers();
	sendDebugMsg();
	//~ Serial.println();
	//~ delay(100);
}

//====================

void readSwitches() {
	pointSwitchState = 0b100; // L
	if (digitalRead(pointSwitchPin) == LOW){
		pointSwitchState = 0b000; // R
	}
	rightSignalSwitchState = 0b00; // ON
	if (digitalRead(rightSignalSwitchPin) == HIGH) {
		rightSignalSwitchState = 0b10; // OFF
	}
	leftSignalSwitchState = 0b0; // ON
	if (digitalRead(leftSignalSwitchPin) == HIGH) {
		leftSignalSwitchState = 0b1; // OFF
	}
	psProposedPositions = pointSwitchState + rightSignalSwitchState + leftSignalSwitchState;
	//~ Serial.print("Switches PRL ");
	//~ Serial.print(psProposedPositions + 0b1000, BIN);
}

//====================

void checkForConflict() {
	if (newData == true) {
			pointOccupied = receivedChars[0];
			//~ pointOccupied = 'C'; // just for testing
			newData = false;
		}
	if (psProposedPositions == psCurrPositions) {
			// no change
		conflictError = false;
	}
	if (psProposedPositions != psCurrPositions && pointOccupied == 'O') {
			// can't change anything if point occupied
		//~ Serial.println("Occupied");
		conflictError = true;
	}
	if (psProposedPositions != psCurrPositions && pointOccupied == 'C') {
		conflictError = true;
		switch (psCurrPositions) {
			case 0b000:
				if (psProposedPositions == 0b001 || psProposedPositions == 0b100) {
					conflictError = false;
				}
			break;
			case 0b001:
				if (psProposedPositions == 0b000) {
					conflictError = false;
				}
			break;
			case 0b100:
				if (psProposedPositions == 0b110 || psProposedPositions == 0b000) {
					conflictError = false;
				}
			break;
			case 0b110:
				if (psProposedPositions == 0b100) {
					conflictError = false;
				}
			break;
		}
	}
	if (conflictError == false) {
		psCurrPositions = psProposedPositions;
	}
}



//====================

void flashErrorLed() {
	if (conflictError == false) {
		digitalWrite(errorLedPin, LOW);
		return;
	}
	if (currMillis - prevFlashMillis >= flashIntervalMillis) {
		prevFlashMillis = currMillis;
		digitalWrite(errorLedPin, ! digitalRead(errorLedPin));
	}
}

//====================

void transmitData() {
	if (currMillis - prevSendMillis >= sendIntervalMillis) {
		Serial1.print('<');
		Serial1.print(psCurrPositions + 0b1000, BIN);
		Serial1.println('>');
		//~ Serial.print('<');
		//~ Serial.print(psCurrPositions + 0b1000, BIN);
		//~ Serial.print('>');
	}
}

//====================

void recvWithStartEndMarkers() {
	static boolean recvInProgress = false;
	static byte ndx = 0;
	char startMarker = '<';
	char endMarker = '>';
	char rc;

	if (Serial1.available() > 0) {
		while (Serial1.available() > 0 && newData == false) {
			rc = Serial1.read();

			if (recvInProgress == true) {
				if (rc != endMarker) {
					receivedChars[ndx] = rc;
					ndx++;
					if (ndx >= numChars) {
						ndx = numChars - 1;
					}
				}
				else {
					receivedChars[ndx] = '\0';
					recvInProgress = false;
					ndx = 0;
					newData = true;
					//~ Serial.print(receivedChars);
				}
			}

			else if (rc == startMarker) {
				recvInProgress = true;
			}
		}
	}
}

//===============

void sendDebugMsg() {
	if (millis() - prevDebugMillis >= debugIntervalMillis) {
		prevDebugMillis += debugIntervalMillis;
		Serial.print("Point switch "); Serial.println(pointSwitchState);
		Serial.print("Switches PRL ");
		Serial.println(psProposedPositions + 0b1000, BIN);
		Serial.print("Current  PRL ");
		Serial.println(psCurrPositions + 0b1000, BIN);
	}
}

...R

OP have you written any code at all? Have you run the examples?

You need to at least read one of your sensors and turn a motor off and on and perhaps reverse as you bring your hand close or move it away.

BTW, one thing you didn't mention was how the motors will be connected to Arduino. Do you have a motor driver of some kind?- you will need to be able to reverse the motors I'm sure, which will require an h-bridge.

And another thing: have you written down the steps involved in parking your car? You can't dive into programming before you design the algorithm. You need to define the logic, so if this sensor says x and another sensor says y, that means one motor must do p and the other one do q.

You have to have that clear first, written down as some kind of flowchart or state diagram, or even in words. This is not a trivial problem.

Write down the things you do in a real car when you park....

Once you have some simple on/off code going, perhaps start with the car next to a wall and moving forward. Then if it sees the wall "disappear" that means an empty parking bay, so you make a note of that, ie move into "park mode" from "search mode", go past the bay until the wall re-appears, and go forward a bit more until it's time to reverse swing into the bay. But then keep an eye out the back so you don't hit the kerb and then swing the front in.

Don't even think about following the course in that map until you can park the car in a bay.

Nobody here will provide you with "a code" as you call it, but will help with specific questions. If you do want "a code" as a turnkey solution, you need to be in Gigs and Collaborations and reach for your wallet.

But presumably this is a uni and/or competition project and needs to be your own work?

khaledbosha:
What's wrong with this code guys ?
Should it make it work properly?

Does it work properly? Presumably not, else you wouldn't be asking.

Assuming it's not working properly, then tell us:

  • What it's supposed to do ("Park the car" is wrong answer... explain your approach to what it's measuring, what each of those measurements means (eg an empty bay, backed up too far etc etc) and what it's supposed to do with those measurements
  • What it's doing correctly
  • What it's doing wrong
  • What it's not doing at all ("Not working" is wrong answer)

Nobody has time to guess how the motors are connected, so you should give a schematic, and explain what pins like in1LF are connected to. What driver chip/shield/board is in there?

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

@khaledbosha, do not cross-post. Other thread removed.

Stop cross-posting, start using code tags

So what's the question?

Are you expecting anyone (who almost certainly doesn't have your hardware and certainly not a built model of your garage) to walk through that code and say "Yup- that'll work?"

You tell us how well the code works or doesn't, and ask for help on specific problems.

See my replies #11 and #13.