Interrupt to reset values or Wire in Reset button

So, I'm working on a project to measure flow rates, and compute these values with the respective volume after a period of time.

I seem to be having trouble getting my reset button to work properly. When I check it in Serial monitor, the code seems to freeze. It appears to count everything just fine until the reset is hit.

I know my code is kinda messy and has a lot of comments but that has been several iterations trying to work things out, and I am by no means a solid programmer.

After I figure out how to get my code to start back over.

I just realized I could wire in an external reset button to the board, however, I think it might be preferable to be able to just reset the values rather than the board.

My setup is an uno with a protoboard, and pulse input on interrupt 2 and reset on interrupt 3.

Eventually will progress towards displaying values on 7 segment displays.

I know my code is kinda messy and has a lot of comments but that has been several iterations trying to work things out, and I am by no means a solid programmer.

If you had posted your code for us to see we might have been able to help

Hmmm, odd. I attached the file itself, but didn't realize it wasn't in the posting.

#include <Wire.h>
#define gallon 5//145
unsigned long pushTime[2];
int buttonPin = 2;
volatile byte state = LOW;
//int timer = 0;
int pulseCounter = 0;
//int count[2];
int galCount = 0;
int resetPin = 3;
int systemState = 2;
int controlPin = 10;

int userStart = 0;

void setup() {
// put your setup code here, to run once:
Wire.begin(); //Join i2c Bus
Wire.beginTransmission(0x71);
Wire.write(0x76);
Wire.endTransmission();
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin),blink,RISING);
pinMode(resetPin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(resetPin),resetValues,RISING);
pinMode(controlPin, OUTPUT);

//Code associated with timer interrupt
//pinMode(outputLED, OUTPUT);
// TCCR0A = (1<<WGM01); //Set the CTC mode
// OCR0A=0xF9; //Value for ORC0A for 1ms
//
// TIMSK0 |= (1<<OCIE0A); //Set the interrupt request
// sei(); //Enable interrupt
//
// TCCR0B |= (1<<CS01); //Set hte prescale 1/64 clock
// TCCR0B |= (1<<CS00);

//unsigned long deltaTime = pushTime[0] - pushTime[1];
}

void loop() {
while(galCount < 3 == true){
if(pulseCounter < gallon == true){
//if(timer>=100){
//condition=!condition;
//timer=0;
//float flowRate = ((float)pulseCounter / (float)gallon)/ ((float) deltaTime);
Serial.print("Number of pulses: ");Serial.println(pulseCounter);
//Serial.print("0: ");Serial.println(pushTime[0]);
//Serial.print("1: ");Serial.println(pushTime[1]);
//Serial.print("Flow Rate: ");Serial.println(flowRate);
//unsigned long deltaTime = pushTime[0] - pushTime[1];
//Serial.print("Delta Time: ");Serial.println(deltaTime);
//unsigned long deltaCount = count[0] - count[1];
//Serial.print("Delta count: ");Serial.println(deltaCount);
//Serial.print("currentTime: ");Serial.println((unsigned long)millis());
//Serial.print("currentTime - pushTime[0]: ");Serial.println(millis() - pushTime[0]);
float flowRate = (((float)pulseCounter)/ ((float)millis() - (float)pushTime[0]));
Serial.print("Flow Rate: ");Serial.println(flowRate);
float volume = ((float)pulseCounter) / ((float)gallon);
Serial.print("Volume: ");Serial.println(volume);
//}
//digitalWrite(outputLED, condition);
}else{
Serial.println("Gallon incremented");
galCount++;
Serial.print("Number of Gallons: ");Serial.println(galCount);
pulseCounter = 0;
//lastPulseTime = currentPulseTime;
//prevPulse = currentPulse;
delay(100);
}
}
if(galCount >= 3 == true){
//digitalWrite(outputLED, HIGH);
systemState = 1;
digitalWrite(controlPin, LOW);
Serial.println("Fluid at 3 gallon capacity");
delay(1000);
do{
//Serial.println("Fill complete");
Wire.beginTransmission(0x71);
Wire.write(0x76);
Wire.write('d');
Wire.write('o');
Wire.write('n');
Wire.write('E');

Wire.endTransmission();
delay(1000);
systemState = 0;
}while(systemState = 1);
}

// put your main code here, to run repeatedly:
//Serial.print("Push Time: ");//Serial.println(pushTime);
// Serial.print("Push Time [0]: ");Serial.println(pushTime[0]);
// Serial.print("Push Time [1]: ");Serial.println(pushTime[1]);
// unsigned long deltaTime = pushTime[0] - pushTime[1];
// Serial.print("Delta Time: ");Serial.println(deltaTime);
}

void blink(){
state = !state;
pushTime[1] = pushTime[0];
pushTime[0] = millis();
//count[1] = count[0];
//count[0] = pulseCounter;
pulseCounter++;
}
//ISR(TIMER0_COMPA_vect){
// timer++;
//}
void resetValues(){
pulseCounter = 0;
galCount = 0;
systemState = 0;
Wire.beginTransmission(0x71);
Wire.write(0x76);
Wire.endTransmission();
for(int i = 0; i < sizeof(pushTime) / sizeof(pushTime[0]); i++){
pushTime = 0;

  • }*
    }

You can not use i2c within an ISR. Interrups are disabled inside the isr and i2c depends on them being enabled.

You do no need an interrupt to read a button press. Write non blocking code and poll state of the button pin.

Yeah, I think I figured that part out because my code wasn't operating as I intended once I included the ISR to the i2c configuration. Fortunately I guess I can work around the ISR because it was only meant to slow down the update to the data, and not entirely needed.