Ping Sensor coding issue

My group and I are trying to do a really basic design for a robot. Were attempting to design a toy that can be chased around. Everything was going good and we were getting excited until we reached the sensors.

We have the code set up so that when one sensors senses something at a distance away it will cause both motors to run. We have another set of code where we cause only one motor to run when the other sensor senses something at the given distance. Where we run into an issue is when we combine the two sets of codes it prioritizes the code that comes second and causes the bot to preform whichever action we have there.

Also when neither is sensing anything the bot does nothing. I dont know if that will matter, but I figured i throw it in anyway. Any feed back would be awesome.

No help can be given unless you post your code.

You will, have course, have read http://forum.arduino.cc/index.php?topic=97455.0

const int pingPin = 2; const int pingPin1 = 3; int motionLed = 13; int motor1 = 7; int motor2 = 4;

unsigned int duration, inches, centimeters;

void setup() { pinMode(pingPin,INPUT); pinMode(pingPin1,INPUT); Serial.begin(9600); pinMode(motionLed, OUTPUT); pinMode(motor1, OUTPUT); pinMode(motor2, OUTPUT); }

void loop() { { pinMode(pingPin, OUTPUT); // Set pin to OUTPUT digitalWrite(pingPin, LOW); // Ensure pin is low delayMicroseconds(2); digitalWrite(pingPin, HIGH); // Start ranging delayMicroseconds(5); // with 5 microsecond burst digitalWrite(pingPin, LOW); // End ranging pinMode(pingPin, INPUT); // Set pin to INPUT duration = pulseIn(pingPin, HIGH); // Read echo pulse inches = duration / 74 / 2; // Convert to inches Serial.print(inches); Serial.println("in"); delay(200); } { pinMode(pingPin1, OUTPUT); // Set pin to OUTPUT digitalWrite(pingPin1, LOW); // Ensure pin is low delayMicroseconds(2); digitalWrite(pingPin1, HIGH); // Start ranging delayMicroseconds(5); // with 5 microsecond burst digitalWrite(pingPin1, LOW); // End ranging pinMode(pingPin1, INPUT); // Set pin to INPUT duration = pulseIn(pingPin1, HIGH); // Read echo pulse centimeters = duration / 74 / 2 / 2.54; // Convert to Centimeters Serial.print(centimeters); // Display result Serial.println("cm"); delay(200); } { long sensor1 = digitalRead(inches); // This is the first section of coding where the bot is suppose to turn if (sensor1 > .1){ digitalWrite(motionLed,HIGH); digitalWrite(motor1,HIGH); digitalWrite(motor2,LOW); }

long sensor2 = digitalRead(centimeters); //This is the second section of coding where the bot is suppose to go forward if (sensor2 > .1){ digitalWrite(motionLed,HIGH); digitalWrite(motor1,HIGH); digitalWrite(motor2,HIGH); } else { digitalWrite(motionLed,LOW); digitalWrite(motor1,LOW); digitalWrite(motor2,LOW); }}}

Please edit your post to include code tags. Also, please use the Auto Format in the IDE to properly format the code. It makes it a lot easier to help you.

I see several things that are wrong with your code.

You calculate inches from a ping, then, when you want to turn, you do a digitalRead(inches) and assign it to a long. Well, that's just bizarre! Presumably (I haven't checked your ping), there is a value in the inches variable. Let's say it's 32. you then assign either a 0 or a 1 to sensor1, depending on the level on pin 32. Hmmm.

You do the same thing with sensor2.

You should probably use two variables, one for each sensor, and assign the value (they can both be inches or both be centimetres), then just use those variables to decide what to do.

As for prioritizing going forward or turning, well, you have to make a decision, taking both ping values into account. You can go forward, first, or turn first, or, if you use PWM, go forward and turn at the same time. It's just a matter of what you want to do when you have the two sensor values.

http://forum.arduino.cc/index.php?topic=97455.0 If you read it, which I doubt, you chose to ignore the bit about using code tags I see.

long sensor1 = digitalRead(inches); // This is the first section of coding where the bot is suppose to turn
  if (sensor1 > .1){

Anything in a long that is positive and non-zero is > 0.1