In my program I use both a timerInterrupt which is triggered every 1 second to calculate the water flow during this time interval and two Interrupts which when active increment the variables Pulscount and Pulscount1 which will be used later for the water flow calculation. I use the arduino mega 2560 for this purpose. According to the data sheet the InterruptPin are at Pin 2, 3, 18, 19, 20 and 21. When I use Pin 18 and 19 my program works without problems. But when I use Pin (2, 3, 20 and 21) the values displayed on the LCD are strange and sometimes the brightness of the LCD decreases. Does anyone have an idea of the source of the problem?
Why use millis within the timer interrupt? If you use millis, you can do that within loop() and need no interrupt at all, which is much better and doesn't interfere with your prints.
Interrupts are only needed for really fast response times. What is the frequency of your pulses?
Thinking about it, milli() has no place in the TimerInterrupt. If I don't perform these operations in the loop using milli(), it's because I'll put my main code there later. The flow rate that has to be measured constantly will be measured in parallel at specific time intervals in the timerinterrupt. Also the number of pulses counted per second is relatively high (about 100 pulses), that's why I use the interrupt
Not sure I understand what you are saying; however, there is no reason for the timer interrupt if you properly code loop() without using delay() calls. You can leave the pulse counter interrupts.
Good to be clearer, the program in the loop represents that of the operation of a water purification system. The program in the loop being very long (almost 400 lines of code) I preferred not to post it. in parallel with the operation of the system, some parameters such as the water flow must be measured by a sensor at precise time intervals. the sensor delivers to its output only pulses that must be counted (pulseCount and PulseCount1) during this time interval specified (in this case 1 second). this will make it possible to calculate the water flow. That is why I am using timerInterrupt to calculate it every one second
Pins 20 and 21 are the Wire/TWI/I2C pins that you are using to talk to your LCD. You can't use than for interrupts when you are using them for the LCD.
You can get more interrupts using "Pin Change" interrupts. The PinChangeInterrupt library makes that fairly easy.
In Datasheet Arduino Mega2560, DigitalPin 0 and 1 are SCL and SDL just like DigitalPin 20 and 21. So for my LCD I am using Pin 1 and 2 so that I can use Pin 20 and 21 for Interrupt. Do you think this can also be a problem?
The ATmega2560 datasheet does not use Arduino pin numbers. The Arduino IDE uses Arduino pin numbers.
On the Arduino MEGA 2560, Pin 0 is RXD0, known in the ATmega2560 datasheet as PE0 (PORTE bit 0). On the Arduino MEGA 2560, Pin 1 is TXD0, known in the ATmega2560 datasheet as PE1 (PORTE bit 1). Neither is SDA or SCL.
There are two Arduino pins connected to each of the TWI pins. Pin 20 and the "SDA" pin near the USB jack are both connected to PD1 (PORTD bit 1). Pin 21 and the "SCL" pin near the USB jack are both connected to PD0 (PORTD bit 0).