Estimados, tengo un problema que se genera al momento de activar las interrupciones en mi código, estoy usando un Arduino DUE y 3 tipos de sensores (un potenciómetro, yf-s201, jsn-sr04 y uno de presión 1,2 Mpa (2104)). en este código se están enviando los datos entregados por estos sensores vía Modbus TCP a un PLC, la lectura individual de estos sensores funciona sin problemas, también de la lectura de todos a excepción de cuando el de flujo, he aquí la duda, al llamar a las funciones para que activen y desactivan interrupciones el Arduino queda "Colgado" y dejamos de recibir datos. desde ya agradecido de todos.
//*Variables Modbus*//
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
uint8_t mac[] = { 0x18, 0xC0, 0x4D, 0x07, 0x44, 0x18 };
uint8_t ip[] = { 172, 16, 1, 15};
//uint8_t gateway[] = { 172, 16, 1, 1 };
uint8_t subnet[] = { 255, 255, 255, 0 };
//*Variables Distancia1*//
#define trigPin_D1 6
#define echoPin_D1 5
long duration1;
int distance1;
//*Variables Presion1*//
int a1; //
float v1; //
float p1; //
float psi1;// La presión PSI.
//*Variables Flujo1*//
const int sensorPin = 22;
const int measureInterval = 2500;
volatile int pulseConter;
const float factorK = 7.5;
float frequency ;
float flow1;
//*********************//
void setup()
{
SPI.begin();
Ethernet.begin(mac, ip, subnet);
Serial.begin(9600);
pinMode(trigPin_D1, OUTPUT);//Distancia1
pinMode(echoPin_D1, INPUT);//Distancia1
attachInterrupt(digitalPinToInterrupt(sensorPin), ISRCountPulse, RISING);//Flujometro
delay(1000);
}
void loop()
{
loopDistancia1 ();
//delay(2000);
loopPresion1 ();
//delay(2000);
loopFlujo1();
//delay(5000);
Mb.Run();
Mb.R[0] = analogRead(A0)*100;
Mb.R[1] = distance1*100;
Mb.R[2] = psi1*100;
Mb.R[3] = flow1*100;
//Serial.println(Mb.R[0]);
//Serial.println(Mb.R[1]);
//Serial.println(Mb.R[2]);
// Serial.println(Mb.R[3]);
}
void loopDistancia1()
{
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin_D1, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin_D1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_D1, LOW);
// Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin_D1, HIGH);
// Calculate the distance:
distance1 = duration1*0.034/2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
//Serial.print("Distance = ");
//Serial.print(distance1);
//Serial.println(" cm");
delay(1000);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)//
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void loopPresion1() {
// Obtengo el valor del ADC.
a1 = analogRead(A1);
v1 = a1*5.0/1023.0;
p1 = mapfloat(v1, 0.5, 4.5, 0.0, 12.0);
psi1 = (p1*14.5);
//Serial.print(psi1,3);
delay(1000);
}
void ISRCountPulse()//Pulso Flujometro
{
pulseConter++;
}
float GetFrequency()
{
pulseConter = 0;
interrupts();
delay(measureInterval);
noInterrupts();
return (float)pulseConter * 1000 / measureInterval;
//delay(5000);
}
void loopFlujo1()
{
// obtener frecuencia en Hz
frequency = GetFrequency();
// calcular caudal L/min
//delay(2000);
flow1 = frequency / factorK;
// delay(2000);
}