Help with Functions.... passing parameters

Hi All,
As I mentioned in my last post! I’m new hear also new to the Arduino (Uno) and to learning C. But I have some working code, except that I have a couple of functions that worked fine until I decided to pass a variable or parameter to them, sure I’ve got it wrong somewhere along the line, but it compiles and uploads fine.

Here’s my code the 2 functions are Halt() and Back() I want to vary the delay by passing the TD value to them so it becomes delay(TD), sorry I know you don’t like the use of delay but at the minute it’s simple!! If you consider I knew nothing of C 10 days ago, I suppose I’m not doing too bad, well that’s what I think??

/* *******************************
 ;    Filename:     PololuDrive	
 ;    Date: 	    10/08/2013		
 ;    File Version: 18082913	
 ;    Written by :   Mel Saunders		
 ;    Function:	     BuggyDrive	
 ;    Last Revision:  18/08/2013
 ;    Target         Uno
 ; ******************************* 
 
 This program demonstrates the use of an L293 dual H bridge.
 This can run 2x DC motors and independantly control their speed and direction.
 */

#include <Servo.h>

Servo scanservo;     //Ping Sensor Servo
int Left=180, Centre=90, Right=0;  //Servo angle in Degrees ??Hextronic servo works back to front LEFT=180, RIGHT=0
const int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
const int PWMpin=11;  //Enable pin on L293 for PWM 
const int scanservopin = A1;   // Pin number for scan servo 
int distance,cm,FRQ,TD,A,B;
boolean go; 

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  pinMode(scanservopin, OUTPUT);
  go = true;
  scanservo.attach(scanservopin);  // Attach the scan servo to pin 11
} 

// * * * start of Main program * * * *

void loop()
{
  if (go == true)
  {
    scan();

    do
    {
      distance=scanner(cm);
      tone(12,800,15);
      delay(500);
      tone(12,800,15);
    }
    while (distance >20);

    startup();
  }

  distance=scanner(cm);
  if(distance <10)
  {
    TD=2000;
    Halt(TD);  // these are the problems, have I formatted them right
    TD=800;
    int Back(int TD);  // these are the problems, have I formatted them right
    int Halt(int TD);  //
    Radar();
  }

  {
    if(distance >=15 and distance <26)
    {
      analogWrite(PWMpin,80);
      delay(900);
    }
    if(distance >20)
    {
      digitalWrite(MA1,HIGH);       //Left motor forward
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,HIGH);      //Right motor forward 
      digitalWrite(MB2,LOW);
    }
  }  
}// end Main loop


//------------------------------------------------------------------
void startup()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);

  for (int i= 40; i <= 255; i++) //start slow, increase speed
  { 
    analogWrite(PWMpin,i);
    delay(40);
  }
  go = false;      //Set go to false
}
//-----------------------------------------------------------------
  int Halt()      //Both motors STOP  // these are the problems, have I formatted them right
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(TD);
}
//------------------------------------------------------------------
void Rturn()      //Turn right
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);       //Right motor reverse 
  digitalWrite(MB2,HIGH);
  delay(500); 
} 
//------------------------------------------------------------------
void Lturn()      //Turn left
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,HIGH);       //Right motor forward
  digitalWrite(MB2,LOW);
  delay(500); 
} 
//------------------------------------------------------------------
void Forward()      //forward
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);
}

//------------------------------------------------------------------
  int Back()      //Reverse    // these are the problems, have I formatted them right
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(TD);
}
//------------------------------------------------------------------ 
void scan()
{
  for (int i=Right; i <= Left; i++) 
  {
    scanservo.write(i); 
    delay(20);
  }
  scanservo.write(Centre); 
}
//------------------------------------------------------------------
long scanner(long cm)
{
  const int pingPin=7, EchoPin=8;
  long duration;

  // 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);
  pinMode(EchoPin, INPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW); 
  duration = pulseIn(EchoPin, HIGH);

  // convert the time into a distance
  // inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  delay(100);

  return (cm);
}
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 Radar()
{
  scanservo.write(180);
  delay(300);
  tone(12,1500,30);
  A=scanner(cm);
  delay(100);
  scanservo.write(0);
  delay(300);  
  tone(12,1500,30);
  B=scanner(cm);
  scanservo.write(90);
  delay(300);  

  if (A>B) 
  {
    Lturn();  //Then turn left
  }
  else  
  {
    Rturn();  //Then turn right
  }
  delay(100);

}

Hope you can help!

Regards
Mel.

    int Back(int TD);  // these are the problems, have I formatted them right
    int Halt(int TD);  //

That’s not how you call a function, that is how you prototype a function. Arduino IDE prototypes your functions for you, so that isn’t necessary. Look at your microsecondsToCentimeters() and scanner() functions to see how to properly implement a function with a parameter, and how to call it.

int Back(int TD);  // these are the problems, have I formatted them right

You're calling all other built in functions right but not these. Do you call digitalRead like?:

int digitalRead(int pinNumber);

No, you just call

pinMode(pinNumber);

These function calls should look like

Halt(TD);

But if you just do that it is going to complain that you don't have a matching function since the only Halt function you've defined takes no parameters.

So you need this:

 int Halt()      //Both motors STOP  // these are the problems, have I formatted them right
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(TD);
}

To look something like this:

void Halt(int delayTime)      //Both motors STOP  // these are the problems, have I formatted them right
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(delayTime);
}

You can either change the function or you can write a second one so that you have two versions, one that takes an argument and one that doesn't. That's called function overloading.

Look up C++ tutorials on functions. It really isn't hard once you've seen it explained once.

Hi All, Thanks for your help. Delta-G you seemed to have made it clearer for me, some of this code was copied! That included the 2 functions you mentioned, so I was'ent that good... I have just altered my code and it seems to do the trick, just tested the back function to vary delay works fine! I suppose I'll be a learner for some time yet? but thanks for you help always nice to have and see.

Would it be more economical with memory to do a simple function like:

sound()

tone(pinNumber, FRQ,TD) and call it each time with different arguments FRQ and TD or just do tone(pinNumber, FRQ,TD) 6 or so times, I think the former might be more proper??

Again thanks for your help and advice, I am learning and you know what they say about the man who never made a mistake!!

Kind regards

Mel.

@Mel,

Congrats on coming so far so fast… I have been following your progress.

Generally, Arduino does NOT require function prototypes because it is done automatically during the compile. Like most automatic systems, she ain’t perfect (see link at bottom.) Hence, you need to know you can roll-your-own.

I have some working code, except that I have a couple of functions that worked fine until I decided to pass a variable or parameter to them,

(Generalized)
In Arduino you can have a function that works on global memory variables, no arguments required and, if declared “void function” then nothing is returned… However, global variables can be manipulated. In this special case, the function is akin to a subroutine and return.

A function could be Boolean, comparing global variables and returning true/false.

A function could be Boolean, returning true/false based on physical states; such as if a button is pressed or not pressed.

But, as functions become more complex, it is generally accepted that the function will not always use global variables, rather it is desirable to encapsulate the function so that only passed parameters are external and every other variable is private to the function. In this manner, no global variables can be accidentally corrupted. Now, all of this function stuff happens on the uC stack - so we do not wish to pass large arrays or long strings… Stack resources are precious. So, we pass a pointer in these cases. When we pass variables, we generally cast the variable in the function parameters, like: boolean myFunct(int yVar)

You will often see programmers doing this:
boolean myFunct(int yVar)
int y = yVar;

This ensures that you can use a private variable in the function and encapsulate the process into a black box. Moving from globals, to pointers, to full encapsulation is one part of generating a library function.

Ray

Edit: added link