So, I have a flowmeter connected to DIO Pin 2 on an arduino uno, a pair of sparkfun 7 segment displays over I2C on Analog pins 4 and 5, a relay controlling the power to a fluid pump being switched on/off from DIO pin 10, and a push button wired to the reset pins on the board.
The code seems to be like 90% there. It will switch on the pump and it will display the flow and volume on the 7 segment displays, it will reset with the push of the reset button, but for some reason, I cannot figure out how to get it to stop the pump and not get reset automatically.
I’ve tried exit(0);, I’ve tried endless loops, but they have seemed to be intermittent in success. I could run the code once, it will stop but the next time it will get reset.
Any guidance as to how I can adjust my code to prevent the self-reset would be greatly appreciated.
#include <Wire.h>
#define gallon 5145
#define volumeDisplay 0x71
#define flowDisplay 0x72
unsigned long pulseTime[2];
int flowPin = 2;
volatile byte state = LOW;
volatile int pulseCounter = 0;
int galCount = 0;
int galFinal = 3;
int controlPin = 10;
byte brightness = 100;
char tempString[10];
const uint16_t t1_load = 0;
const uint16_t t1_comp = 20000;
volatile uint8_t timer = 0;
float lastvolume =0;
void setup() {
pulseTime[1] = 0;
pulseTime[0] = 0;
Wire.begin();
clearDisplayI2C(volumeDisplay);
clearDisplayI2C(flowDisplay);
setBrightnessI2C(volumeDisplay, 250);
setBrightnessI2C(flowDisplay, 250);
pinMode(flowPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(flowPin), pulse, RISING);
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, HIGH);
// put your setup code here, to run once:
//Reset Timer1 Control Reg A
TCCR1A = 0;
//Set CTC mode
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << WGM12);//Set to prescaler of 8
TCCR1B &= ~(1 << CS12);
TCCR1B |= (1 << CS11);
TCCR1B &= ~(1 << CS10);
//Reset Timer1 and set compare value
TCNT1 = t1_load;
OCR1A = t1_comp;
//Enable Timer1 compare interrupt
TIMSK1 = (1 << OCIE1A);
//Enable global interrupts
sei();
clearDisplayI2C(flowDisplay);
s7sSendString(flowDisplay, "-HI-");
setBrightnessI2C(flowDisplay, 250);
delay(500);
clearDisplayI2C(flowDisplay);
}
void loop() {
// put your main code here, to run repeatedly:
while (galCount < galFinal == true) {
if (timer>=10) {
float volume = (((float)pulseCounter) / ((float)gallon)) + (galCount);
float flowRate = (volume-lastvolume)*600;
s7sSendFloat(flowDisplay, flowRate);
lastvolume=volume;
timer = 0;
}
if (pulseCounter < gallon == true) {
float volume = (((float)pulseCounter) / ((float)gallon)) + (galCount);
s7sSendFloat(volumeDisplay, volume);
//float flowRate = (((float)pulseCounter)/ ((float)millis() - (float)pulseTime[0]));
//s7sSendFloat(flowDisplay, flowRate);
}
else {
galCount++;
pulseCounter = 0;
delay(5);
}
}
while (galCount >= galFinal == true) {
//delay(2500);
digitalWrite(controlPin, LOW);
//detachInterrupt(digitalPinToInterrupt(flowPin));
delay(2500);
//while (1) {
finished();
//}
}
}
void pulse() {
state = !state;
pulseTime[1] = pulseTime[0];
pulseTime[0] = millis();
pulseCounter++;
}
ISR(TIMER1_COMPA_vect) {
timer++;
}
void s7sSendString(byte s7sAddress, String toSend)
{
Wire.beginTransmission(s7sAddress);
for (int i = 0; i < 4; i++)
{
Wire.write(toSend[i]);
}
Wire.endTransmission();
}
void s7sSendFloat(byte s7sAddress, float value) {
char temp_str[10];
dtostrf(value, 4, 2, temp_str);
char temp_str2[10];
int j = 0;
int k = 0;
for (int i = 0; i < 10; i++) {
if (temp_str[i] == '.') {
k = i - 1;
i++;
}
temp_str2[j] = temp_str[i];
j++;
}
setDecimals(s7sAddress, (1 << k));
sprintf(tempString, "%s", temp_str2);
s7sSendString(s7sAddress, tempString);
}
void clearDisplayI2C(byte s7sAddress)
{
Wire.beginTransmission(s7sAddress);
Wire.write(0x76); // Clear display command
Wire.endTransmission();
}
void setBrightnessI2C(byte s7sAddress, byte value)
{
Wire.beginTransmission(s7sAddress);
Wire.write(0x7A); // Set brightness command byte
Wire.write(value); // brightness data byte
Wire.endTransmission();
}
void setDecimals(byte s7sAddress, byte decimals)
{
Wire.beginTransmission(s7sAddress);
Wire.write(0x77);
Wire.write(decimals);
Wire.endTransmission();
}
void finished() {
while(1){
digitalWrite(controlPin, LOW);
Wire.beginTransmission(volumeDisplay);
Wire.write(0x76);
Wire.write('F');
Wire.write('i');
Wire.write('L');
Wire.write('L');
Wire.endTransmission();
Wire.beginTransmission(flowDisplay);
Wire.write(0x76);
Wire.write('D');
Wire.write('o');
Wire.write('n');
Wire.write('E');
Wire.endTransmission();
delay(500);
//exit(0);
}
}