Redifining Volatile Error

I got this sketch off the internet with a Cut and Paste and I hope I've got the error/code format right. Copied the error message, Code tags etc.

The sketch won't verify/compile. The error is something about redefinition of "volatile int rpmcount". There's a reference link to volatile and I've looked it several times.

It seems a volatile variable needs to be from RAM vs a Storage Register. This sketch counts pulses so I'm guessing the need for a volatile is because the number can change. I've also noticed the very first line of code seems to establish the "volatile int rpmcount as 0, but I don't know what to make of that.

I'm deeply interested in this particular sketch and was planning on getting it running so I could study it further, but maybe there's something to learn by fixing the code or whatever it is I'm doing wrong.

Here it is:

 Arduino: 1.8.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

sketch_nov11a:44: error: redefinition of 'volatile int rpmcount'

 volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 

              ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:3:14: note: 'volatile int rpmcount' previously defined here

 volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile

              ^

sketch_nov11a:45: error: conflicting declaration 'long unsigned int rpm'

 unsigned long rpm = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:4:5: note: previous declaration as 'int rpm'

 int rpm = 0;

     ^

sketch_nov11a:46: error: redefinition of 'long unsigned int lastmillis'

 unsigned long lastmillis = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:5:15: note: 'long unsigned int lastmillis' previously defined here

 unsigned long lastmillis = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void setup()':

sketch_nov11a:48: error: redefinition of 'void setup()'

 void setup(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:7:6: note: 'void setup()' previously defined here

 void setup(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void loop()':

sketch_nov11a:53: error: redefinition of 'void loop()'

 void loop(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:12:6: note: 'void loop()' previously defined here

 void loop(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void rpm_fan()':

sketch_nov11a:88: error: redefinition of 'void rpm_fan()'

 void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:33:6: note: 'void rpm_fan()' previously defined here

 void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/

      ^

exit status 1
redefinition of 'volatile int rpmcount'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

pntrbl:
I got this sketch...

  • ? -

LOL! What sketch? That would seem to be pertinent to any sort of discussion. Wouldn't it? :o

The sketch;

// read RPM

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2).
}

void loop(){
 
 if (millis() - lastmillis == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 
 
 rpm = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.print(rpm); // print the rpm value.
 Serial.print("\t Hz=\t"); //print the word "Hz".
 Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/
 
 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Uptade lasmillis
 attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}


// Elimelec Lopez - April 25th 2013
// read RPM and calculate average every then readings.
const int numreadings = 10;
int readings[numreadings];
unsigned long average = 0;
int index = 0;
unsigned long total; 

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 
unsigned long rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);
}

void loop(){
 
  
 if (millis() - lastmillis >= 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 total = 0;  
 readings[index] = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 for (int x=0; x<=9; x++){
   total = total + readings[x];
 }
 
 average = total / numreadings;
 rpm = average;
 
 rpmcount = 0; // Restart the RPM counter
 index++;
 if(index >= numreadings){
  index=0; 
 } 
 
 
if (millis() > 11000){  // wait for RPMs average to get stable

 Serial.print(" RPM = ");
 Serial.println(rpm);
}
 
 lastmillis = millis(); // Uptade lasmillis
  attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}

And the error message again;

Arduino: 1.8.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

sketch_nov11a:44: error: redefinition of 'volatile int rpmcount'

 volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 

              ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:3:14: note: 'volatile int rpmcount' previously defined here

 volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile

              ^

sketch_nov11a:45: error: conflicting declaration 'long unsigned int rpm'

 unsigned long rpm = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:4:5: note: previous declaration as 'int rpm'

 int rpm = 0;

     ^

sketch_nov11a:46: error: redefinition of 'long unsigned int lastmillis'

 unsigned long lastmillis = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:5:15: note: 'long unsigned int lastmillis' previously defined here

 unsigned long lastmillis = 0;

               ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void setup()':

sketch_nov11a:48: error: redefinition of 'void setup()'

 void setup(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:7:6: note: 'void setup()' previously defined here

 void setup(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void loop()':

sketch_nov11a:53: error: redefinition of 'void loop()'

 void loop(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:12:6: note: 'void loop()' previously defined here

 void loop(){

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino: In function 'void rpm_fan()':

sketch_nov11a:88: error: redefinition of 'void rpm_fan()'

 void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/

      ^

C:\Users\Bill\AppData\Local\Temp\arduino_modified_sketch_928984\sketch_nov11a.ino:33:6: note: 'void rpm_fan()' previously defined here

 void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/

      ^

exit status 1
redefinition of 'volatile int rpmcount'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You have several variables declared twice. Don't do that.

 if (millis() - lastmillis == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/

Needs to be greater than or equal.

I've no direct experience but, I've read other posts to the effect that using nointerrupts() and its opposite number is preferable to detaching/reattaching interrupts.

The difference is that an interrupt that comes in while detached is simply gone, it never existed. Interrupts arriving while disabled via noInterrupts() will be latched and acted upon when re-enabled via interrupts().

Interrupts arriving while disabled via noInterrupts() will be latched and acted upon when re-enabled via interrupts().

ONE instance of the interrupt will be queued, not ALL instances.

The absolute minimum time should be spent with interrupts disabled/detached. Serial printing is verbotten in that context. Disable/detach the interrupt. Copy the needed data. Re-enable/re-attach the interrupts. THEN, perform the required computations based on the copied data and print, as needed.

PaulS:
ONE instance of the interrupt will be queued, not ALL instances.

Thanks for the clarification.