Attachedinterrupt arduino uno wifi REV2

I have problem to get the code working on my new arduino wifi rev2. The code is working on my standard arduino uno but not with the wifi rev2.

I have tried to change the attachedinterrupt to digitalpin but it still dont work.
Can some please look at my code and thell what is wrong.

#include <FlowMeter.h> // 
// connect a flow meter to an interrupt pin (see notes on your Arduino model for pin numbers)
FlowMeter Meter1 = FlowMeter(2);
FlowMeter Meter2 = FlowMeter(3);
// set the measurement update period to 1s (1000 ms)
const unsigned long period = 1000;
// define an 'interrupt service handler' (ISR) for every interrupt pin you use
void Meter1ISR() {
// let our flow meter count the pulses
Meter1.count();
}
// define an 'interrupt service handler' (ISR) for every interrupt pin you use
void Meter2ISR() {
// let our flow meter count the pulses
Meter2.count();
}
void setup() {
// prepare serial communication
Serial.begin(9600);
// 
// 
attachInterrupt(2, Meter1ISR, RISING);
attachInterrupt(3, Meter2ISR, RISING);
Meter1.reset();
Meter2.reset();
}
void loop() {
// wait between output updates
delay(period);
// process the (possibly) counted ticks
Meter1.tick(period);
Meter2.tick(period);
// output some measurement result
Serial.println("M1 " + String(Meter1.getCurrentFlowrate()) + " l/min, " + String(Meter1.getTotalVolume())+ " l total.");
Serial.println("M2 " + String(Meter2.getCurrentFlowrate()) + " l/min, " + String(Meter2.getTotalVolume())+ " l total.");
}

Best regards

Posting code that way is wrong.

Fix your post. Use code tags.

what's wrong is that it's unreadable...

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Sry i forgott.

Is it readable now ?

The interrupt numbers are not generally the same as the pin numbers. You should use:

attachInterrupt(digitalPinToInterrupt(2), Meter1ISR, RISING);
attachInterrupt(digitalPinToInterrupt(3), Meter2ISR, RISING);

tnx John but it still dosent work. the error code is this:

C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp: In constructor 'FlowMeter::FlowMeter(unsigned int, FlowSensorProperties, void (*)(), uint8_t)':
C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:18:83: error: no matching function for call to 'attachInterrupt(unsigned int&, void (*&)(), uint8_t&)'
         attachInterrupt(this->_pin, this->_interruptCallback, this->_interruptMode);
                                                                                   ^
In file included from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/HardwareSPI.h:21:0,
                 from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/ArduinoAPI.h:31,
                 from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/Arduino.h:23,
                 from C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:5:
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Common.h:108:6: note: candidate: void attachInterrupt(pin_size_t, voidFuncPtr, PinStatus) <near match>
 void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode);
      ^~~~~~~~~~~~~~~
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Common.h:108:6: note:   conversion of argument 3 would be ill-formed:
C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:18:69: warning: invalid conversion from 'uint8_t {aka unsigned char}' to 'PinStatus' [-fpermissive]
         attachInterrupt(this->_pin, this->_interruptCallback, this->_interruptMode);
                                                               ~~~~~~^~~~~~~~~~~~~~
In file included from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/ArduinoAPI.h:33:0,
                 from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/Arduino.h:23,
                 from C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:5:
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Interrupts.h:38:27: note: candidate: template<class T> void arduino::attachInterrupt(pin_size_t, arduino::voidTemplateFuncPtrParam<T*>, PinStatus, T*)
 template<typename T> void attachInterrupt(pin_size_t interruptNum, voidTemplateFuncPtrParam<T*> userFunc, PinStatus mode, T* param) {
                           ^~~~~~~~~~~~~~~
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Interrupts.h:38:27: note:   template argument deduction/substitution failed:
C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:18:83: note:   candidate expects 1 argument, 0 provided
         attachInterrupt(this->_pin, this->_interruptCallback, this->_interruptMode);
                                                                                   ^
In file included from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/ArduinoAPI.h:33:0,
                 from C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/Arduino.h:23,
                 from C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:5:
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Interrupts.h:21:27: note: candidate: template<class T> void arduino::attachInterrupt(pin_size_t, arduino::voidTemplateFuncPtrParam<T>, PinStatus, T&)
 template<typename T> void attachInterrupt(pin_size_t interruptNum, voidTemplateFuncPtrParam<T> userFunc, PinStatus mode, T& param) {
                           ^~~~~~~~~~~~~~~
C:\Users\ludvig.andersson\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/Interrupts.h:21:27: note:   template argument deduction/substitution failed:
C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:18:83: note:   candidate expects 1 argument, 0 provided
         attachInterrupt(this->_pin, this->_interruptCallback, this->_interruptMode);
                                                                                   ^
exit status 1
Error compiling for board Arduino Uno WiFi Rev2.

If GitHub - sekdiy/FlowMeter: Arduino flow meter library that provides calibrated liquid flow and volume measurement with flow sensors. is the library, it does not seem to be compatible with the megaAVR processor; below is the indication

C:\Users\ludvig.andersson\OneDrive - Beulco Armatur AB\Dokument\Arduino\libraries\FlowMeter-master\src\FlowMeter.cpp:18:69: warning: invalid conversion from 'uint8_t {aka unsigned char}' to 'PinStatus' [-fpermissive]
attachInterrupt(this->_pin, this->_interruptCallback, this->_interruptMode);

You can raise an issue on the github page or you can hack the library.

The below will break compatibility with the Uno (AVR in general) but will compile for the Uno WiFi Rev2 (megaAVR).
Flowmeter.cpp
Change line 9 from

FlowMeter::FlowMeter(unsigned int pin, FlowSensorProperties prop, void (*callback)(void), uint8_t interruptMode) :```
to
```c++
FlowMeter::FlowMeter(unsigned int pin, FlowSensorProperties prop, void (*callback)(void), PinStatus interruptMode) :

FlowMeter.h
Change line 47 from

FlowMeter(unsigned int pin = 2, FlowSensorProperties prop = UncalibratedSensor, void (*callback)(void) = NULL, uint8_t interruptMode = RISING);

to

FlowMeter(unsigned int pin = 2, FlowSensorProperties prop = UncalibratedSensor, void (*callback)(void) = NULL, PinStatus interruptMode = RISING);

And change line 100 from

    uint8_t _interruptMode;                      //!< interrupt mode (LOW, CHANGE, RISING, FALLING, HIGH)

to

PinStatus _interruptMode;                      //!< interrupt mode (LOW, CHANGE, RISING, FALLING, HIGH)

In the next step you can use conditional compilation to make the code compatible with both AVR and megaAVR. I know what to do for that but it will take me too long to figure out the exact details.

I'll leave the testing up to you, it does compile :wink:

thanks a lot :slight_smile: it works !!!!

I've modified the two files in question and added conditional compilation so it should now work on the old AVR architecture (Uno, Nano, Mega, ...) and the megaAVR architecture (Uno WiFi Rev2 and Nano Every). Both files are attached.

@ludand14, would it be possible for you to test with the Uno and the Uno WiFi Rev2; it does compile but I don't have a megaAVR board and I don't have a flow sensor. You can replace the existing files in libraries\FlowMeter\src by those two (make a backup first).

FlowMeter.cpp (6.4 KB)
FlowMeter.h (7.8 KB)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.