0

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

2 Answers 2

1

You have declared count_pulse as a variable and NOT as function prototype.

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

void count_pulse(); \\ <--- Function prototype

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

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 agree to our terms of service and acknowledge you have read our privacy policy.

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