AttachInterrupt two sensors Arduino Uno

Hi,
I'm trying to detect two encoders with AttachInterrupt event using Arduino Uno module.

The code attached

void flow(){
flow_frequency++;
}
void flow_1(){
flow_frequency_1++;
}
void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), flow, RISING);
pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(3), flow_1, RISING);
sei();
}

When the input 2 is rising the event "flow" trigger and also event "flow_1" is trigger which cause to flow_frequency_1 parameter to increment and it is worng.

When input 3 is rising event "flow_1" trigger and event "flow" not trigger which is ok.

Please advise ?

Thank you

Yalon

Do you have a data sheet for the encoders? Some encoders have an open collector output so need a pullup resistor.

Thank you for your reply.
They are two simple water flow sensors connected to input 2 & 3
I tried to put 110 Ohm resistor without any help

Any idea ?

Thank you

Yalon

Pull up resistors are generally in the 5K to 10K range.

I tried these type of resistors without results.
It seems to be bug that causes the event 1 to trigger the event 2 ??
Any idea ?
Thank you
Yalon

ygnd:
It seems to be bug that causes the event 1 to trigger the event 2 ?? Any idea ?

I suspect problems/bugs in the code you do not show.

A circuit diagram would be useful.
Have you avoided ground loops?
Do you have a link to your water flow sensors?

The sei(); from the snippet shows that there are misunderstandings in the whole interrupt system.

Post the whole code, to make shure it's no handling problem.

If you are wiring things correctly and using pull-up resistors the two inputs will be independent,
therefore you haven't wired things up correctly.

Show the circuit and the code if you want more than guesswork, as has been carefully detailed
in the "how to post to this forum" threads.......

Thank you all for your replies
Please see complete code & typical output

#include <Arduino.h>
#include <time.h>
#include <SPI.h>
#include <Ethernet.h>

volatile int flow_frequency_2,flow_frequency_3;
unsigned int l_hour, l_hour_1,prev_sens_2=0,prev_sens_3=0;
unsigned long currentTime;
unsigned long cloopTime;
unsigned int sec_count_2=0, sec_count_3=0,pulse_count_2=0,pulse_count_3=0,delay_time=2000;
unsigned int rec_exsist_2=0, rec_exsist_3=0;

EthernetClient client;

byte mac[] = { 0xDE, 0xAD, 0xBF, 0x1F, 0x4E, 0xED };
char server[] = "192.168.1.9";

IPAddress ip(192,168,1,20);

void flow_2(){

flow_frequency_2++;
Serial.print("flow_frequency_2 ");
Serial.print(flow_frequency_2, DEC);
Serial.println("");

}
void flow_3(){
flow_frequency_3++;
Serial.print("flow_frequency_3 ");
Serial.print(flow_frequency_3, DEC);
Serial.println("");
}
void setup(){
Serial.begin(9600);

pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), flow_2, FALLING);

pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(3), flow_3, RISING);

currentTime = millis();
cloopTime = currentTime;
pinMode(13, OUTPUT);

Ethernet.begin(mac, ip);

Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());

sei(); // Enable interrupts

}
void loop (){
currentTime = millis();

if(currentTime >= (cloopTime + delay_time)){

digitalWrite(13, LOW);
int del_pulse_2=0;
int del_pulse_3=0;

cloopTime = currentTime;

cli();

Serial.print("delta_pulse_2 ");
del_pulse_2 = flow_frequency_2;
flow_frequency_2=0;

Serial.print(del_pulse_2, DEC);
Serial.println("");

Serial.print("delta_pulse_3 ");
del_pulse_3 = flow_frequency_3;
flow_frequency_3=0;

Serial.print(del_pulse_3, DEC);
Serial.println("");

sei();

delay_time=2000;

if(del_pulse_2==0 && pulse_count_2 == 0){

sec_count_2 = sec_count_2+2;
Serial.print("continu no flow 2 -> sec count : ");
Serial.print(sec_count_2);
Serial.println("");
if(sec_count_2 > 500){
send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
rec_exsist_2=1;
sec_count_2=0;
}

}else if(del_pulse_2 > 0 && pulse_count_2 > 0){

pulse_count_2 = pulse_count_2 + del_pulse_2;
sec_count_2 = sec_count_2+2;
Serial.print("continu flow 2 -> sec count : ");
Serial.print(sec_count_2);
Serial.print(" pulse count : ");
Serial.print(pulse_count_2);
Serial.println("");

if(sec_count_2 > 500){
send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
rec_exsist_2=1;
sec_count_2=0;
}

}else if(del_pulse_2 > 0 && pulse_count_2 == 0){

Serial.print("transmit zero 2 -> sec count : ");
Serial.print(sec_count_2);
Serial.println("");
if(sec_count_2>0)
send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
rec_exsist_2 = 0;
pulse_count_2 = del_pulse_2;
sec_count_2 = 2;

}else if(del_pulse_2 == 0 && pulse_count_2 > 0){

Serial.print("transmit flow 2 -> sec count : ");
Serial.print(sec_count_2);
Serial.print(" pulse count : ");
Serial.print(pulse_count_2);
Serial.println("");

send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
rec_exsist_2 = 0;
sec_count_2 = 0;
pulse_count_2 = 0;

}

if(del_pulse_3==0 && pulse_count_3 == 0){

sec_count_3 = sec_count_3+2;
Serial.print("continu no flow 3 -> sec count : ");
Serial.print(sec_count_3);
Serial.println("");
if(sec_count_3>500){
send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
rec_exsist_3=1;
sec_count_3=0;
}

}else if(del_pulse_3 > 0 && pulse_count_3 > 0){

pulse_count_3 = pulse_count_3 + del_pulse_3;
sec_count_3 = sec_count_3+2;
Serial.print("continu flow 3 -> sec count : ");
Serial.print(sec_count_3);
Serial.print(" pulse count : ");
Serial.print(pulse_count_3);
Serial.println("");

if(sec_count_3 > 500){
send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
rec_exsist_3=1;
sec_count_3=0;
}

}else if(del_pulse_3 > 0 && pulse_count_3 == 0){

Serial.print("transmit zero 3 -> sec count : ");
Serial.print(sec_count_3);
Serial.println("");

if(sec_count_3>0)
send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);

rec_exsist_3 = 0;
pulse_count_3 = del_pulse_3;
sec_count_3 = 2;

}else if(del_pulse_3 == 0 && pulse_count_3 > 0){

Serial.print("transmit flow 3 -> sec count : ");
Serial.print(sec_count_3);
Serial.print(" pulse count : ");
Serial.print(pulse_count_3);
Serial.println("");

send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
rec_exsist_3 = 0;
sec_count_3 = 0;
pulse_count_3 = 0;
}
}
}

void send_data_to_server(int seneNo,int procSec, int pulseCount, int transType){

if (client.connect(server, 80)) {

Serial.println("-> Connected");
client.print( "GET /BondaASP.aspx?");
client.print("prm=100");
client.print("&&");
client.print("ControlNo=A00000001");
client.print("&&");
client.print("SensorNo=");
client.print(seneNo);
client.print("&&");
client.print("TransTime=");
client.print("24/06/2016%2020:00:00");
client.print("&&");
client.print("delaySec=");
client.print(0);
client.print("&&");
client.print("procSec=");
client.print(procSec);
client.print("&&");
client.print("pulseCount=");
client.print(pulseCount);
client.print("&&");
client.print("transType=");
client.print(transType);

client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println( "Connection: close" );
client.println();
client.println();
client.stop();

delay(1000);
delay_time = delay_time - 1000;

} else
Serial.println("--> connection failed/n");

}

Typical output : when sensor 2 fire also sensor 3 fire

TransTime input ProcSec Pulses Liters
05/07/2016 18:54:07 3 474 0 0.00
05/07/2016 18:54:05 2 474 0 0.00
05/07/2016 18:46:09 3 2 3 0.01
05/07/2016 18:46:08 2 2 1 0.00
05/07/2016 18:46:06 3 474 0 0.00
05/07/2016 18:46:04 2 474 0 0.00
05/07/2016 18:38:09 3 2 4 0.01
05/07/2016 18:38:07 2 2 3 0.01
05/07/2016 18:38:06 3 474 0 0.00
05/07/2016 18:38:05 2 474 0 0.00
05/07/2016 18:30:09 3 2 5 0.02
05/07/2016 18:30:08 2 2 4 0.01
05/07/2016 18:30:07 3 474 0 0.00
05/07/2016 18:30:05 2 474 0 0.00
05/07/2016 18:22:09 3 2 1 0.00
05/07/2016 18:22:07 2 2 2 0.01
05/07/2016 18:22:06 3 474 0 0.00
05/07/2016 18:22:04 2 474 0 0.00
05/07/2016 18:14:08 3 2 4 0.01
05/07/2016 18:14:07 2 2 3 0.01
05/07/2016 18:14:06 3 474 0 0.00
05/07/2016 18:14:04 2 474 0 0.00

please see diagram attached

Looking forward to your comments
BR
Yalon

Do not use Serial.print in an ISR.

Do not disable interrupts and use Serial.print.

Read about interrupts Gammon Forum : Electronics : Microprocessors : Interrupts

Hi,
I did the changes as posted in #10 but the behavior remain the same
I also added the monitoring image of the counters (before the change)
Any more idea ?
Thank you
Yalon

Post the new code and output (in code tags </>).

Hi,

Please find the changed code and the attached the output image

/*
YF‐ S201 Water Flow Sensor
Water Flow Sensor output processed to read in litres/hour
Adaptation Courtesy: www.hobbytronics.co.uk
*/
#include <Arduino.h>
#include <time.h>
#include <SPI.h>
#include <Ethernet.h>
//#include <UIPEthernet.h>

volatile int flow_frequency_2,flow_frequency_3;
unsigned int l_hour, l_hour_1,prev_sens_2=0,prev_sens_3=0;
unsigned long currentTime;
unsigned long cloopTime;
unsigned int sec_count_2=0, sec_count_3=0,pulse_count_2=0,pulse_count_3=0,delay_time=2000;
unsigned int rec_exsist_2=0, rec_exsist_3=0;

EthernetClient client;

byte mac[] = { 0xDE, 0xAD, 0xBF, 0x1F, 0x4E, 0xED };  
char server[] = "192.168.1.9";

IPAddress ip(192,168,1,20);
//EthernetServer server(80);

void flow_2(){

     flow_frequency_2++;

}
void flow_3(){
     flow_frequency_3++;
}
void setup(){
   Serial.begin(9600);

   pinMode(2, INPUT);
   attachInterrupt(digitalPinToInterrupt(2), flow_2, FALLING); // Setup Interrupt
   //attachInterrupt(1, flow, RISING); // Setup Interrupt

   pinMode(3, INPUT);
   attachInterrupt(digitalPinToInterrupt(3), flow_3, RISING); // Setup Interrupt
 
   currentTime = millis();
   cloopTime = currentTime;
     
   Ethernet.begin(mac, ip);

   //Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
   //Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
   //Serial.print("IP Address        : ");
   //Serial.println(Ethernet.localIP());
   //Serial.print("Subnet Mask       : ");
   //Serial.println(Ethernet.subnetMask());
   //Serial.print("Default Gateway IP: ");
   //Serial.println(Ethernet.gatewayIP());
   //.Serial.print("DNS Server IP     : ");
   //Serial.println(Ethernet.dnsServerIP());
   
   sei(); 

  
}
void loop (){
   currentTime = millis();
   
   if(currentTime >= (cloopTime + delay_time)){
        
        digitalWrite(13, LOW); 
        int del_pulse_2=0;
        int del_pulse_3=0;
        
        
        cloopTime = currentTime;
        
        //cli();
          
          //l_hour = (flow_frequency_2 * 60 / 5.5);
          //Serial.print("delta_pulse_2 ");
          del_pulse_2 = flow_frequency_2;
          
          if(flow_frequency_2>1)
             digitalWrite(13, HIGH);    

          flow_frequency_2=0;
          //prev_sens_2=flow_frequency_2;
          //Serial.print(del_pulse_2, DEC);
          //Serial.println("");
          
          //l_hour_1 = (flow_frequency_3 * 60 / 5.5);     
          //Serial.print("delta_pulse_3 ");
          del_pulse_3 = flow_frequency_3;
          flow_frequency_3=0;
          //prev_sens_3=flow_frequency_3;
          //Serial.print(del_pulse_3, DEC); 
          //Serial.println("");
              
        //sei();
        
        delay_time=2000;
        
        if(del_pulse_2==0 && pulse_count_2 == 0){
        
           sec_count_2 = sec_count_2+2;
           
           //Serial.print("continu no flow 2 -> sec count : ");
           //Serial.print(sec_count_2);
           //Serial.println("");
           
           if(sec_count_2 > 500){
             send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
             rec_exsist_2=1;
             sec_count_2=0;
           }
           
        }else if(del_pulse_2 > 0 && pulse_count_2 > 0){
          
           pulse_count_2 = pulse_count_2 + del_pulse_2;
           sec_count_2 = sec_count_2+2;
           
           //Serial.print("continu flow 2 -> sec count : ");
           //Serial.print(sec_count_2);
           //Serial.print(" pulse count : ");
           //Serial.print(pulse_count_2);
           //Serial.println("");
           
           if(sec_count_2 > 500){
             send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
             rec_exsist_2=1;
             sec_count_2=0;
           }
          

        }else if(del_pulse_2 > 0 && pulse_count_2 == 0){
                    
          //Serial.print("transmit zero 2 -> sec count : ");
          //Serial.print(sec_count_2);
          //Serial.println("");
          
          if(sec_count_2>0)
            send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
          rec_exsist_2 = 0;      
          pulse_count_2 = del_pulse_2;
          sec_count_2 = 2;         
          
        }else if(del_pulse_2 == 0 && pulse_count_2 > 0){
          
          
          //Serial.print("transmit flow 2 -> sec count : ");
          //Serial.print(sec_count_2);
          //Serial.print(" pulse count : ");
          //Serial.print(pulse_count_2);
          //Serial.println("");
          
          send_data_to_server(2, sec_count_2, pulse_count_2,rec_exsist_2);
          rec_exsist_2 = 0;
          sec_count_2 = 0;
          pulse_count_2 = 0;
          
        }

        if(del_pulse_3==0 && pulse_count_3 == 0){
        
           sec_count_3 = sec_count_3+2;
           
           //Serial.print("continu no flow 3 -> sec count : ");
           //Serial.print(sec_count_3);
           //Serial.println("");
           
           if(sec_count_3>500){
             send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
             rec_exsist_3=1;
             sec_count_3=0;
           }
       
        }else if(del_pulse_3 > 0 && pulse_count_3 > 0){
          
           pulse_count_3 = pulse_count_3 + del_pulse_3;
           sec_count_3 = sec_count_3+2;
           
           //Serial.print("continu flow 3 -> sec count : ");
           //Serial.print(sec_count_3);
           //Serial.print(" pulse count : ");
           //Serial.print(pulse_count_3);
           //Serial.println("");
           
           if(sec_count_3 > 500){
             send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
             rec_exsist_3=1;
             sec_count_3=0;
           }
        
        }else if(del_pulse_3 > 0 && pulse_count_3 == 0){
         
          //Serial.print("transmit zero 3 -> sec count : ");
          //Serial.print(sec_count_3);
          //Serial.println("");
          
          if(sec_count_3>0)
             send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
          
          rec_exsist_3 = 0;
          pulse_count_3 = del_pulse_3;
          sec_count_3 = 2;         
          
        }else if(del_pulse_3 == 0 && pulse_count_3 > 0){
          
          //Serial.print("transmit flow 3 -> sec count : ");
          //Serial.print(sec_count_3);
          //Serial.print(" pulse count : ");
          //Serial.print(pulse_count_3);
          //Serial.println("");

          send_data_to_server(3, sec_count_3, pulse_count_3,rec_exsist_3);
          rec_exsist_3 = 0;
          sec_count_3 = 0;
          pulse_count_3 = 0;
        }
   } 
}

void send_data_to_server(int seneNo,int procSec, int pulseCount, int transType){

  if (client.connect(server, 80)) {
    
    //Serial.println("-> Connected");
    
    client.print( "GET /BondaASP.aspx?");
    client.print("prm=100");
    client.print("&&");
    client.print("ControlNo=A00000001");
    client.print("&&");
    client.print("SensorNo=");
    client.print(seneNo);
    client.print("&&");
    client.print("TransTime=");
    client.print("24/06/2016%2020:00:00");
    client.print("&&");
    client.print("delaySec=");
    client.print(0);
    client.print("&&");
    client.print("procSec=");
    client.print(procSec);
    client.print("&&");
    client.print("pulseCount=");
    client.print(pulseCount);
    client.print("&&");
    client.print("transType=");
    client.print(transType);

    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
    
    delay(1000); 
    delay_time = delay_time - 1000;   
    
  } 
  //else 
  //  Serial.println("--> connection failed/n");
}

How can a different second be at the same time?

To work with multi-byte-valatile data, you have to make a copy with interrupts disabled.
This is true both for reading and writing.

Your sei(); in setup is superfluous, interrupts are enabled in that state already.

Read about interrupts Gammon Forum : Electronics : Microprocessors : Interrupts

Hi,

The time come from the server (when it receive the request) and there is delay of a second between the transaction of sensor 1 to db and sensor 2 to db.

see in the end of "void send_data_to_server()"

but we could see in post #9 the in the arduino_events_output.png attached file (before the change)
that both counters raised before entering the main block
if(currentTime >= (cloopTime + delay_time)){
}

It help ?

You were/are not handling the volatile data properly.
Until this is fixed, you can see any result.

After reading your link about interrupts,
Any hint how to handle volatile variable properly ?

From the posted link:

Why disable interrupts?

There may be time-critical pieces of code that you don't want interrupted, for example by a timer interrupt.

Also if multi-byte fields are being updated by an ISR then you may need to disable interrupts so that you get the data "atomically". Otherwise one byte may be updated by the ISR while you are reading the other one.

An example:

void setup() {
  Serial.begin(115200);
  Serial.println(F("Wait for error...\r\n\r\nfrom - to - difference"));
  // set up Timer 1
  TCCR1A = 0;                        // normal operation
  TCCR1B = bit(WGM12) | bit(CS10);   // CTC, no pre-scaling
  OCR1A =  7999;                     // compare A register value for 2 kHz
  TIMSK1 = bit (OCIE1A);             // interrupt on Compare A Match
}
volatile unsigned long Counter;

ISR(TIMER1_COMPA_vect)
{
  Counter++;
}

void loop() {
  static unsigned long before;
  static unsigned long current;

  // Counter is incremented with 2 kHz
  //  noInterrupts(); // uncomment to make it work
  current = Counter;
  //  interrupts();   // uncomment to make it work

  unsigned long differenz = current - before;

  // loop is so fast, you should never see a difference  bigger than 1

  if (differenz > 1) {
    Serial.print(before, HEX);
    Serial.write(' ');
    Serial.print(current, HEX);
    Serial.print(F(" - "));
    Serial.println(differenz, HEX);
  }
  before = current;
}
Wait for error...

from - to - difference
AFF BFF - 100
BFF B01 - FFFFFF02
BFF CFF - 100
CFF C01 - FFFFFF02
21FF 22FF - 100
22FF 2201 - FFFFFF02
2201 2203 - 2
4FFF 50FF - 100
50FF 5001 - FFFFFF02
5001 5003 - 2
67FF 68FF - 100
68FF 6801 - FFFFFF02
6801 6803 - 2
78FF 79FF - 100
79FF 7901 - FFFFFF02
7901 7903 - 2