Pages: [1]   Go Down
Author Topic: Ping+Blink+Sweep=headache  (Read 1269 times)
0 Members and 1 Guest are viewing this topic.
Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks & Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

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);
  }


  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

  }

}
Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
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.
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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()
{
  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;
}
Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...119° instead of 160° ...

Actually, this is correct.

Your code does this:
  • Turn servo from 120 to 160
  • Turn servo from 160 to 119
  • Use and read Ping sensor
  • Debug to serial monitor (using the last known value of pos which is 119)
You need to do something more like:

void loop(){
  • Step servo x degrees every y millisecond
  • Use and read Ping sensor
  • Act upon reading
[/list]
  • Blink/flash LED every z millisecond
  • Toggle LED
[/list]
}


You might be interested in these libraries:
  • TimedAction
    • This will help you schedule a toggle of the LED and step servo/take readings at a set interval
  • LED
    • This will help you toggle an LED
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
« Last Edit: June 02, 2009, 09:35:42 pm by bnmorgan » Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is an example implementation using the abovementioned libraries:
[UNTESTED CODE] but it compiles.
Quote

//include libraries:
#include <Servo.h>
//TimedAction.zip
#include <TimedAction.h>
//LED.zip
#include <LED.h>

//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

const byte SCAN_FREQUENCY = 1000/NUMBER_OF_SCANS_PER_SECOND;

//pin number definitions
const byte LED_PIN = 13;
const byte SERVO_PIN = 9;
const byte PING_PIN = 7;

//TimedAction / 'thread' objects
TimedAction blinkAction = TimedAction(BLINK_FREQUENCY,blinkLED);
TimedAction scanAction = TimedAction(SCAN_FREQUENCY,scan);

//LED object
LED led = LED(LED_PIN);

//Servo object
Servo myservo;
int servoPosition = 120;       //start position
boolean scanIncrement = true;  //increase position?
byte servoIncrementValue = 10;
byte servoDecrementValue = 10;

void setup(){
  Serial.begin(9600);
  myservo.attach(SERVO_PIN);
}

void loop(){
  blinkAction.check();
  scanAction.check();
}

//when it's time, toggle led
void blinkLED(){ led.toggle(); }

//when it's time, scan area
void scan(){
  scanIncrement ? servoPosition+=servoIncrementValue : servoPosition-=servoDecrementValue; //increment or decrement current position
  if (servoPosition>160){
    scanIncrement = false;
    servoPosition = 160;
  } else if (servoPosition < 120){
    scanIncrement = true;
    servoPosition = 120;
  }
  myservo.write(servoPosition);
  long pingInches = ping();
  
  //DEBUG
  Serial.print(pingInches);
  Serial.print("in, ");
  Serial.print(servoPosition);
  Serial.println("deg");
  //END DEBUG
}

//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.
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley

Glad to be of assistance, do not hesitate to ask about anything. I'll gladly try to explain.
Logged

Tupelo, Mississippi
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino Rocks &amp; Confuses Me
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Basic Research is what I am doing when I I yet don't know what I am doing. - Wernher vonBraun

Pages: [1]   Go Up
Jump to: