Arduino variable won't work as i want it to.

Hi,
I am making a arduino car, for my school project. Every component like the wheels, shield, ultrasonic sensor etc. But i am struggling with the code. I want my car to turn left when it obtains the first obstacle, Turn right for the second obstacle and stop at the third. But the code won't work properly. Can you guys maybe help me?
This is my code:

#define trigPin 12
#define echoPin 13
int analogValue = 0;
int analogValue2 = 0;
int directionValue = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}

void loop() {

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(analogValue);
Serial.println(directionValue);

if (distance <= 20){
if (directionValue = 0){
Serial.println("Dit is mijn eerste draai");
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
analogWrite(6, 150);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
analogWrite(9, 150);
analogValue = analogValue + 1;
} else if (directionValue = 1) {
Serial.println("Dit is mijn tweede draai");
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
analogWrite(6, 150);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
analogWrite(9, 150);
analogValue = analogValue + 1;
}
}

if (distance > 20){
if (analogValue > 5){
directionValue = directionValue + 1;
analogValue = 0;
} else if (analogValue <= 5)
{
analogValue = 0;
}

Serial.println("Ik hoef niet te draaien");
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
analogWrite(6, 150);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
analogWrite(9, 125);

}
}

Thanks for your effort,
Jesser

For a start, you need == in an if, not just =, eg:

  } else if (directionValue == 1) {

if (directionValue = 0){
else if (directionValue = 1) {

There is a big difference between = and ==

Use CTRL T to format your sketch.

Please read Nick Gammon's post at the top of this Forum on how to post source code using code tags. It will help us help you.

Also, anytime a variable doesn't do what you want it to, a stern scolding usually makes it behave better!

Jesser:
But the code won't work properly.

You need to tell us what it actually does.

And 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.

...R

Also, anytime a variable doesn't do what you want it to, a stern scolding usually makes it behave better!

Never works in my code. 8)

I probably messed up with the = and ==. I think that is the problem. I will do my best to solve it now.

PaulS:
Never works in my code. 8)

You need to describe to your code what "works" means to you 8)

I managed to fix the code. Thank you all for your amazing help :slight_smile:

Before posting you source code using code tags, use Ctrl-T while the code is in the IDE. It will reformat your code into a common C style, which makes it easier for us to read.