we have created one fuel sensor in our lab with arduino in order to get fuel data from vehicles.Regarding the code below i receive an error PARSE ERROR: 'attachInterrupt': this function needs to be defined.Could you please advice?

int pin = 2;
volatile unsigned int pulse;
float volume = 0;
float flow_rate =0;
const int pulses_per_litre = 2000;
int count_pulse;

void setup()
{
    Serial.begin(9600);
    pinMode(pin, INPUT);
    attachInterrupt(0, count_pulse, RISING);

}

void loop()
{
    pulse=0;
    interrupts();
    delay(1000); 
    noInterrupts();

    Serial.print("Pulses per second: ");
    Serial.println(pulse);

    flow_rate = pulse * 1000/pulses_per_litre; //

    Serial.print("FUEL FLOW rate: ");
    Serial.print(flow_rate);
    Serial.println(" milliliters per second");

    volume = volume + flow_rate * 0.1; //Time Interval is 0.1 second

    Serial.print("Volume: ");
    Serial.print(volume);
    Serial.println(" milliliters");
}

void count_pulse()
{
pulse++;}
  • 2
    You have a int count_pulse and a function count_pulse, that confuses the compiler. – Jot Dec 6 at 14:16
  • and add forward declaration line void count_pulse(); before setup() – Juraj Dec 6 at 14:19
  • @Juraj Doesn't the Arduino IDE do that for you? – Gerben Dec 6 at 15:38
  • there is some problem for function as parameter – Juraj Dec 6 at 15:54

Just initialize pulse to zero in its definition at line 2.

The message "'pulse': used without being explicitly initialized" is due to the count_pulse() function: The compiler can't analyze the run-time behavior of the code, nor your intentions. Some interrupts could arrive before loop() runs, so in fact, pulse could very well get incremented without having been initialized. You may not care about and can discard the first few readings, but the compiler doesn't (and shouldn't!) guess that.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.