Flow Sensor readout on different pin?

i hope this is the right category... if not please move it...
I have trouble with the reading of an flow-sensor.
it only works on pin2 of the arduino mega.
but that pin is occupied by an lcd-shield. wanted to use any pin from 14 up.
is there any chance i can do that?

// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                              
int hallsensor = 2;    //The pin location of the sensor

void rpm ()     //This is the function that the interupt calls
{
 NbTopsFan++;  //This function measures the rising and falling edge of the

//hall effect sensors signal
}
// The setup() method runs once, when the sketch starts
void setup() //
{
 pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
 Serial.begin(9600); //This is the setup function where the serial port is

//initialised,
 attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()    
{
 NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
 sei();            //Enables interrupts
 delay (1000);      //Wait 1 second
 cli();            //Disable interrupts
 Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate

//in L/hour
 Serial.print (Calc, DEC); //Prints the number calculated above
 Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
}

The Mega supports external interrupts on pins 2, 3, 18, 19, 20, and 21.

it only works on pin2 of the arduino mega.

Are you sure? You should be able to connect it to many other pins, and change the definition in your code accordingly.

 cli();            //Disable interrupts
 Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate

Two points here: why are you disabling interrupts all the time? aren't you going to miss some counts while you do serial output? and ideally you shouldn't be using floating-point numbers for an integer calculation. Just use (NbTopsFan * 8 )

I tried all pins by changing the number "int hallsensor = x"
the only one that worked was pin2 or is there anything else to be changed?
sorry i´m not so familiar with the coding yet..

the code is pretty much copy and pase from a different website thats why i left averything as is.

if you have any recommendations i would be happy to change them..
oh and the output does not need to be exact, because it is pretty much just a check if the pump is working...

Just a guess, I haven't tried this. But, did you change the interrupt number in the attach statement?

int hallsensor = 2; // or 3, 18, 19, 20, 21 But NOT 14 per PaulS

void setup() {
  attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, RISING);

}

so actual setting is:

int hallsensor = 19


and 

attachInterrupt(4, rpm, RISING)

also tried
attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, RISING);

still not showing signal

Did you actually move the sensor to the correct pin?

PaulS:
Did you actually move the sensor to the correct pin?

yes i did. i use a breadboard and just switch the output-pin to the needed terminal.

ok.. the problem is in the program i am using the sensor for.. in the testprogram it worked... but it does not work in this sketch...

//Temperatursensor
#include <OneWire.h>
#include <DallasTemperature.h>
 
OneWire  ds(17); //pin für ds1820

int Temp1;
int Temp2;
int Temp3;
int Temp4;
int Temp5;

//DeviceAdressen der einzelnen ds1820 Temperatursensoren angeben. (loop anpassen)
DeviceAddress sensor1 = { 0x28, 0x70, 0x42, 0x8, 0x0, 0x0, 0x80, 0x7C };
DeviceAddress sensor2 = { 0x28, 0xFF, 0xC2, 0xA7, 0x74, 0x16, 0x4, 0x90 }; //Knoten
DeviceAddress sensor3 = { 0x28, 0xFF, 0xD, 0xF7, 0x74, 0x16, 0x4, 0x65 };
DeviceAddress sensor4 = { 0x28, 0xFF, 0x35, 0xA1, 0x73, 0x16, 0x05, 0x4A };
DeviceAddress sensor5 = { 0x28, 0xFF, 0x44, 0xB8, 0x74, 0x16, 0x4, 0x65 };
char sensorFName[] = "Wasserfluss: ";
char sensor1Name[] = "CPU:         ";
char sensor2Name[] = "Wasser:      ";
char sensor3Name[] = "nix:3        ";
char sensor4Name[] = "nix:4        ";
char sensor5Name[] = "nix:5        ";

//ende TEMP
//Durchflusssensor
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                              
int hallsensor = 19;    //The pin location of the sensor

void rpm ()     //This is the function that the interupt calls
{
 NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
}
//ende Flow

// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.

// Modified for SPFD5408 Library by Joao Lopes
// Version 0.9.2 - Rotation for Mega and screen initial

// *** SPFD5408 change -- Begin
#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
// *** SPFD5408 change -- End

// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0xFFFF
#define BLUE    0xFFE0
#define RED     0x07FF
#define GREEN   0xF81F
#define CYAN    0x07E0
#define MAGENTA 0xF800
#define YELLOW  0x001F
#define WHITE   0x0000

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;

// -- Setup

void setup(void) {
  
  Serial.begin(9600);
  
 
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
  progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
  
#endif

  tft.reset();
 
    
  tft.begin(0x9341); // SDFP5408

  tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial

  
//Temp anfang
}
void writeTimeToScratchpad(byte* address)
{
  //reset the bus
  ds.reset();
  //select our sensor
  ds.select(address);
  //CONVERT T function call (44h) which puts the temperature into the scratchpad
  ds.write(0x44,1);
  //sleep a second for the write to take place
  delay(1000);
}

void readTimeFromScratchpad(byte* address, byte* data)
{
  //reset the bus
  ds.reset();
  //select our sensor
  ds.select(address);
  //read the scratchpad (BEh)
  ds.write(0xBE);
  for (byte i=0;i<9;i++){
    data[i] = ds.read();
  }
}
 
float getTemperature(byte* address)
{
int tr;
byte data[12];

writeTimeToScratchpad(address);

readTimeFromScratchpad(address,data);

//put in temp all the 8 bits of LSB (least significant byte)
tr = data[0];

if (address[0] == 0x10) // DS18S20
{
//check for negative temperature
if (data[1] > 0x80)
{
tr = !tr + 1; //two’s complement adjustment
tr = tr * -1; //flip value negative.
}

//drop bit 0
tr = tr >> 1;

//COUNT PER Celsius degree (10h)
int cpc = data[7];
//COUNT REMAIN (0Ch)
int cr = data[6];

return tr - (float)0.25 + (cpc - cr)/(float)cpc;
}
else // DS18B20
{
return ((data[1] << 8) + tr) * (float)0.0625;
}

 
  //COUNT PER Celsius degree (10h)
  int cpc = data[7];
  //COUNT REMAIN (0Ch)
  int cr = data[6];
 
  //drop bit 0
  tr = tr >> 1;
 
  return tr - (float)0.25 + (cpc - cr)/(float)cpc;


// temp ende
//flow beginn
{
 pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
 Serial.begin(9600); //This is the setup function where the serial port is initialised,
 attachInterrupt(4, rpm, RISING); //and the interrupt is attached
 }
//flow ende
}
void loop(void) {

//Temp beginn
{
  float temp1 = getTemperature(sensor1);
  float temp2 = getTemperature(sensor2);
  float temp3 = getTemperature(sensor3);
  float temp4 = getTemperature(sensor4);
  float temp5 = getTemperature(sensor5);
  
Temp1 = temp1;
Temp2 = temp2;
Temp3 = temp3;
Temp4 = temp4;
Temp5 = temp5;
   
  Serial.print(sensor1Name);
  Serial.print(temp1);
  Serial.println(" Celsius");
 
  Serial.print(sensor2Name);
  Serial.print(temp2);
  Serial.println(" Celsius");
 
  Serial.print(sensor3Name);
  Serial.print(temp3);
  Serial.println(" Celsius");
 
  Serial.print(sensor4Name);
  Serial.print(temp4);
  Serial.println(" Celsius");

  Serial.print(sensor5Name);
  Serial.print(temp5);
  Serial.println(" Celsius");
 
  Serial.println();
  delay(500);
}
//Temp Ende
//Flow Beginn
NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
 
 delay (500);      //Wait 0,5 second
 
 Calc = (NbTopsFan * 8); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
 Serial.print (Calc, DEC); //Prints the number calculated above
 Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
//flow ende

  
  for(uint8_t rotation=3; rotation<4; rotation++) {
    tft.setRotation(rotation);
    testText();
    delay(500);
  }
}

unsigned long testFillScreen() {
  unsigned long start = micros();
  tft.fillScreen(BLACK);
  tft.fillScreen(RED);
  tft.fillScreen(GREEN);
  tft.fillScreen(BLUE);
  tft.fillScreen(BLACK);
  return micros() - start;
}

unsigned long testText() 
{
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE);  tft.setTextSize(3);
  tft.print(sensor1Name);
  tft.setTextColor(YELLOW); tft.setTextSize(3);
  tft.println(Temp1);
  tft.setTextColor(RED);    tft.setTextSize(3);
  tft.print(sensor2Name);
  tft.println(Temp2);
  tft.setTextColor (BLUE);
  tft.print(sensor3Name);
  tft.println(Temp3);
  tft.setTextColor (GREEN);
  tft.print(sensor4Name);
  tft.println(Temp4);
  tft.setTextColor (CYAN);
  tft.print(sensor5Name);
  tft.println(Temp5);
  tft.setTextColor (MAGENTA);
  tft.print(sensorFName);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println(Calc, DEC);
  tft.setTextSize(2);
  tft.println();
  tft.setTextSize(2);
  tft.println();
  return micros() - start;
}

This has somehow drifted to the end of getTemperature() after a return statement where it will never be executed.

 {
    pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
    Serial.begin(9600); //This is the setup function where the serial port is initialised,
    attachInterrupt(4, rpm, RISING); //and the interrupt is attached
  }

this solved my problem! Thanks a lot!!!!

oqibidipo:
This has somehow drifted to the end of getTemperature() after a return statement where it will never be executed.

 {

pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
    Serial.begin(9600); //This is the setup function where the serial port is initialised,
    attachInterrupt(4, rpm, RISING); //and the interrupt is attached
  }