|

楼主 |
发表于 2021-7-9 14:56
|
显示全部楼层
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程) 实验八十八: LCD1602A液晶屏5V显示模块(IIC/I2C接口)
项目二十一:水流量传感器、5V继电器模块和LCD1602 I2C模块
实验接线:Uno D2接流量传感器OUT,继电器接D4
Arduino------LCD1602
5V-------------VCC
GND-----------GND
A4-----------SDA IIC 数据线
A5-----------SCL IIC 时钟线
实验开源代码
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验八十八: LCD1602A液晶屏5V显示模块(IIC/I2C接口)
- 项目二十一:水流量传感器、5V继电器模块和LCD1602 I2C模块
- 实验接线:Uno D2接流量传感器OUT,继电器接D4
- Arduino------LCD1602
- 5V-------------VCC
- GND-----------GND
- A4-----------SDA IIC 数据线
- A5-----------SCL IIC 时钟线
- */
- #include <LiquidCrystal_I2C.h> //include LiquidCrystal Library
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- #define FLOWSENSORPIN 2 //连接到 Arduino 数字引脚 2 的水流传感器
- #define relayPin 4 // 连接到 Arduino 数字引脚 4 的 5v 继电器模块
- volatile uint16_t pulses = 0; // count how many pulses
- volatile uint8_t lastflowpinstate; // track the state of the pulse pin
- volatile uint32_t lastflowratetimer = 0; // you can try to keep time of how long it is between pulses
- volatile float flowrate; // and use that to calculate a flow rate
- // Interrupt is called once a millisecond, looks for any pulses from the sensor!
- SIGNAL(TIMER0_COMPA_vect) {
- uint8_t x = digitalRead(FLOWSENSORPIN);
- if (x == lastflowpinstate) {
- lastflowratetimer++;
- return; // nothing changed!
- }
- if (x == HIGH) {
- //low to high transition!
- pulses++;
- }
- lastflowpinstate = x;
- flowrate = 1000.0;
- flowrate /= lastflowratetimer; // in hertz
- lastflowratetimer = 0;
- }
- void useInterrupt(boolean v) {
- if (v) {
- // Timer0 is already used for millis() - we'll just interrupt somewhere
- // in the middle and call the "Compare A" function above
- OCR0A = 0xAF;
- TIMSK0 |= _BV(OCIE0A);
- } else {
- // do not call the interrupt function COMPA anymore
- TIMSK0 &= ~_BV(OCIE0A);
- }
- }
- void setup() {
- Serial.begin(9600);
- Serial.print("---Water flow sensor---");
- lcd.init(); // 初始化液晶显示器
- lcd.backlight();
- lcd.begin(16, 2); //16X2 lcd display
- lcd.setBacklight(HIGH);
- lcd.setCursor(0, 0); //setting display position
- lcd.print("Aqua counter");
- pinMode(FLOWSENSORPIN, INPUT); //sets the FLOWSENSORPIN as an INPUT
- pinMode(relayPin, OUTPUT);//sets the relayPin as OUTPUT
- digitalWrite(relayPin, LOW);
- digitalWrite(FLOWSENSORPIN, HIGH);//optional Internal Pull-Up
- lastflowpinstate = digitalRead(FLOWSENSORPIN);
- useInterrupt(true);
- delay(2000);
- lcd.clear();
- }
- void loop()
- {
- lcd.setCursor(0, 0);
- lcd.print("Pulses:"); lcd.print(pulses, DEC);
- lcd.print(" Hz:");
- lcd.print(flowrate);
- //lcd.print(flowrate);
- Serial.print("Freq: "); Serial.println(flowrate);
- Serial.print("Pulses: "); Serial.println(pulses, DEC);
- // if a plastic sensor use the following calculation
- // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
- // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
- // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
- // Liters = Pulses / (7.5 * 60)
- float liters = pulses;
- liters /= 7.5;
- liters /= 60.0;
- /*
- // if a brass sensor use the following calculation
- float liters = pulses;
- liters /= 8.1;
- liters -= 6;
- liters /= 60.0;
- */
- Serial.print(liters); Serial.println(" Liters");
- lcd.setCursor(0, 1);
- lcd.print(liters); lcd.print(" Litres ");
- if (liters >= 0.15) //water limit set
- {
- digitalWrite(relayPin, HIGH);
- }
- else {
- digitalWrite(relayPin, LOW);
- }
- delay(2000);
- lcd.clear();
- }
复制代码
|
|