Hey folks. I'm working on a project which involves using two water flow sensors. i have interfaced it using the GIGA R1 board. I'm not very much familiar with the interrupts so i just added a pre-existing flow sensor code into my main code which looks like this:
//Libraries
#include <Wire.h>
#include <WiFi.h>
#include <math.h>
#include "Variables.h"
#include "PinAssign.h"
#include "MyConstants.h"
#include "ExternalFlash.h"
#include "thingProperties.h"
// Interrupt Service Routines
void flow1ISR() {
flow1++;
}
void flow2ISR() {
flow2++;
}
void select_channel(uint8_t channel){
Wire.beginTransmission(PCA9548A);
Wire.write(1 << channel);
Wire.endTransmission();
}
int count;
void setup(){
Serial.begin(9600);
Wire.begin();
delay(1000);
//WiFi connection
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Arduino cloud setup
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Wait for a successful connection to Arduino IoT Cloud
Serial.print("Waiting for Arduino IoT Cloud connection...");
while (!ArduinoCloud.connected()) {
ArduinoCloud.update();
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to Arduino IoT Cloud");
//Enable USB-A port
pinMode(PA_15, OUTPUT);
//Connect GIGA to USB port
digitalWrite(PA_15,HIGH);
delay(1000);
msd.connect();
Serial.print("Connecting to port...");
while(!msd.connect()){
Serial.print(".");
}
Serial.println("");
//Float pins
pinMode(top1,INPUT); //Float 1 top
pinMode(bottom1,INPUT); //Float 1 bottom
pinMode(top2,INPUT); //Float 2 top
pinMode(bottom2,INPUT); //Float 2 bottom
Serial.println("Float pins initiated\n");
//Flow Sensor pins
pinMode(FLOW_SENSOR_1, INPUT_PULLUP);
pinMode(FLOW_SENSOR_2, INPUT_PULLUP);
// Attach the interrupts
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_1), flow1ISR, RISING);
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_2), flow2ISR, RISING);
//Valve relays
pinMode(t1d,OUTPUT);
digitalWrite(t1d,LOW);
pinMode(t2d,OUTPUT);
digitalWrite(t2d,LOW);
// pinMode(t1r,OUTPUT);
// pinMode(t2r,OUTPUT);
pinMode(dt1,OUTPUT);
digitalWrite(dt1,LOW);
pinMode(dt2,OUTPUT);
digitalWrite(dt2,LOW);
// pinMode(rt1,OUTPUT);
// pinMode(rt2,OUTPUT);
Serial.println("Valve relays defined\n");
//12V Relays
// pinMode(h1,OUTPUT);
// pinMode(h2,OUTPUT);
//Serial.println("Heater relays defined\n");
//delay(1000);
//Pump Relays
pinMode(dsp,OUTPUT);
digitalWrite(dsp,LOW);
pinMode(drp,OUTPUT);
digitalWrite(drp,LOW);
// pinMode(rsp,OUTPUT);
// pinMode(rrp,OUTPUT);
pinMode(dpc1,OUTPUT);
digitalWrite(dpc1,LOW);
pinMode(dpc2,OUTPUT);
digitalWrite(dpc2,LOW);
Serial.println("Pump relays defined\n");
//Fan Relays
//pinMode(rf,OUTPUT);
pinMode(df,OUTPUT);
pinMode(fcu,OUTPUT);
Serial.println("Fan relays defined\n");
digitalWrite(df,LOW);
digitalWrite(fcu,LOW);
//Mount Cruzer-Blade
Serial.println("Mounting Cruzer Blade (16GB)...\n");
int err = usb.mount(&msd);
if (err) {
Serial.print("Error mounting Cruzer Blade ");
Serial.println(err);
while (1);
}
if(digitalRead(bottom1) == HIGH){
lowTrig1 = true;
tank1_active = true;
Serial.println("Default float 1");
}
else {
// Read the value of lowTrig1 from the file
FILE *f = fopen("/usb/lowTrig1.txt", "r");
if (f) {
char buffer[10];
fgets(buffer, sizeof(buffer), f);
fclose(f);
lowTrig1 = (buffer[0] == '1') ? 1 : 0;
prevLowTrig1 = lowTrig1;
Serial.println("Reading lowTrig1 from Cruzer Blade");
delay(500);
}
}
// Read the value of lowTrig2 from the file
FILE *g = fopen("/usb/lowTrig2.txt", "r");
if (g) {
char buffer[10];
fgets(buffer, sizeof(buffer), g);
fclose(g);
lowTrig2 = (buffer[0] == '1') ? 1 : 0;
prevLowTrig2 = lowTrig2;
Serial.println("Reading lowTrig2 from Cruzer Blade");
}
// // Configure the interrupt pins
// pinMode(FLOW_SENSOR_1, INPUT_PULLUP);
// pinMode(FLOW_SENSOR_2, INPUT_PULLUP);
previousTime = millis();
//SHT connection
//Precision setting
Precision = PRECISION_HIGH;
Heater = NO_HEATER;
// Initialize first sensor
select_channel(SENSOR1_CHANNEL);
Serial.println("Initializing first ES13511 sensor\n");
ES135xx_probe();
// Initialize second sensor
select_channel(SENSOR2_CHANNEL);
Serial.println("Initializing second ES13511 sensor\n");
ES135xx_probe();
//Temp sensor
// Start up the library
sensors.begin();
delay(1000);
}
void loop(){
//On-board timer
currentTime = millis();
if(WiFi.status() == WL_CONNECTED){
if(currentTime - lastupdate >= updateInterval){
Serial.println("\nCloud Update");
ArduinoCloud.update();
lastupdate = currentTime;
}
}
delay(1000);
if(!testswitch){
if(power_Switch){
Serial.println("\nSystem ON");
if(power){
digitalWrite (df,HIGH);
digitalWrite (fcu,HIGH);
digitalWrite(dsp, LOW);
digitalWrite(drp,LOW);
digitalWrite(t1d,LOW);
digitalWrite(dt1,LOW);
digitalWrite(t2d,LOW);
digitalWrite(dt2,LOW);
digitalWrite(dpc1,LOW);
digitalWrite(dpc2,LOW);
Serial.println("Fans ON");
serial_Monitor = "SYSTEM ON";
printonce = true;
power = false;
}
onFanswitchChange();
FloatSensorReading();
SensorReadings();
Dehumidification();
Cooling();
onResetChange();
}
else if (!power_Switch){
onPowerSwitchChange();
SensorReadings();
onFanswitchChange();
}
}
else{
onTestSwitchChange();
SensorReadings();
}
delay(1000);
}
Sensor reading function:
#include "PinAssign.h"
#include "MyConstants.h"
#include "Variables.h"
#include "thingProperties.h"
void SensorReadings(){
//SHT reading
extern float ES135xx_Temp;
extern float ES135xx_Hum;
//Storage room sensor
select_channel(SENSOR1_CHANNEL);
ES135xx_read_T_H();
storage_hum = ES135xx_Hum;
storage_temp = ES135xx_Temp;
Serial.print("Storage Temp = " + String(ES135xx_Temp) + " C " + " Storage RH = " + String(ES135xx_Hum) + "% | ");
storage_Humidity = storage_hum;
storage_Temperature = storage_temp;
// Utility sensor
select_channel(SENSOR2_CHANNEL);
ES135xx_read_T_H();
utility_hum = ES135xx_Hum;
utility_temp = ES135xx_Temp;
Serial.println("Utility Temp = " + String(ES135xx_Temp) + " C " + " Utility RH = " + String(ES135xx_Hum) + "%");
//DS18B20
// call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Temp_tank2 = sensors.getTempCByIndex(1);// Why "byIndex"? You can have more than one DS18B20 on the same bus. 0 refers to the first IC on the wire
Temp_tank1 = sensors.getTempCByIndex(0);// Why "byIndex"? You can have more than one DS18B20 on the same bus. 1 refers to the second IC on the wire
//Calibration
/*float rawTempC = sensors.getTempCByIndex(0);
float calibratedTempC = rawTempC + CALIBRATION_OFFSET;
if(rawTempC == -127.00)
{
Serial.println("Error: Could not read temperature data");
}
else
{
Serial.print("Temperature: ");
Serial.println(calibratedTempC);
}*/
Serial.println("Temperature 1: " + String(Temp_tank1) + " C " + "| Temperature 2: " + String(Temp_tank2) + " C");
//Flow Sensor
// Disable the interrupts
detachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_1));
detachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_2));
// Calculate the flow rate in L/min and apply the calibration factor
float flowRate1 = (flow1 / 7.5) * CALIBRATION_FACTOR_1;
float flowRate2 = (flow2 / 7.5) * CALIBRATION_FACTOR_2;
// Print the calibrated flow rates
Serial.print("Calibrated flow rate sensor 1: ");
Serial.print(flowRate1);
Serial.println(" L/min");
Serial.print("Calibrated flow rate sensor 2: ");
Serial.print(flowRate2);
Serial.println(" L/min");
// Reset the pulse counters
flow1 = 0;
flow2 = 0;
// Reattach the interrupts
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_1), flow1ISR, RISING);
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_2), flow2ISR, RISING);
//Cloud Variables
storage_Humidity = storage_hum;
storage_Temperature = storage_temp;
utility_Humidity = utility_hum;
utility_Temperature = utility_temp;
tank1_Temp = Temp_tank1;
tank2_Temp = Temp_tank2;
flowSensor_Dsupply = flowRate1;
flowSensor_Dreturn = flowRate2
}
void FloatSensorReading () {
// if(!check && digitalRead(top1) == LOW && digitalRead(bottom1) == HIGH){
// lowTrig1 = true;
// tank1_active = true;
// }
if(!check && digitalRead(top1) == HIGH){
tank1_active = false;
lowTrig1 = false;
Serial.println("Warning, weak desiccant in tank 1. Checking tank 2");
serial_Monitor = "Warning!, weak desiccant in tank 1";
digitalWrite(dsp, LOW);
digitalWrite(drp, LOW);
delay(1000);
digitalWrite(t1d, LOW);
digitalWrite(dt1, LOW);
if(digitalRead(top2) == LOW){
tank2_active = true;
lowTrig2 = true;
printonce = true;
pumpdelaytime = currentTime;
drpdelaytime = currentTime;
serial_Monitor="Tank 2 active";
Serial.println("Assigning tank 2");
}
else if(digitalRead(top2) == HIGH){
tank2_active = false;
lowTrig2 = false;
Serial.println("Weak desiccant in tank 2...Turning off dehumidifier");
serial_Monitor = "Weak desiccant in tank 2. Turning off dehumidifier";
add_salt = true;
dehumidifier_status = false;
}
check = true;
Serial.println("Switching complete");
}
else if(check && digitalRead(top2) == HIGH && digitalRead(bottom2) == LOW){
lowTrig2 = false;
tank2_active = false;
serial_Monitor = "Warning, weak desiccant in tank 2.";
add_salt = true;
}
// If the value of lowTrig1 and lowTrig2 has changed, write it to the file
if (lowTrig1 != prevLowTrig1) {
FILE *f = fopen("/usb/lowTrig1.txt", "w");
if (f) {
fprintf(f, "%d", lowTrig1);
fclose(f);
Serial.println("Updating float 1 position...");
serial_Monitor = "Updating float 1 position";
}
prevLowTrig1 = lowTrig1;
}
if (lowTrig2 != prevLowTrig2) {
FILE *g = fopen("/usb/lowTrig2.txt", "w");
if (g) {
fprintf(g, "%d", lowTrig2);
fclose(g);
Serial.println("Updating float 2 position...");
serial_Monitor = "Updating float 2 position";
}
prevLowTrig2 = lowTrig2;
}
Serial.println("Float status 1: " + String(lowTrig1) + ", Float status 2: " + String(lowTrig2));
if(lowTrig1){
tank1_active = true;
Serial.print("Tank 1: ACTIVE | ");
}
else if (!lowTrig1){
tank1_active = false;
Serial.print("Tank 1: INACTIVE | ");
}
if(lowTrig2){
tank2_active = true;
Serial.println("Tank 2: ACTIVE");
}
else if(!lowTrig2){
tank2_active = false;
Serial.println("Tank 2: INACTIVE");
}
}
the board crashes (red light blinking) at the 'attachInterrupts' in the void setup function right after initializing the pins. I tried removing it from the setup, but it just crashes when it encounters it in the loop (void sensor reading function). The code works perfectly fine as a standalone. I can't figure out how to solve this.
any help would be appreciated. Thanks