costa mesa, CA, united states
Offline
Full Member
Karma: 0
Posts: 133
/* no comment*/
|
 |
« on: May 07, 2012, 06:06:32 pm » |
Hi, i am programming a small two wheel car using an arduino. here is the code for making it turn if an object comes too close to it (pin 3 and 5 are the motors): const int pingPin = 2;
void setup() { pinMode(3,OUTPUT); pinMode(5,OUTPUT); }
void loop() { long duration, inches, cm;
pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); delay(10);
pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); if(inches>4){ digitalWrite(3,HIGH); digitalWrite(5,HIGH); } else{ digitalWrite(3,LOW); digitalWrite(5,HIGH); delay(7600); }
} long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
The car just spins around in a circle and does not turn when I want it to. any help is needed. Thanks in advance, jared
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 69
Posts: 3264
|
 |
« Reply #1 on: May 07, 2012, 06:57:55 pm » |
Have you tried writing the measured distance to the serial interface? Maybe you should try your distance sensor before installing it on your robot. I suppose you got most of the code from an example for the distance sensor, didn't you? What sensor are you using?
|
|
|
|
|
Logged
|
|
|
|
|
costa mesa, CA, united states
Offline
Full Member
Karma: 0
Posts: 133
/* no comment*/
|
 |
« Reply #2 on: May 07, 2012, 09:01:43 pm » |
Maybe you should try your distance sensor before installing it on your robot. I suppose you got most of the code from an example for the distance sensor, didn't you? Yes and sort-of. This is the distance sensor i am using: http://www.makershed.com/product_p/mkpx5.htm
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 69
Posts: 3264
|
 |
« Reply #3 on: May 08, 2012, 03:50:32 am » |
If you tried your sensor alone (just the Arduino and the sensor), did it work then? When writing the distance values to the serial interface, what values do you get? How accurate are these values (hold your hand in front of the sensor in a known distance and note the value the sensor is measuring)?
|
|
|
|
|
Logged
|
|
|
|
|
costa mesa, CA, united states
Offline
Full Member
Karma: 0
Posts: 133
/* no comment*/
|
 |
« Reply #4 on: May 08, 2012, 03:23:15 pm » |
If you tried your sensor alone (just the Arduino and the sensor), did it work then? yes How accurate are these values very accurate
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: May 08, 2012, 03:27:15 pm » |
What does your debug output tell you is happening?
|
|
|
|
« Last Edit: May 08, 2012, 03:34:21 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
costa mesa, CA, united states
Offline
Full Member
Karma: 0
Posts: 133
/* no comment*/
|
 |
« Reply #6 on: May 08, 2012, 04:27:35 pm » |
I am new to the arduino (>1year), what is a debug output?
|
|
|
|
|
Logged
|
|
|
|
|
FL
Offline
Newbie
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
|
 |
« Reply #7 on: May 08, 2012, 04:36:09 pm » |
Serial Output can be considered debug output
|
|
|
|
|
Logged
|
|
|
|
|
FL
Offline
Newbie
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
|
 |
« Reply #8 on: May 08, 2012, 04:43:43 pm » |
const int pingPin = 2;
void setup() { pinMode(3,OUTPUT); pinMode(5,OUTPUT); // initialize serial communication: Serial.begin(9600); }
void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);
// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); if(inches>4){ digitalWrite(3,HIGH); digitalWrite(5,HIGH); } else{ digitalWrite(3,LOW); digitalWrite(5,HIGH); delay(7600); } Serial.println(inches); // debug output delay(100); }
long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }
Try that
|
|
|
|
|
Logged
|
|
|
|
|
costa mesa, CA, united states
Offline
Full Member
Karma: 0
Posts: 133
/* no comment*/
|
 |
« Reply #9 on: May 08, 2012, 05:00:48 pm » |
Thank you, it works. 
|
|
|
|
|
Logged
|
|
|
|
|
FL
Offline
Newbie
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
|
 |
« Reply #10 on: May 08, 2012, 08:31:21 pm » |
Not a problem, feel free to PM me if you need anymore help (or simply post again)
|
|
|
|
|
Logged
|
|
|
|
|
|