Help on my code!

Hi everyone! I am trying to make a robot with two motors and two sensors. If the sensor hits the wall it should go the opposite direction that it is currently going in. However, the motors don’t stop even when I trigger the sensors. Can someone help? Thanks so much!

int M1dirP = 4; //motor 1 direction
int M1spdP = 5; //motor 1 speed
int trigPin = 4; //switch in front connected to pin 4
int echoPin =5; //switch in back connected to pin 5
long distance;
long duration;
int spd1 = 150;
int FWD1 = HIGH;
int BKWD1 = !FWD1;
int M2dirP = 6; //motor2
int M2spdP = 7; //motor 2
int FWD2 = HIGH;
int BKWD2 = !FWD2;
int spd2 = 250; //speed = 0-255

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);//
pinMode(echoPin, INPUT);//

pinMode(M1dirP, OUTPUT);
pinMode(M1spdP, OUTPUT);
pinMode(M2dirP, OUTPUT);
pinMode(M2spdP, OUTPUT);

}

void loop() {
//for ( spd1 = 0; spd1 <spd2; spd1+10)
//{
digitalWrite(M2dirP, FWD2); //wheel = motor 2
analogWrite(M2spdP, spd2);
digitalWrite(M1dirP, FWD1); //wheel = motor 1
analogWrite(M1spdP, spd1); //analog write
digitalWrite (trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;

if (digitalRead(trigPin == HIGH) && digitalRead(echoPin == LOW)) //if the sensor hits the fall in front switch reads either 0 or 1
{
digitalWrite(M1dirP, BKWD1);
digitalWrite(M2dirP, BKWD2);
Serial.println(“Stop”);
}
else //if (digitalRead(trigPin == LOW) && digitalRead(echoPin == HIGH))//
{
digitalWrite(M1dirP, FWD1);
digitalWrite(M2dirP, FWD2);
Serial.println(“Keep going”);
}

if (digitalRead(echoPin == HIGH) && digitalRead(trigPin ==LOW)) //if sensor hits the back
{
digitalWrite(M1dirP, FWD1);
digitalWrite(M2dirP, FWD2);
Serial.println(“Stop”);
}
else //if(digitalRead(echoPin == LOW) && digitalRead(echoPin == HIGH))
{
digitalWrite(M1dirP, BKWD1);
digitalWrite(M2dirP, BKWD2);
Serial.println(“Keep going”);
}
delay(250);

}

Where have you ever seen digitalRead(trigPin == HIGH) && digitalRead(echoPin == LOW)) //if the sensor hits the fall in front switch reads either 0 or 1 being used to check hitting a wall with what I suppose is an ultrasonic sensor? What do you think

  digitalWrite (trigPin, LOW);
  delayMicroseconds(2); 
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (10);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2)/29.1;

is doing?

Also the else of this if(... && ...) is not what you document (it’s an OR not AND)

===

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Hi J-M-L, I think the goal of doing a digitalRead to see if the trigPin (sensor) is HIGH is basically when the sensor in the front of the "robot" hits, it should change directions and vice versa.

I just started coding in this language so I'm not quite sure where to start when doing this efficiently/correctly.

Thanks!

Where did you get that code? Did you write it? Do you understand it?

again Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

arduinohelp:
Hi J-M-L, I think the goal of doing a digitalRead to see if the trigPin (sensor) is HIGH is basically when the sensor in the front of the "robot" hits, it should change directions and vice versa.

I just started coding in this language so I'm not quite sure where to start when doing this efficiently/correctly.

Thanks!

distance = (duration/2)/29.1;

Should give you the distance from the sensor to the nearest object. If it is below a certain value then stop and move backwards.

The subsequent state of the two digital pins is irrelevant, but seeing these comments

int trigPin = 4; //switch in front connected to pin 4
int echoPin =5; //switch in back connected to pin 5

I wonder if you have an ultrasonic sensor at all.

So, do you have an ultrasonic sensor or just switches ?

if (digitalRead(trigPin == HIGH)Look again, closely.

Hi,
Why aren’t you using the NewPing library.

http://playground.arduino.cc/Code/NewPing

Fixes a lot of probems.

Tom… :slight_smile:

TomGeorge:
Why aren't you using the NewPing library

I am not convinced that he is using an ultrasonic sensor

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Can you post a picture of your project please, so we can see you component layout?

Thanks.. Tom.. :slight_smile:

Hi everyone, I don't have an ultrasound sensor. I just have switches. Sorry I didn't clarify that before!

And I did write the code combining random snippets I found on stack overflow so that is the reason why it won't work altogether.

combining random snippets I found on stack overflow

Not the most effective method. If you'd spend some of that time on a C++ tutorial then you might actually understand the code you find and it would be much more useful to be able to use it sensibly rather than just copy-paste and stitch it all together and hope.

combining random snippets

Stochastic programming methodologies have been largely discredited.
Except on Instructables.

@AWOL, Karma++
I learned a new word AND got a laugh over the Instructables quality.

arduinohelp:
Hi everyone, I don't have an ultrasound sensor. I just have switches. Sorry I didn't clarify that before!

And I did write the code combining random snippets I found on stack overflow so that is the reason why it won't work altogether.

Just as I thought.

So, you have a front switch and a rear switch each attached to an Arduino pin.

Give the pins sensible names so that the rest of the code is easier to read.
Write a small program that tests whether they work
I suggest that you set the pinMode()s INPUT_PULLUP and wire them to go LOW (ie to GND) when activated.
Print a message when you discover that one of them is activated

Post the code here and we can then provide help as to what to do next.
Use code tags when you post the code.

and please read a button tutorial (this one - first hit on google but there are zillions of others)