Pages: [1]   Go Down
Author Topic: Too Many Arguments to Function?  (Read 2465 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
what would i do without arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey everybody,
I've been working on a project to make noise with an arduino by hooking it up to a speaker and some pots. Email me if you're interested in the full code, but here is the part that's effected:
Code:
int waitTable[] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};

int waitFunction()
{
  waitVal = analogRead(waitPot);
  if(waitVal >=0 && waitVal <=255)
  {
    if(waitVal <= 25)
    {
      waitFinal = waitTable[0];
      return(waitFinal);
    }
    else if(waitVal > 25 && waitVal <= 50)
    {
      waitFinal = waitTable[1];
      return(waitFinal);
    }
    else if(waitVal > 50 && waitVal <= 75)
    {
      waitFinal = waitTable[2];
      return(waitFinal);
    }
    else if(waitVal > 75 && waitVal <= 100)
    {
      waitFinal = waitTable[3];
      return(waitFinal);
    }
    else if(waitVal > 100 && waitVal <= 125)
    {
      waitFinal = waitTable[4];
      return(waitFinal);
    }
    else if(waitVal > 125 && waitVal <= 151)
    {
      waitFinal = waitTable[5];
      return(waitFinal);
    }
    else if(waitVal > 151 && waitVal <= 177)
    {
      waitFinal = waitTable[6];
      return(waitFinal);
    }
    else if(waitVal > 177 && waitVal <= 203)
    {
      waitFinal = waitTable[7];
      return(waitFinal);
    }
    else if(waitVal > 203 && waitVal <= 229)
    {
      waitFinal = waitTable[8];
      return(waitFinal);
    }
    else if(waitVal > 229)
    {
      waitFinal = waitTable[9];
      return(waitFinal);
    }
  }
}
When I try to compile the program, the following error pops up:

 In function 'void loop()':
error: too many arguments to function 'int waitFunction()'


Is there a more efficient way to write this code? I'm making sound by pulsing a speaker HIGH and then LOW with an extremely small delay measured in another function, but the waitFunction() allows me to separate strings of noise into beats. I could just set the delay directly proportional to the input from the waitPot, but I'd have it this way if possible. Am I missing an obvious solution that will let me keep my code's functionality the same?

(the_downsmeister)
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

downsmeister--

First of all, the error message is from where you are *calling* the function, something that we can't see with your post.

It looks like you are trying to scale a range from 0-255 to 0-1000?  If so, you can just write waitFunction like this:

Code:
int waitFunction()
{
    int waitVal = analogRead(waitPot);
    return waitVal * 1000 / 256;
}

If you really want just values that are divisible by 100, you can modify this to:

Code:
int waitFunction()
{
    int waitVal = analogRead(waitPot);
    waitVal = waitVal * 1000 / 256;
    return 100 * (waitVal / 100);
}

Make sure when you call it you are not trying to pass any parameters.

Mikal
« Last Edit: February 07, 2009, 12:24:40 am by mikalhart » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
what would i do without arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey mikalhart-
thanks for the help. Your code is not what I want to accomplish; I want the beats delay to be only those 10 lengths, not every length in between. When you have waitFunction()
Code:
   return 100 * (waitVal / 100);

waitVal remains the same. Dividing waitVal by 100 and then multiplying it by 100 returns what was on the line above,
Code:
   waitVal = waitVal * 1000 / 256;
because in effect your multiplying it by 1.

Even though your code samples weren't exactly what I was looking for, you helped solve the problem; I WAS passing values into the functions where you couldn't see them:
Code:
void loop()
{
  waitVal = analogRead(waitPot);
  waitFunction(waitVal);
  pitchVal = analogRead(pitchPot);
  freqFunction(pitchVal);
  digitalWrite(speakerPin, HIGH); // pulses the speaker HIGH (on)
  delayMicroseconds(freqDelay); // waits for pitchDelay microseconds
  digitalWrite(speakerPin, LOW); // pulses the speaker LOW (off)
  delayMicroseconds(freqDelay); // waits for pitchDelay microseconds
  delay(waitFinal);
}

Removing the waitVal from waitFunction() and pitchVal from freqFunction() let the code compile perfectly. Thanks so much for the help.

(the_downsmeister)

Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int waitVal;
...
return 100 * (waitVal / 100);

This is NOT identical to "multiplying by 1."

The variable is an integer.  It cannot store fractional parts.  The math done on an integer variable is done only with integers, and so any fraction is immediately discarded.

Example:  if waitVal were 356, then waitval / 100 would result in 3, not 3.56.  Then 3 * 100 is 300.

The formula above just helped you round DOWN to the nearest 100.
« Last Edit: February 07, 2009, 12:30:34 pm by halley » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Halley's description is quite right.  I think if you were to use my 2nd sample waitFunction(), you'd find its results (nearly) identical to those in your version, but in a much smaller package.  You've taken the time to precalculate the way the various ranges map to new values, but to my thinking, calculating is what processors are for.  Let the Arduino do the work for you.

Cheers,

Mikal
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
what would i do without arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The variable is an integer.  It cannot store fractional parts.  The math done on an integer variable is done only with integers, and so any fraction is immediately discarded.
Quote
The formula above just helped you round DOWN to the nearest 100.

Halley, thanks for the information. Sorry, Mikalhart, I didn't realize that int's can't hold fractional parts, so your code is actually perfect for what I'm trying to do. Thanks again everybody,

(the_downsmeister)
Logged

Pages: [1]   Go Up
Jump to: