0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« on: January 24, 2010, 06:50:10 pm » |
Well i am new to programming in C. But I am trying to learn as much as i can. What i am trying to do is run a ping distance sensor to judge distance to nearest object and if the distance is < 40 cm, output to servos. the actual servo values are bogus and just used in a testing rig to watch for movement. here's the code /* Ping))) Sensor This sketch reads a PING))) ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor. The circuit: * +V connection of the PING))) attached to +5V * GND connection of the PING))) attached to ground * SIG connection of the PING))) attached to digital pin 7
created 3 Nov 2008 by David A. Mellis modified 30 Jun 2009 by Tom Igoe
*/
// this constant won't change. It's the pin number // of the sensor's output:
#include <Servo.h>
Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo Servo myservo3; // create servo object to control a servo const int pingPin = 53;
void setup() { // initialize serial communication: Serial.begin(9600); {myservo1.attach(31); myservo2.attach(32); myservo3.attach(33); } } 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);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(1000); }
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. 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; }
void loop() { if (cm > 15 && cm < 40 )
{ myservo3.write(90); myservo2.write(90); myservo1.write(90); delay(1000); } { myservo3.write(190); myservo2.write(175); myservo1.write(175); delay(1000); } { myservo3.write(10); myservo2.write(5); myservo1.write(5); delay(1000); } else myservo3.write(90); myservo2.write(90); myservo1.write(90); delay(1000); } }
Any suggestions would be greatly appriciated :-?
|
|
|
|
« Last Edit: January 24, 2010, 07:14:06 pm by jenius8477 »
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #1 on: January 24, 2010, 07:09:50 pm » |
Hello! Try using thw - button when posting code.
It looks like you're trying to have two loop()s, that won't work. Please edit your post, highlight all code and press - .

|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #2 on: January 24, 2010, 07:23:14 pm » |
thanks for that catch, any suggestion on what to do?
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #3 on: January 24, 2010, 07:34:06 pm » |
/* Ping))) Sensor This sketch reads a PING))) ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor.
The circuit: * +V connection of the PING))) attached to +5V * GND connection of the PING))) attached to ground * SIG connection of the PING))) attached to digital pin 7
created 3 Nov 2008 by David A. Mellis modified 30 Jun 2009 by Tom Igoe
*/
// this constant won't change. It's the pin number // of the sensor's output:
#include <Servo.h>
Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo Servo myservo3; // create servo object to control a servo const int pingPin = 53;
void setup() { // initialize serial communication: Serial.begin(9600); {myservo1.attach(31); myservo2.attach(32); myservo3.attach(33); } } 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);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(1000);if (cm > 15 && cm < 40 )
{ myservo3.write(90); myservo2.write(90); myservo1.write(90); delay(1000);
} { myservo3.write(190); myservo2.write(175); myservo1.write(175); delay(1000);
} { myservo3.write(10); myservo2.write(5); myservo1.write(5); delay(1000);
} else myservo3.write(90); myservo2.write(90); myservo1.write(90); delay(1000); } }
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. 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; }
You can try this.  What I did: Copy all code from the below loop, paste it in at the bottom of the above loop.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #4 on: January 24, 2010, 07:41:11 pm » |
well i tried the code you posted and it gave the error that there was no if before else. after removing the else statement. code compiled. but i would like to use the else statement later on. any help?
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #5 on: January 24, 2010, 07:52:04 pm » |
you need an explicit if() before all your {} for it to work like you expext it is no compile error to omit them (if you have no else) but since you had an else you need something like: if(){ } else if(){ } else { } not if(){ }{ }{ }else{ } 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #6 on: January 24, 2010, 08:05:24 pm » |
you are a lifesaver.........pineapple flavor haha. code is working fine, and i have a better understanding of how the code works. thank you . any other suggestions you may have. lay them on
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #7 on: January 24, 2010, 08:22:30 pm » |
how can i impliment a counter so that the sensor will scan say, 250 times in between the servo movement. the code has developed a little bit so here it is /* Ping))) Sensor This sketch reads a PING))) ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor. The circuit: * +V connection of the PING))) attached to +5V * GND connection of the PING))) attached to ground * SIG connection of the PING))) attached to digital pin 7
http://www.arduino.cc/en/Tutorial/Ping created 3 Nov 2008 by David A. Mellis modified 30 Jun 2009 by Tom Igoe
*/
// this constant won't change. It's the pin number // of the sensor's output: #include <Servo.h>
Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo Servo myservo3; const int pingPin = 53;
void setup() { // initialize serial communication: Serial.begin(9600); myservo1.attach(31); myservo2.attach(32); myservo3.attach(33); }
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: myservo3.write(10); 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);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(10); myservo3.write(190); 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);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(10); myservo3.write(90); 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);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(10); if (cm < 40){ myservo2.write(90); myservo1.write(90); delay(1000); myservo2.write(175); myservo1.write(175); delay(1000); myservo2.write(50); myservo1.write(50); delay(1000); }
}
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; }
here it is
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #8 on: January 25, 2010, 12:06:52 am » |
can you do an if inside of an if?
|
|
|
|
|
Logged
|
|
|
|
|
MD, USA
Offline
God Member
Karma: 1
Posts: 663
A jack of all trades and a master of none!
|
 |
« Reply #9 on: January 25, 2010, 12:40:27 am » |
yes.
you can do a for() inside a for() or if(). You can use any number of control structure commands inside of each other as you need. Just be sure to have the syntax correct. Remember you need the colosing curly bracket for each one you start.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #10 on: January 25, 2010, 12:46:36 am » |
Thank you very much, im pluging along and now i have a working code that needs a few tweaks. here is the code i am working on right now #include <Servo.h>
Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo Servo myservo3; const int pingPin = 53;
void setup() { Serial.begin(9600); myservo1.attach(31); myservo2.attach(32); myservo3.attach(33); } void loop() { long duration, inches, cm, inches1, cm1, inches2, cm2, inches3, cm3; myservo3.write(90); pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(10); if (cm < 40){ myservo3.write(10); delay(500); pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); inches1 = microsecondsToInches(duration); cm1 = microsecondsToCentimeters(duration); Serial.print(inches1); Serial.print("in, "); Serial.print(cm1); Serial.print("cm"); Serial.println(); delay(10); myservo3.write(180); delay(500); pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); inches2 = microsecondsToInches(duration); cm2 = microsecondsToCentimeters(duration); Serial.print(inches2); Serial.print("in, "); Serial.print(cm2); Serial.print("cm"); Serial.println(); delay(10); myservo.write(90); delay(500) pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); inches3 = microsecondsToInches(duration); cm3 = microsecondsToCentimeters(duration); Serial.print(inches3); Serial.print("in, "); Serial.print(cm3); Serial.print("cm"); Serial.println(); delay(10); if (cm1<cm2) { myservo2.write(10); myservo1.write(175); delay(500); } if (cm1>cm2){ myservo2.write(10); myservo1.write(10); delay(1000); }
} }
long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
|
|
|
|
|
Logged
|
|
|
|
|
|