I'm trying to get a fairly simple combination of the blink example, ping example, and sweep example. I'm still very new to coding ardu electronics etc. I am learning backwards by trying to adapt code and change it to do what I want, having to learn how it does it's intended function first. I can get sweep to work, and I know ping alone is working, but when combined (as below) I get error:
In function 'void loop()':
error: 'microsecondsToInches' was not declared in this scope
I don't understand why with nothing currently but copy and paste suddenly it's not declared?
// Sweep
// by BARRAGAN <http://barraganstudio.com>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int ledPin = 13; // Led & Laser Pin
int LaserPin = 12;
int pingPin = 7;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(LaserPin, OUTPUT);
}
void loop()
{
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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(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;
}
*//
for(pos = 120; pos < 161; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(pos);
Serial.println();
digitalWrite(LaserPin, HIGH); // sets the laser on
delay(5); // waits
digitalWrite(LaserPin, LOW); // sets the laser off
delay(5); // waits
delay(490);
}
for(pos = 160; pos>=120; pos-=10); // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(500); // waits 15ms for the servo to reach the position
}
}
You had some errors in that code. You did not end loop before defining microsecondsToX functions, this will be a compile error.
Try this code:
[UNTESTED CODE]
// Sweep
// by BARRAGAN <http://barraganstudio.com>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int ledPin = 13; // Led & Laser Pin
int LaserPin = 12;
int pingPin = 7;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(LaserPin, OUTPUT);
}
void loop()
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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(100);
for(pos = 120; pos < 161; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(pos);
Serial.println();
digitalWrite(LaserPin, HIGH); // sets the laser on
delay(5); // waits
digitalWrite(LaserPin, LOW); // sets the laser off
delay(5); // waits
delay(490);
}
for(pos = 160; pos>=120; pos-=10); // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(500); // waits 15ms for the servo to reach the position
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
It is your code, I've simply deleted and added some brackets and tabs.
That got it compiling and on upload, it works mostly, but still no pinging or ping report, or at least not every step, it does one ping at the far limit reports the distance, and 119° instead of 160° where it pinged.....
.< this is really getting to me.
// Sweep
// by BARRAGAN <http://barraganstudio.com>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int ledPin = 13; // Led & Laser Pin
int LaserPin = 12;
int pingPin = 7;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(LaserPin, OUTPUT);
}
void loop()
{
for(pos = 120; pos < 161; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(LaserPin, HIGH); // sets the laser on
delay(5); // waits
digitalWrite(LaserPin, LOW); // sets the laser off
delay(5); // waits
delay(490);
}
for(pos = 160; pos>=120; pos-=1); // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(500); // waits 15ms for the servo to reach the position
}
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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(pos);
Serial.print(" deg");
Serial.println();
// Serial.print(cm);
// Serial.print("cm ");
//delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2; // .25" graduations
//}
//long microsecondsToCentimeters(long microseconds)
//{
// return microseconds / 29 / 2;
}
Small progress. Scan off for the moment, totally rewriting it, no copying this time, at least not by copy/paste.
got it pinging and blinking togather with your LED lib.
Now to see if i can figure out how to get the servo movement. I'm way over my head in code already. :-/ I don't understand how timed delay is going to play in here....
//config timing
const byte BLINK_FREQUENCY = 1000; //the led will blink every second
const byte NUMBER_OF_SCANS_PER_SECOND = 2; //change this to suit your needs
//return inches
long ping(){
long duration = 0;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(PING_PIN, OUTPUT);
digitalWrite(PING_PIN, LOW);
delayMicroseconds(2);
digitalWrite(PING_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(PING_PIN, 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(PING_PIN, INPUT);
duration = pulseIn(PING_PIN, HIGH);
return (duration / 74 / 2);
}
This will, or at least should, periodically blink the led and scan the area using the servo and the ping sensor.
Wow. Well, I was getting somewhere with just the LED.h but i was a LONG way from that bit. Works beautifully. And the easy adjustments are great. It amazes me how simple some of y'all make this stuff.
Wow. Well, I was getting somewhere with just the LED.h but i was a LONG way from that bit. Works beautifully. And the easy adjustments are great. It amazes me how simple some of y'all make this stuff.
I almost feel bad for stealing all the fun coding it, from you.
The LED and TimedAction libraries hides some Arduino API calls, the program could've been written without the use of those libraries.
But, they do simplify things, and makes for a 'prettier' code, if you ask me.
I am glad to hear that you think it is/was/looked simple! That's what the arduino is all about
Glad to be of assistance, do not hesitate to ask about anything. I'll gladly try to explain.
Don't feel bad....i'd gotten past the fun point for the night, was well into the growl stage, and rapidly approching the alt-f4 go play WoW stage.
I will dive in to make sure i understand what is going on before i get much deeper. This thing is heading toward a mobile creature sometime in the future.
Thank you again for the help. I'll surely need it again, and i'll try not to make too much of a nuisance of myself. ;D