Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: Recursion Problem with Arduino on: November 28, 2010, 03:24:29 am
Thanks! MikMo , and CB.

Until now I didn't know that recursion used up memory...

I'm not used to working within small memory limits yet. And I do feel that recursion is at least the easier way if not the better way to do this program.

My original program didn't have any recursion, but I need to use PWM on the motor and still read from the Sonar Range finder. I would ideally like to do this at the same time, but we know that's impossible so within a few microseconds is the goal.

If anyone has ideas on that front let me know otherwise I'm sure I'll figure it out eventually.

LOL figured it out....

There's a reason why I should never program on a 10" screen.
2  Forum 2005-2010 (read only) / Syntax & Programs / Recursion Problem with Arduino on: November 28, 2010, 02:43:10 am
My Arduino Duemilanova will run my (semi-infinite) recursive program 929 times before freezing, crashing, etc. Does anyone know why this may be and if there is a simple way around it. (i.e not having to re-logic the code)

Any thoughts are appreciated.

Code is below if you care to look through it, there are some troubleshooting print statements in there as well as some commented out code that I'm not using ATM b/c it doesn't meet my requirements.

/* 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.
 created 3 Nov 2008
 by David A. Mellis
 modified 30 Jun 2009
 by Tom Igoe
 This example code is in the public domain.

/* Auto Brake Car */

//Global Varibles:
const int watchdog= 2, button= 3, mpower= 5, dir= 6, pingPin= 7, brakeled= 8, headlight= 9;

//function declarations
void watchdogled();
void sonar();
void motor();
long int microsecondsToCentimeters(long int microseconds);
long int microsecondsToCentimeters(long int microseconds);

//onetime setups
void setup()
  Serial.begin(9600);         // initialize serial communication:
  pinMode(watchdog, OUTPUT);
  pinMode(button, INPUT);
  pinMode(mpower, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(brakeled, OUTPUT);
  pinMode(headlight, OUTPUT);

void loop()
  Serial.println("begin driver loop");
  //watchdog call
  digitalWrite(headlight, HIGH); //turn on headlights.
  delay(3000);                   //wait for user to move away
  Serial.println("sonar call");
  Serial.println("end driver loop");

void watchdogled()
  Serial.println("begin watchdog");
  digitalWrite(brakeled, LOW);       //make sure lights are off
  digitalWrite(headlight, LOW);
  while(digitalRead(button) == HIGH) //run watchdogled while button isn't pressed
    digitalWrite(watchdog, HIGH);    //turn on LED
    for(int i=0; i<10; i++)          //waits 250ms checking every 25 for a button press
      if(digitalRead(button) == LOW) //if button pressed we return to the driver function
        digitalWrite(watchdog, LOW);

    digitalWrite(watchdog, LOW);
    for(int i=0; i<30; i++)         //waits 750ms while checking for button press
      if(digitalRead(button) == LOW)
        digitalWrite(watchdog, LOW);
  Serial.println("end watchdog");

void sonar()
  Serial.println("begin sonar funct");
  /*local varibles:
   establish variables for duration of the ping,
   and the distance result in inches and centimeters:*/
  long int duration, inches, cm;
  int stat= 0;

  // do
  // {
  /*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);
  digitalWrite(pingPin, HIGH);
  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("in, ");

  //Brake Lights and engine braking
  if (cm <= 13)
    Serial.println("if statement");
    digitalWrite(brakeled, HIGH);
    digitalWrite(dir, HIGH);
    digitalWrite(mpower, HIGH);   // a quick reverse spurt
    digitalWrite(mpower, LOW);
    return;                       // return to driver
    Serial.println("motor call");
    motor();                      // call motor to go forward.

  //}while(cm >= 13);
  Serial.println("end sonar funct");

void motor()
  Serial.println("begin motor funct");
  digitalWrite(brakeled, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(mpower, HIGH);
  //digitalWrite(mpower, LOW);
 // delay(100);
  sonar();                    // call sonar to check distance. This uses recursion to loop until car is near an object
  Serial.println("end motor funct");
long int microsecondsToInches(long int 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: */
  return microseconds / 74 / 2;

long int microsecondsToCentimeters(long int 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;

3  Forum 2005-2010 (read only) / News / Re: RGB LED Shield kit now available on: November 28, 2010, 02:42:50 am
That is really cool. hmmmm.... I will keep this in mind for any projects that I may or maynot having coming up.
Pages: [1]