Go Down

Topic: Pin Change interrupt for flow sensor(s) (Read 90 times) previous topic - next topic

dpetican

So I read this:

https://playground.arduino.cc/Main/PinChangeInterrupt

But looking at this pinout I'm confused about the D0 to D8 group. Only PCINT8 is listed.  What are INT4 and INT5?

http://pighixxx.com/megapdf.pdf

However, I'm currently using INT4 connected to a flow sensor to count pulses and it seems to work good.  I need to add a second flow sensor so should I add the connection on another pin group?  Probably PCINT7 (pin 13). Should I disable interrupts when doing the count++ or is the code short enough to not matter?  With one it may not matter, but with 2 interrupts like this is it required? FT1 variables (in caps) are globals defined in another module. Thanks.

Code: [Select]

volatile unsigned long count = 0UL;

void readFlow() {
  unsigned long currentCount;
  float flow;
  static unsigned long startTime = millis();
  unsigned long timePassed;

  // Has at least 1sec passed?
  timePassed = millis() - startTime;
  if (timePassed > ONESECOND * 1000UL) {
      // Reset count
      currentCount = count;
      count = 0UL;
      startTime = millis();
  }
  else
    return;

  // Convert pulses to mL
 flow = (float)currentCount * 0.45f; // 0.45 = 1000 ml/2200 pulses; result = ml

  // Totalize flow
  FT1_PURGE += flow / 1000.0f; // convert ml to L and totalize
 
  // Calculate flow rate in L/min
  FT1_VALUE = flow * 60.0f; // convert ml/1000ms to ml/min
  FT1_VALUE = FT1_VALUE / 1000.0f; // convert ml/min to L/min
}

void resetFlowRate() {
  FT1_VALUE = 0.0f;
}

void resetPurgeFlowTotal() {
  FT1_PURGE = 0.0f;
}

void countPulses() {
     count++;
  }

void setupFlow() {
  pinMode(FT1_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(FT1_PIN), countPulses, RISING);
}

cattledog

#1
Jan 21, 2018, 11:51 pm Last Edit: Jan 21, 2018, 11:59 pm by cattledog
Quote
But looking at this pinout I'm confused about the D0 to D8 group. Only PCINT8 is listed.  What are INT4 and INT5?
A little more reading is required. There are two types of pin interrupts available--pin change interrupts and external interrupts.

https://playground.arduino.cc/Code/Interrupts

http://gammon.com.au/interrupts

Your current code is using an external interrupt, not a pin change interrupt.

Here's some code from another thread which processes interrupts from three flow sensors. The code could be simplified with arrays, but you might get some ideas. Using the attachInterrupt(digitalPinToInterrupt(), isr, mode)  is good, especially with the Mega, where the attach interrupt numbers of the IDE are not the same as the interrupt vectors of the processor.
Code: [Select]

//For FlowSensor_1   NWA
byte sensorInterrupt_1 = 0;  // 0 = digital pin 2
byte sensorPin_1       = 2;
//float calibrationFactor_1 = 3.8;
float calibrationFactor_1 = 5.5;
volatile byte pulseCount_1;
byte pulseCount1;//protected transfer
float flowRate_1;
unsigned int flowMilliLitres_1;
unsigned long totalMilliLitres_1;

unsigned long totalLitres_1;


//For FlowSensor_2   Ble
byte sensorInterrupt_2 = 1;  // 1 = digital pin 3
byte sensorPin_2       = 3;
//float calibrationFactor_2 = 5.44;
float calibrationFactor_2 = 6.44;
volatile byte pulseCount_2;
byte pulseCount2;//protected transfer
float flowRate_2;
unsigned int flowMilliLitres_2;
unsigned long totalMilliLitres_2;
unsigned long totalLitres_2;


//For FlowSensor_3 Gri
byte sensorInterrupt_3 = 5;  // 5 = digital pin 18
byte sensorPin_3       = 18;
float calibrationFactor_3 = .59;
volatile byte pulseCount_3;
byte pulseCount3;//protected transfer
float flowRate_3;
unsigned int flowMilliLitres_3;
unsigned long totalMilliLitres_3;
unsigned long totalLitres_3;


//sensor reading and print timing
unsigned long readInterval = 1000;
unsigned long lastRead;


void setup() {

  Serial.begin(115200);

  //Setup_FlowSensor_1
  pinMode(sensorPin_1, INPUT);
  digitalWrite(sensorPin_1, HIGH);
  pulseCount_1        = 0;
  flowRate_1          = 0.0;
  flowMilliLitres_1   = 0;
  totalMilliLitres_1  = 0;
  totalLitres_1 = 0;
  attachInterrupt(sensorInterrupt_1, pulseCounter_1, FALLING);


  //Setup_FlowSensor_2
  pinMode(sensorPin_2, INPUT);
  digitalWrite(sensorPin_2, HIGH);
  pulseCount_2        = 0;
  flowRate_2          = 0.0;
  flowMilliLitres_2   = 0;
  totalMilliLitres_2  = 0;
  totalLitres_2 = 0;
  attachInterrupt(sensorInterrupt_2, pulseCounter_2, FALLING);


  //Setup_FlowSensor_3
  pinMode(sensorPin_3, INPUT);
  digitalWrite(sensorPin_3, HIGH);
  pulseCount_3        = 0;
  flowRate_3          = 0.0;
  flowMilliLitres_3   = 0;
  totalMilliLitres_3  = 0;
  totalLitres_3 = 0;
  attachInterrupt(sensorInterrupt_3, pulseCounter_3, FALLING);
}

void flowSensors() {

  if ((millis() - lastRead) > readInterval)   // process counters once per second
  {
    lastRead += readInterval;
    noInterrupts();
    pulseCount1 = pulseCount_1;
    pulseCount_1 = 0;
    pulseCount2 = pulseCount_2;
    pulseCount_2 = 0;
    pulseCount3 = pulseCount_3;
    pulseCount_3 = 0;
    interrupts();

    flowRate_1 = (pulseCount1) / calibrationFactor_1;
    flowRate_2 = (pulseCount2) / calibrationFactor_2;
    flowRate_3 = (pulseCount3) / calibrationFactor_3;

    flowMilliLitres_1 = (flowRate_1 / 60) * 1000;
    totalMilliLitres_1 += flowMilliLitres_1;
    totalLitres_1 = (totalMilliLitres_1 / 1000);

    flowMilliLitres_2 = (flowRate_2 / 60) * 1000;
    totalMilliLitres_2 += flowMilliLitres_2;
    totalLitres_2 = (totalMilliLitres_2 / 1000);

    flowMilliLitres_3 = (flowRate_3 / 60) * 1000;
    totalMilliLitres_3 += flowMilliLitres_3;
    totalLitres_3 = (totalMilliLitres_3 / 1000);

    Serial.print("FlowRate_1= ");
    Serial.print(flowRate_1);
    Serial.print("   Total_1= ");
    Serial.println(totalLitres_1);
    Serial.println("  ");
    //pulseCount_1 = 0;

    Serial.print("FlowRate_2= ");
    Serial.print(flowRate_2);
    Serial.print("   Total_2= ");
    Serial.println(totalLitres_2);
    Serial.println("  ");
    //pulseCount_2 = 0;

    Serial.print("FlowRate_3= ");
    Serial.print(flowRate_3);
    Serial.print("   Total_3= ");
    Serial.println(totalLitres_3);
    Serial.println("  ");
    //pulseCount_3 = 0;
  }
}

void pulseCounter_1() {
  pulseCount_1++;
}

void pulseCounter_2() {
  pulseCount_2++;
}

void pulseCounter_3() {
  pulseCount_3++;
}


void loop() {

  flowSensors();

}

Go Up