Go Down

Topic: need help with code (Read 454 times) previous topic - next topic

deadlywingz

Nov 15, 2012, 04:27 pm Last Edit: Nov 15, 2012, 04:32 pm by AWOL Reason: 1
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


Code: [Select]

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.
i am a noob and need serious help

AWOL

#1
Nov 15, 2012, 04:33 pm Last Edit: Nov 15, 2012, 04:35 pm by AWOL Reason: 1
Code: [Select]
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.

Quote
it doesn't work

Grrrrr.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

NobleNoob

#2
Nov 15, 2012, 07:16 pm Last Edit: Nov 15, 2012, 07:23 pm by AWOL Reason: 1
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:

Code: [Select]
#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

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
i am a noob and need serious help

Arrch


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.

Go Up