Why is this extra variable declaration needed?

I eventually got my program working with the full code shown below.

// Friday 2 September 2022
// 1. 14 V heavy duty supply for solenoid; converted to 5 V for 328 etc.
// 2. Circuit places 0-5 V from a 10k preset on analog pin A0.
// 3. Based on that value, delivers various numbers of pulses from pin D9
// 4. Via an NPN transistor and 5V relay these activate the solenoid.
// 5. After last pulse all apparent activity stops until next power-up.

const int onPin = 9;
const int tOn = 100;
const int tOff = 200;
const int analogPin = A0;

// For initial configuration and testing
int period = tOn + tOff; // ms
int f = 1000 / period; // Frequency (Hz)

int result;

// Function to deliver chosen number of pulses
int pulsing (int result)
// But int pulsing(result) caused the error described
// ***************************************************
{
  for (int count = 0; count < result; count++)
  {
    digitalWrite(onPin, HIGH);
    delay(tOn);
    digitalWrite(onPin, LOW);
    delay(tOff);
  }
}

void setup() //
{
  Serial.begin(115200);
  Serial.println(F("CanShaker-5a"));
  Serial.print(F("tOn = "));
  Serial.println(tOn);
  Serial.print("tOff = ");
  Serial.println(tOff);
  Serial.print(F("period = "));
  Serial.println(period);

  Serial.print(F("f = "));
  Serial.println(F(" Hz"));

  pinMode(onPin, OUTPUT);

  int reading = analogRead(analogPin); // 0-1023
  int result;

  Serial.print(F("reading = "));
  Serial.println(reading);


  // Based on reading from preset, calculate how many pulses required
  // ( 5 up to 300) and deliver them using the pulsing() function and the
  // switch-case structure
  if (reading <= 146)
  {
    result = 5; // For quick test
  }
  else if (reading <= 292)
  {
    result = 30; // Half a minute
  }
  else if (reading <= 438)
  {
    result = 60; // 1 minute
  }
  else if (reading <= 584)
  {
    result = 90; // 1.5 mins
  }
  else if (reading <= 730)
  {
    result = 120; // 2 mins
  }
  else if (reading <= 876)
  {
    result = 180; // 3 mins
  }
  else if (reading >= 877 )
  {
    result = 300; // 5 mins
  }

  // Delivers the chosen result via these seven cases
  switch (result)
  {
    case 5:
      Serial.println("5 pulses");
      pulsing(5);
      break;

    case 30:
      Serial.println("30 pulses");
      pulsing(30);
      break;

    case 60:
      Serial.println("60 pulses");
      pulsing(60);
      break;

    case 90:
      Serial.println("90 pulses");
      for (int count = 0; count < 90; count++)
        pulsing(90);
      break;

    case 120:
      Serial.println("120 pulses");
      pulsing(120);
      break;

    case 180:
      Serial.println("180 pulses");
      pulsing(180);
      break;

    case 300:
      Serial.println("300 pulses");
      pulsing(300);
      break;
  }


} // End of setup

void loop()
{

}

But why was the global declaration of 'result' apparently insufficient please? IOW, with this one line changed:

int pulsing (result) // Deliver result pulses

I'd like to learn the cause of this error:
redefinition of 'int pulsing'.

I dont find this line in your code.

By the way, you have a three variables named "result" in the code - first global, the second is local to pulsing() subroutine and third in setup. Note that the values of these variables are completely independent. I an in doubt that this is what you intended.

I suggest you read up on functions...

Post the code that causes error and post the whole error message. What you posted makes no sense, might as well hit keyboard with your left foot and ask why it doesn’t compile

Why do you need switch() here?

it can be easily replaced with

 Serial.print(result);
 Serial.println(" pulses");
 pulsing(result);

your explanation is a bit confusing. i don't see this error in the code you posted.

i corrected the following warning with your posted code, by defining pulsing() as returning void

C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:37:1: warning: no return statement in function returning non-void [-Wreturn-type]

but even with void, an attempt to reproduce your original problem by defining pulsing as

resulted in the following errors

Tst:23:20: error: variable or field 'pulsing' declared void
 void pulsing (result)
                    ^
Tst:23:14: error: 'result' was not declared in this scope
 void pulsing (result)
              ^~~~~~
Tst:23:21: error: variable or field 'pulsing' declared void
 void pulsing (result)
                     ^
Tst:23:15: error: 'result' was not declared in this scope
 void pulsing (result)
               ^~~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In function 'void setup()':
Tst:90:9: error: 'pulsing' was not declared in this scope
         pulsing(5);
         ^~~~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:90:9: note: suggested alternative: 'pulseIn'
         pulsing(5);
         ^~~~~~~
         pulseIn
exit status 1
variable or field 'pulsing' declared void

presumably you corrected your error by properly defining the argument type to "pulsing". you add "int"

but the following comments suggests the global definition of "result" fixed the problem.

but that global definition is not necessary because you defined it locally in setup()

so i'm guessing that you started making unnecessary changes and the problem because fixed without understanding which change was actually needed.

Thanks, a thoughtful reply from someone who actually read and tested my post and its code in full!

But it’s odd that you don’t get the same error as me. To make sure we're both singing from the same hymn sheet, I'll post in full both the working code and the one generating the error:
redefinition of 'int pulsing'

WORKING CODE

// Friday 2 September 2022
// 1. 14 V heavy duty supply for solenoid; converted to 5 V for 328 etc.
// 2. Circuit places 0-5 V from a 10k preset on analog pin A0.
// 3. Based on that value, delivers various numbers of pulses from pin D9
// 4. Via an NPN transistor and 5V relay these activate the solenoid.
// 5. After last pulse all apparent activity stops until next power-up.

const int onPin = 9;
const int tOn = 100;
const int tOff = 200;
const int analogPin = A0;

// For initial configuration and testing
int period = tOn + tOff; // ms
int f = 1000 / period; // Frequency (Hz)

int result;

// Function to deliver chosen number of pulses
int pulsing (int result)
// But int pulsing(result) caused the error described
// ***************************************************
{
  for (int count = 0; count < result; count++)
  {
    digitalWrite(onPin, HIGH);
    delay(tOn);
    digitalWrite(onPin, LOW);
    delay(tOff);
  }
}

void setup() //
{
  Serial.begin(115200);
  Serial.println(F("CanShaker-5a"));
  Serial.print(F("tOn = "));
  Serial.println(tOn);
  Serial.print("tOff = ");
  Serial.println(tOff);
  Serial.print(F("period = "));
  Serial.println(period);

  Serial.print(F("f = "));
  Serial.println(F(" Hz"));

  pinMode(onPin, OUTPUT);

  int reading = analogRead(analogPin); // 0-1023
  int result;

  Serial.print(F("reading = "));
  Serial.println(reading);


  // Based on reading from preset, calculate how many pulses required
  // ( 5 up to 300) and deliver them using the pulsing() function and the
  // switch-case structure
  if (reading <= 146)
  {
    result = 5; // For quick test
  }
  else if (reading <= 292)
  {
    result = 30; // Half a minute
  }
  else if (reading <= 438)
  {
    result = 60; // 1 minute
  }
  else if (reading <= 584)
  {
    result = 90; // 1.5 mins
  }
  else if (reading <= 730)
  {
    result = 120; // 2 mins
  }
  else if (reading <= 876)
  {
    result = 180; // 3 mins
  }
  else if (reading >= 877 )
  {
    result = 300; // 5 mins
  }

  // Delivers the chosen result via these seven cases
  switch (result)
  {
    case 5:
      Serial.println("5 pulses");
      pulsing(5);
      break;

    case 30:
      Serial.println("30 pulses");
      pulsing(30);
      break;

    case 60:
      Serial.println("60 pulses");
      pulsing(60);
      break;

    case 90:
      Serial.println("90 pulses");
      for (int count = 0; count < 90; count++)
        pulsing(90);
      break;

    case 120:
      Serial.println("120 pulses");
      pulsing(120);
      break;

    case 180:
      Serial.println("180 pulses");
      pulsing(180);
      break;

    case 300:
      Serial.println("300 pulses");
      pulsing(300);
      break;
  }


} // End of setup

void loop()
{

}

CODE WITH ERROR

// Friday 2 September 2022
// 1. 14 V heavy duty supply for solenoid; converted to 5 V for 328 etc.
// 2. Circuit places 0-5 V from a 10k preset on analog pin A0.
// 3. Based on that value, delivers various numbers of pulses from pin D9
// 4. Via an NPN transistor and 5V relay these activate the solenoid.
// 5. After last pulse all apparent activity stops until next power-up.

const int onPin = 9;
const int tOn = 100;
const int tOff = 200;
const int analogPin = A0;

// For initial configuration and testing
int period = tOn + tOff; // ms
int f = 1000 / period; // Frequency (Hz)

int result;

// Function to deliver chosen number of pulses
int pulsing (result)
// But int pulsing(result) caused the error described
// ***************************************************
{
  for (int count = 0; count < result; count++)
  {
    digitalWrite(onPin, HIGH);
    delay(tOn);
    digitalWrite(onPin, LOW);
    delay(tOff);
  }
}

void setup() //
{
  Serial.begin(115200);
  Serial.println(F("CanShaker-5a"));
  Serial.print(F("tOn = "));
  Serial.println(tOn);
  Serial.print("tOff = ");
  Serial.println(tOff);
  Serial.print(F("period = "));
  Serial.println(period);

  Serial.print(F("f = "));
  Serial.println(F(" Hz"));

  pinMode(onPin, OUTPUT);

  int reading = analogRead(analogPin); // 0-1023
  int result;

  Serial.print(F("reading = "));
  Serial.println(reading);


  // Based on reading from preset, calculate how many pulses required
  // ( 5 up to 300) and deliver them using the pulsing() function and the
  // switch-case structure
  if (reading <= 146)
  {
    result = 5; // For quick test
  }
  else if (reading <= 292)
  {
    result = 30; // Half a minute
  }
  else if (reading <= 438)
  {
    result = 60; // 1 minute
  }
  else if (reading <= 584)
  {
    result = 90; // 1.5 mins
  }
  else if (reading <= 730)
  {
    result = 120; // 2 mins
  }
  else if (reading <= 876)
  {
    result = 180; // 3 mins
  }
  else if (reading >= 877 )
  {
    result = 300; // 5 mins
  }

  // Delivers the chosen result via these seven cases
  switch (result)
  {
    case 5:
      Serial.println("5 pulses");
      pulsing(5);
      break;

    case 30:
      Serial.println("30 pulses");
      pulsing(30);
      break;

    case 60:
      Serial.println("60 pulses");
      pulsing(60);
      break;

    case 90:
      Serial.println("90 pulses");
      for (int count = 0; count < 90; count++)
        pulsing(90);
      break;

    case 120:
      Serial.println("120 pulses");
      pulsing(120);
      break;

    case 180:
      Serial.println("180 pulses");
      pulsing(180);
      break;

    case 300:
      Serial.println("300 pulses");
      pulsing(300);
      break;
  }


} // End of setup

void loop()
{

}

I didn’t show the entire error text, as I wanted to focus on this issue about re-definition, before possibly confusing matters with the other error reported:
'pulsing' cannot be used as a function

But I’ve now attached it below, making this a very long post. Plainly the cause is somehow about variable scope, but the penny hasn't yet dropped. As an ‘Arduino programmer’ (not trained in C/C++) I'm vexed over my key question. After setting ‘int result’ as global, why does it need inclusion anywhere else?

FULL ERROR REPORT

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\terry\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\terry\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries -fqbn=arduino:avr:uno -vid-pid=0000_0000 -ide-version=10819 -build-path C:\Users\terry\AppData\Local\Temp\arduino_build_486319 -warnings=none -build-cache C:\Users\terry\AppData\Local\Temp\arduino_cache_506310 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.arduinoOTA.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MY SKETCHES 2021-2022\CanShakerAsPosted\CanShakerAsPosted-ERROR\CanShakerAsPosted-ERROR.ino

C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\terry\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\terry\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries -fqbn=arduino:avr:uno -vid-pid=0000_0000 -ide-version=10819 -build-path C:\Users\terry\AppData\Local\Temp\arduino_build_486319 -warnings=none -build-cache C:\Users\terry\AppData\Local\Temp\arduino_cache_506310 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.arduinoOTA.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MY SKETCHES 2021-2022\CanShakerAsPosted\CanShakerAsPosted-ERROR\CanShakerAsPosted-ERROR.ino

Using board 'uno' from platform in folder: C:\Users\terry\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5

Using core 'arduino' from platform in folder: C:\Users\terry\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5

Detecting libraries used...

"C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\sketch\\CanShakerAsPosted-ERROR.ino.cpp" -o nul

Generating function prototypes...

"C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\sketch\\CanShakerAsPosted-ERROR.ino.cpp" -o "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\preproc\\ctags_target_for_gcc_minus_e.cpp"

"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\preproc\\ctags_target_for_gcc_minus_e.cpp"

Compiling sketch...

"C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\sketch\\CanShakerAsPosted-ERROR.ino.cpp" -o "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_486319\\sketch\\CanShakerAsPosted-ERROR.ino.cpp.o"

CanShakerAsPosted-ERROR:20:20: error: redefinition of 'int pulsing'

 int pulsing (result)

                    ^

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MY SKETCHES 2021-2022\CanShakerAsPosted\CanShakerAsPosted-ERROR\CanShakerAsPosted-ERROR.ino:20:5: note: 'int pulsing' previously declared here

 int pulsing (result)

     ^~~~~~~

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MY SKETCHES 2021-2022\CanShakerAsPosted\CanShakerAsPosted-ERROR\CanShakerAsPosted-ERROR.ino: In function 'void setup()':

CanShakerAsPosted-ERROR:93:16: error: 'pulsing' cannot be used as a function

       pulsing(5);

                ^

CanShakerAsPosted-ERROR:98:17: error: 'pulsing' cannot be used as a function

       pulsing(30);

                 ^

CanShakerAsPosted-ERROR:103:17: error: 'pulsing' cannot be used as a function

       pulsing(60);

                 ^

CanShakerAsPosted-ERROR:109:19: error: 'pulsing' cannot be used as a function

         pulsing(90);

                   ^

CanShakerAsPosted-ERROR:114:18: error: 'pulsing' cannot be used as a function

       pulsing(120);

                  ^

CanShakerAsPosted-ERROR:119:18: error: 'pulsing' cannot be used as a function

       pulsing(180);

                  ^

CanShakerAsPosted-ERROR:124:18: error: 'pulsing' cannot be used as a function

       pulsing(300);

                  ^

exit status 1

redefinition of 'int pulsing'

> "I dont find this line in your code."

That's because it wasn't in the WORKING code
Did you not then see these two lines?

// But int pulsing(result) caused the error described
// ***************************************************

I'm sure you're right about the issue being due to some mistake about the scope of my variables. But what exactly? Specifically:

  1. Why does the globally declared variable 'result' need including anywhere else?
  2. How can 'int pulsing' be a redefinition when it is the only instance?

because

int pulsing (result) 

is not a function declaration, this declare int variable pulsing and initialise it with value of result

Please read language textbook about function declaration, it return values and parameters. Your code contains many errors related to function definition and use, the scope of variables etc...

Eh? Where would the seven choices get made, converting the voltage on A0 to a number ('result') of pulses.

Sorry, but I'd read up on functions and I just re-read that specific article,. But still don't see what's wrong with my function?

Return type = int
Function name = pulsing
Argument = (result)

When you define the function, it are not arguments, this are parameters. Note the difference.
A function parameters should be defined with type.

(result) - value of variable, defined before
(int result) - function parameter of type int

I guess english is probably not your native language, but I'm afraid I have no idea what you mean by that! I used the terminology in the article you endorsed. There is a single parameter in my case, namely 'result'.

For starters you are not returning anything from your function

1 Like

@Terrypin you need declare a type for each "argument".

You understand?

And if the function isn't void, need return a value.

Your picture contains a errors :slight_smile: Where are you find it?

If this is a function definition (which is indicated by the return type and the function body in curly braces) then these parameters or arguments - call it what you want - must be specified with types.
Open any C textbook and make sure that this picture was drawn by an ignoramus

1 Like

Did you miss post #3 as well?

There are a lot of errors on the web sites - is this new to you?

So do not read this source anymore, it is too simplistic.

And you miss the examples in #3. :wink: