need help with code

hey guys i need help fixing the code that i am using. basically my project i am working on is a autonomous robot that detects when it gets close to a wall and then turns right. i am using a ping sensor as my distance sensor and i have two motors controlling two wheels. down below is the code that i have now but for some reason it doesn't work. i would like some input as to where do i take it from here or what did i do wrong and need to fix. thank you in advance for your time and help

int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 10; // pin for left motor reverse
int motorpinleft = 11; // pin for left motor forward
int motorpinrevright = 5; // pin for right motor reverse
int motorpinrevleft = 6; // pin for right motor forward


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------
}

void loop() {



/* Start Scan
* --------------------------------------------------
*/{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse
* -------------------------------------------------------------------
*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Lite up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
delay(50); //LED on for 50 microseconds
digitalWrite(ledPin, LOW);
}

/* Delay of program
* -------------------------------------------------------------------
*/

delay(100);

}
/* Action based on data
* -------------------------------------------------------------------
*/
{
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
}
if (ultrasoundValue > 800)

{
/* Drive straight forward 
*-----------------------------------------------
*/
analogWrite(motorpinleft, 255); //100% speed 
analogWrite(motorpinright, 255); //100% speed0
}
/*------------------------------------------------
*/
else
/* Turn hard right
*---------------------------------------------
*/
{
analogWrite(motorpinleft, 0); //stop left motor 
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevright, 0); // stop right rev motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 255); //100% speed
analogWrite(motorpinleft, 255); //100% speed
delay(380); //380 milliseconds
analogWrite(motorpinrevright, 0); // off 
analogWrite(motorpinleft, 0); // off

/*----------------------------------------------
*/
}


}

Moderator edit: CODE TAGS.

hile(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

pulseIn would do the job better.

it doesn't work

Grrrrr.

I attached 3 files that I found that work for the 4-wire ultrasonic sensors, at least the one I have.

Create a directory in your arduino.x.x.x/libraries folder named Ping4 and copy the attached files there

in sketch:

#include <Ping4.h>

int TriggerPin = 12;
int EchoPin = 11;

Ping4 Sensor;

void setup() {
  pinMode(TriggerPin, OUTPUT);
  pinMode(EchoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  Sensor.Read();
  Serial.println(Sensor.Distance());
  delay(1000);
}

Moderator edit: The usual. Grrrr

Ping4.h (203 Bytes)

Ping4.cpp (325 Bytes)

keywords.txt (47 Bytes)

sorry i wasn't specific about which ping sensor i have. i have a 3 pin ping sensor by parallax not 4 pin........ which I'm sure makes a difference with the code..... once again thanks in advance for all help received

deadlywingz:
sorry i wasn't specific about which ping sensor i have. i have a 3 pin ping sensor by parallax not 4 pin........ which I'm sure makes a difference with the code..... once again thanks in advance for all help received

You also weren't specific on what "it doesn't work" means.

Read the "Read this before Posting a Programming Question" thread at the top of this forum.

you are correct let me be a little bit more precise. well the code for some reason the ping sensor starts to work but then stops working. like you see the vehicle moving forward but it keeps moving and never stops. then if you reset and try the code while the vehicle is facing the wall it will turn right but once it clears the wall it stops working again untill the next reset. when i say reset i mean reset the arduino board

BUMP....... any help is appreciated

I haven't taken the time to review your whole sketch, but it seems to me there are three parts to your problem and you could test them separately to see which ones work and which ones don't.

You should be able to read the distance sensor repeatedly and see the return value change sensibly as you move obstacles around in front of the sensor. Just use Serial.println() to output the sensor results via USB.

You should be able to start and stop the motors and run them forward and backward separately and together. Just program a fixed sequence and confirm that the motors both work in both directions and you don't have any electrical problems causing the board to reset or crash.

In order to minimise the changes needed between your test code and your final sketch I suggest you encapsulate these separate operations in 'C' functions (for example forwards(), turnRight(), stop(), measureDistance()).

You need an algorithm to decide what the motors need to do based on the sensor input. This is the last part to test and relies on the other two parts already working. The code here will be relatively simple, but you should aim to structure it so that your driving algorithm is separated from your functions that interface to the sensor and motors and don't let the separate bits of code all get jumbled together.