Pages: [1]   Go Down
Author Topic: programming help please  (Read 953 times)
0 Members and 1 Guest are viewing this topic.
costa mesa, CA, united states
Offline Offline
Full Member
***
Karma: 0
Posts: 135
/* no comment*/
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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):
Code:
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 Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 135
/* no comment*/
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 135
/* no comment*/
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you tried your sensor alone (just the Arduino and the sensor), did it work then?
yes
Quote
How accurate are these values
very accurate
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

costa mesa, CA, united states
Offline Offline
Full Member
***
Karma: 0
Posts: 135
/* no comment*/
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to the arduino (>1year), what is a debug output?
Logged

FL
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial Output can be considered debug output
Logged

FL
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 Offline
Full Member
***
Karma: 0
Posts: 135
/* no comment*/
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, it works. smiley
Logged

FL
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Its not that I can't explain it... Its just you wouldn't understand.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not a problem, feel free to PM me if you need anymore help (or simply post again)
Logged

Pages: [1]   Go Up
Jump to: