Thus far, this is the code that most operates as I had hoped, however, at some point, the second seven segment display address gets configured mid-run, and stops working.
I'm not sure what/how to correct this. Is there an issue using an interrupt with I2C communication? Or is it my method for displaying the values/interfacing with the display? I guess I'm kind of at a loss as to where to begin.
I'm using the Sparkfun I2C 4 digit seven segment displays, in addition to a push button attached to Di/o 2.
/*
*
* This code is designed to calculate the flow and volume of fluid through an Omega FTB-432 flow meter
*
* This code utilizes interrupts to determine the pulses from the flow meter, then calculates the volume and flow
* Code to be added will include the means to display flow and volume on a pair of 4 digit seven segment
* displays utilizing an i2c interface.
*
*/
#include <Wire.h>
#define gallon 51//45
unsigned long pulseTime[2];
int buttonPin = 2;
volatile byte state = LOW;
//int timer = 0;
int pulseCounter = 0;
int pulseCount = 0;
//int count[2];
int galCount = 0;
int resetPin = 3;
int systemState = 2;
int controlPin = 10;
byte flowDigits[3];
byte volumeDigits[3];
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.write(0b00000001);
Wire.endTransmission();
Wire.beginTransmission(0x72);
Wire.write(0x76);
Wire.endTransmission();
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin),pulse,RISING);
pinMode(controlPin, OUTPUT);
}
void loop() {
while(galCount < 3 == true){
if(pulseCounter < gallon == true){
Serial.print("Number of pulses: ");Serial.println(pulseCounter);
unsigned char flowRate = (((float)pulseCounter)/ ((float)millis() - (float)pulseTime[0]));
Serial.print("Flow Rate: ");Serial.print(flowRate);Serial.println(" gpm");
unsigned char volume = (((float)pulseCounter) / ((float)gallon)) + (galCount);
Serial.print("Volume: ");Serial.print(volume);Serial.println(" Gallons");
//i2c
float flo0 = ((float)pulseCount/(float)gallon);
int val0 = (int) (10 * (int)flo0);
float flo1 = (((float)flo0*10) - (float)val0);
int val1a = (int) ((100*(int)flo0));
int val1b = (int) (10 * (int)flo1);
int val1 = (int)val1a + (int)val1b;
float flo2 = ((((float)pulseCount/((float)millis()-(float)pulseTime[0])) * (100)) - ((int)val1));
float vol0 = ((float)pulseCount/(float)gallon);
int valf0 = (int) (10 * (int)vol0);
float vol1 = (((float)vol0*10) - (float)valf0);
int valf1a = (int) ((100*(int)vol0));
int valf1b = (int) (10 * (int)vol1);
int valf1 = (int)valf1a + (int)valf1b;
float vol2 = ((((float)pulseCount/(float)gallon) * (100)) - ((int)val1));
//byte volumeDigits[3];//values[3];
volumeDigits[0] = (int)vol0;
volumeDigits[1] = (int)vol1;
volumeDigits[2] = (int)vol2;
flowDigits[0] = (int) flo0;
flowDigits[1] = (int) flo1;
flowDigits[2] = (int) flo2;
Wire.beginTransmission(0x71);
Wire.write(0x76);
Wire.write(0x77);
Wire.write(0b00000001);
Wire.write(volumeDigits[0]);
Wire.write(volumeDigits[1]);
Wire.write(volumeDigits[2]);
Wire.endTransmission();
Wire.beginTransmission(0x72);
Wire.write(0x76);
Wire.write(0x77);
Wire.write(0b00000001);
Wire.write(flowDigits[0]);
Wire.write(flowDigits[1]);
Wire.write(flowDigits[2]);
Wire.endTransmission();
}else{
Serial.println("Gallon incremented");
galCount++;
Serial.print("Number of Gallons: ");Serial.println(galCount);
pulseCounter = 0;
delay(100);
}
}
if(galCount >= 3 == true){
//digitalWrite(outputLED, HIGH);
systemState = 1;
digitalWrite(controlPin, LOW);
Serial.println("Fluid at 3 gallon capacity");
delay(500);
do{
//Serial.println("Fill complete");
Wire.beginTransmission(0x71);
Wire.write(0x76);
Wire.write('F');
Wire.write('i');
Wire.write('L');
Wire.write('L');
delay(10);
Wire.endTransmission();
Wire.beginTransmission(0x72);
Wire.write(0x76);
Wire.write('D');
Wire.write('o');
Wire.write('n');
Wire.write('E');
delay(10);
Wire.endTransmission();
//systemState = 0;
}while(systemState = 1);
}
}
void pulse(){
state = !state;
pulseTime[1] = pulseTime[0];
pulseTime[0] = millis();
pulseCounter++;
pulseCount++;
}