Error "Variable or field 'blinkNum' declared void"

I’ve spent much of today trying in vain to get this sketch working. I’m not completely surprised that it fails as I’m a relative Arduino novice and it includes two firsts:

  • incorporates my first function, albeit a trivially simple one
  • first attempt to define a variable using a switch set in SETUP mode

Uploading the program gives the error message: “Variable or field ‘blinkNum’ declared void”

In case it offers clues, I’ve also attached the full error message.

/* Blink an LED either 2 or 4 times depending on a switch at D4
  Choice is made in setup mode by setting the variable num.
  That value is then used in loop mode.
  A function blinkNum is used to blink the LED on D8
*/

//PROBLEM
// Error message:
// Variable or field 'blinkNum' declared void

const int ledPin = 8;
const int timeOn = 500;
const int timeOff = 500;
const int switchPin = 4;
int num;


void setup()
{
  Serial.begin(9600);
  Serial.println("Program now in setup");
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);

  if (digitalRead(switchPin) == HIGH)
  {
    int num = 4;
  }
  else
  {
    int num = 2;
  }
  Serial.print("num = ");
  Serial.println(num);
  Serial.println(digitalRead(switchPin));
}

void blinkNum(num)
{
  for (int i = 0; i < num ; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(timeOn);
    digitalWrite(ledPin, LOW);
    delay(timeOff);
  }

}

void loop()
{
  Serial.print("num = ");
  Serial.println(num);
  blinkNum(num);
  delay(2000);

}

DeclaredVoid-Error-1.txt (6.33 KB)

void blinkNum(int num)

Try this. The data type must be specified in the function definition.

if (digitalRead(switchPin) == HIGH)
  {
    int num = 4;
  }
  else
  {
    int num = 2;
  }

The num variables declared in that if block are local to the setup() function. Remove the int so that you are working on the global num.

Thanks @groundFungus. If I understood you correctly I must declare num in two places: at the top, under my constants, and within the actual definition of the function?

That, and changing ‘int num = 4’ etc as you said, has indeed resolved that pesky error message, many thanks!

However, even when D4 is low (0V), on restarting I’m still getting four blinks, not two.

My latest code is this:

/* Blink an LED either 2 or 4 times depending on a switch at D4
  Choice is made in setup mode by setting the variable num.
  That value is then used in loop mode.
  A function blinkNum is used to blink the LED on D8
*/

//PROBLEM
// Error message:
// Variable or field 'blinkNum' declared void

const int ledPin = 8;
const int timeOn = 500;
const int timeOff = 500;
const int switchPin = 4;
int num;


void setup()
{
  Serial.begin(9600);
  Serial.println("Program now in setup");
  Serial.print("num = ");
  Serial.println(num);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);

  if (digitalRead(switchPin) == HIGH)
  {
    num = 4;
  }
  else
  {
    num = 2;
  }
  Serial.print("num = ");
  Serial.println(num);
  Serial.print("switchPin = ");
  Serial.println(digitalRead(switchPin));
}

void blinkNum(int num)
{
  for (int i = 0; i < num ; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(timeOn);
    digitalWrite(ledPin, LOW);
    delay(timeOff);
  }

}

void loop()
{
  Serial.print("num = ");
  Serial.println(num);
  blinkNum(num);
  delay(2000);

}

Terry

How is the switch wired? Is there a pulldown resistor connected to pin 4?

The better way to wire a switch is to wire one side to ground and the other side to a digital input set to pinMode(pin, INPUT_PULLUP). The switch will read HIGH when not pressed and LOW when pressed. Adjust the logic in the code accordingly.

And you know that the switch will only be read once as the setup() function only runs once at startup?

Thanks, that was it! I'm embarrassed that I didn't see it myself, as I've used PULLUP before.

EDIT: Was I right about having to declare num twice please?

Terry

f I understood you correctly I must declare num in two places: at the top, under my constants, and within the actual definition of the function?

No, those are 2 completely different things.

You must declare the variable, num before you use it.

The num in the function definition is just a placeholder for the function's argument, but the data type is required. That does not even have to be num. It can be anything as long as that is how it is used in the function.

The 2 are in no way related except for the name.