每隔一段时间怎样才能读取arduino水流量传感器的流量?

每隔一段时间怎样才能读取arduino水流量传感器的流量?

问题描述:

我正在尝试阅读或确定与arduino水流量。在这一刻我连接:每隔一段时间怎样才能读取arduino水流量传感器的流量?

  1. Arduino UNO。
  2. 的Arduino GSM SIM900模块
  3. Arduino的流量传感器

这里是我的代码:

#include <SoftwareSerial.h> 
SoftwareSerial SIM900(7, 8); // Configura el puerto serial para el SIM900 

char incoming_char=0; //Variable que guarda los caracteres que envia el SIM900 
int salir = 0; 

byte statusLed = 13; 

byte sensorInterrupt = 0; // 0 = digital pin 2 
byte sensorPin  = 2; 

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per 
// litre/minute of flow. 
float calibrationFactor = 4.5; 

volatile byte pulseCount; 

float flowRate; 
unsigned int flowMilliLitres; 
unsigned long totalMilliLitres; 
unsigned long oldTime; 

void setup() 
{ 
SIM900.begin(19200); //Configura velocidad serial para el SIM900 
delay(25000); //Retardo para que encuentra a una RED 
Serial.begin(19200); //Configura velocidad serial para el Arduino 
Serial.println("OK"); //Mensaje OK en el arduino, para saber que todo va bien. 

// Set up the status LED line as an output 
    pinMode(statusLed, OUTPUT); 
    digitalWrite(statusLed, HIGH); // We have an active-low LED attached 

    pinMode(sensorPin, INPUT); 
    digitalWrite(sensorPin, HIGH); 

    pulseCount  = 0; 
    flowRate   = 0.0; 
    flowMilliLitres = 0; 
    totalMilliLitres = 0; 
    oldTime   = 0; 

    // The Hall-effect sensor is connected to pin 2 which uses interrupt 0. 
    // Configured to trigger on a FALLING state change (transition from HIGH 
    // state to LOW state) 
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING); 
} 

void mensaje_sms() 
//Funcion para mandar mensaje de texto 
{ 

SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message 
delay(100); 
SIM900.println("AT+CMGS=\"3003043789\""); // recipient's mobile number, in international format 
delay(100); 
//SIM900.println("Saludos desde HetPro"); // message to send 
// Print the flow rate for this second in litres/minute 
    //SIM900.print("Flow rate: " + int(flowRate) + "L/min" + "\t"); 
    SIM900.print("Flow rate: "); 
    delay(100); 
    SIM900.print(int(flowRate)); // Print the integer part of the variable 
    delay(100); 
    SIM900.print("L/min"); 
    delay(100); 
    SIM900.print("\t");  // Print tab space 
delay(100); 
    // Print the cumulative total of litres flowed since starting 
    //SIM900.print("Output Liquid Quantity: " + totalMilliLitres + "mL" + "L" + totalMilliLitres/1000 + "L"); 
    SIM900.print("Output Liquid Quantity: "); 
    delay(100);   
    SIM900.print(totalMilliLitres); 
    delay(100); 
    SIM900.println("mL"); 
    delay(100); 
    SIM900.print("\t");  // Print tab space 
    delay(100); 
    SIM900.print(totalMilliLitres/1000); 
    delay(100); 
    SIM900.print("L"); 

delay(100); 
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26 //Comando de finalizacion 
delay(100); 
SIM900.println(); 
delay(5000); // Tiempo para que se envie el mensaje 
Serial.println("SMS sent successfully"); 
} 


void loop() 
{ 
    if((millis() - oldTime) > 1000) // Only process counters once per second 
    { 
    detachInterrupt(sensorInterrupt); 
    flowRate = ((1000.0/(millis() - oldTime)) * pulseCount)/calibrationFactor; 
    oldTime = millis(); 

    flowMilliLitres = (flowRate/60) * 1000; 

    totalMilliLitres += flowMilliLitres; 

    unsigned int frac; 

    mensaje_sms(); //Envia mensaje 
    pulseCount = 0; 
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING); 
    } 
    while(1); // Esperate por tiempo indefinido 
} 

/* 
Insterrupt Service Routine 
*/ 
void pulseCounter() 
{ 
    // Increment the pulse counter 
    pulseCount++; 
} 

我想给每分钟水流量传感器的价值,但我只是收到1条短信,也许代码有问题。

我会帮助你解决这个问题。

感谢。

嗨问题是在代码中的最后一次,删除行。 (1);(3) // Esperate por tiempo indefinido