arduino uno R3输入引脚,带有gsm屏蔽

时间:2016-04-03 07:22:15

标签: arduino-uno

在我的项目中,我需要读取水流量传感器的输入数据。当我用针2或3连接水流时,它工作得很好,但当我将它连接到任何其他针时,它不起作用。这成为一个问题,因为我需要使用GSM屏蔽,你知道为arduino和调制解调器保留的引脚2,3和7

代码:

#include <LiquidCrystal.h>    // initialize the library with the numbers of the interface pins 

LiquidCrystal lcd(5, 6, 9, 10, 11, 12);

volatile int  TopsFan; //measuring the rising edges of the signal

int result;                               

int pin = 2;    //The pin location of the sensor



void rpm ()     //This is the function that the interrupt calls 

{ 

  TopsFan++;  //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(pin, INPUT); //initializes digital pin 2 as an input

  Serial.begin(9600); //This is the setup function where the 

serial port is initialized(USB port)

  attachInterrupt(0, rpm, RISING); // the interrupt is attached

} 

 lcd.begin(16, 2);     // set up the LCD's number of columns and rows

lcd.print("The water flow: ");    // Print a message to the LCD.


// the loop() method runs over and over again,

// as long as the Arduino has power

void loop ()    

{

  TopsFan = 0;   //Set TopsFan to 0 ready for calculations

  sei();      //Enables interrupts

  delay (1000);   //Wait 1 second

  cli();      //Disable interrupts

  result =  (TopsFan * 60 / 7.5); //(Pulse frequency x 60) / 

7.5Q, = flow rate in L/min 

 lcd.setCursor(0, 1);   //prepare the cursor on the screen

 lcd.print(result, DEC);  //Prints the number calculated above

 lcd.print(" L/min\r\n");  //Prints "L/min" and returns a  new 

line

}

1 个答案:

答案 0 :(得分:0)

请阅读https://www.arduino.cc/en/Reference/AttachInterrupt

...

...

attachInterrupt的第一个参数是一个中断号。通常,您应该使用digitalPinToInterrupt(引脚)将实际数字引脚转换为特定的中断号。例如,如果连接到引脚3,请使用digitalPinToInterrupt(3)作为attachInterrupt的第一个参数。

Board                               Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based    2, 3

...

...

看起来您需要尝试不同的方法,可能完全绕过中断。如下所示:

#include <LiquidCrystal.h>    // initialize the library with the numbers of the interface pins 

int count_rpm(void) ;

LiquidCrystal lcd(5, 6, 9, 10, 11, 12);

int flow_pin = 5 ;    //The pin location of the sensor (5 or whatever suits you)

// The setup() method runs once, when the sketch starts

void setup()  

{ 
  pinMode(flow_pin, INPUT); //initializes digital pin 2 as an input
  Serial.begin(9600); //This is the setup function where the serial port is initialized(USB port)

  lcd.begin(16, 2);     // set up the LCD's number of columns and rows
  lcd.print("The water flow: ");    // Print a message to the LCD.
} 



// the loop() method runs over and over again,

// as long as the Arduino has power

void loop ()    
{
int result;                               

 result =  (count_rpm() * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/min 
 lcd.setCursor(0, 1);   //prepare the cursor on the screen
 lcd.print(result, DEC);  //Prints the number calculated above
 lcd.print(" L/min\r\n");  //Prints "L/min" and returns a  new line
}

int count_rpm(void)
{
  long start = millis() ;
  int count = 0 ;
  boolean current_state ;
  boolean previous_state = digitalRead(flow_pin) ;
  for( ; millis()-start <= 1000 ; )
  {
     current_state = digitalRead(flow_pin) ;
     if (current_state && !previous_state)
        count++ ;
     previous_state = current_state ;
  }
  return count ;
}