How to fix "too many arguments to function" error

hey, im pretty new to coding with arduino, and i had an issue that im not sure how to solve. im trying to make a die which displays a number on a 7 segment display, and you can change the range of the random number with a button press to simulate changing the sides of the die it is "rolling" i have a function that displays a variable called "digit," but whenever the function is called it gives the error "too many arguments to function 'void displayDigit()'" on the line where i call the function. im not sure what to do about this, and id appreciate some advise. im using an arduino nano, and here is my code:

const int a = 8;
const int b = 9;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 2;
const int g = 3;

int rollState = 0; // current state of the button
const int rollPin = 10;
int dieState = 0;
const int diePin = 11;

int digit = 0;
int dieVal = 0; // counter for the number of button presses

void setup() {
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G

pinMode( rollPin , INPUT_PULLUP );
pinMode( diePin , INPUT_PULLUP );
}

void loop() {

rollState = digitalRead(rollPin);
dieState = digitalRead(diePin);
int lastDieState = dieState;
int lastRollState = rollState;

if (dieState != lastDieState){
if (dieState == HIGH){
dieVal = dieVal + 1;
if (dieVal == 10){
dieVal = 0;
}
digit = dieVal;
displayDigit(digit);
}
}
dieState = lastDieState;

if (rollState != lastRollState){
if (rollState == HIGH){
rollRandom();
rollRandom();
rollRandom();
rollRandom();
rollRandom();
rollRandom();
int numRolled = random(dieVal);
digit = numRolled;
displayDigit(digit);
}
}

}

void rollRandom(){

int rand1 = random(1);
int rand2 = random(1);
int rand3 = random(1);
int rand4 = random(1);
int rand5 = random(1);
int rand6 = random(1);
int rand7 = random(1);

if (rollState == HIGH){
if (rand1 == 0){
digitalWrite(a,HIGH);
}
if (rand2 == 0){
digitalWrite(b,HIGH);
}
if (rand3 == 0){
digitalWrite(c,HIGH);
}
if (rand4 == 0){
digitalWrite(d,HIGH);
}
if (rand5 == 0){
digitalWrite(e,HIGH);
}
if (rand6 == 0){
digitalWrite(f,HIGH);
}
if (rand7 == 0){
digitalWrite(g,HIGH);
}
delay(500);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
}

void displayDigit()
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);

//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);

//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);

//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);

//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);

//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);

//Conditions for displaying segment g
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);

}

This function

does not take arguments.

Change

to

displayDigit();

Use code tags.

1 Like

You call the function displayDigit with:

      displayDigit(digit);

but displayDigit is declared as:

void displayDigit()

(i.e. it takes no parameters.)

The function references the variable "digit" - e.g.

.
.
.
  //Conditions for displaying segment a
  if (digit != 1 && digit != 4)
.
.
.

which you have declared as a global variable.

You can just remove the "digit" from the function call:

      displayDigit();

or keep it that way, and call it like:

void displayDigit(int digit)
{
...}

displayDigit(dieVal);

It's a nice segue to functional programming.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.