Ok, so I've managed to get to this point with my code, and it seems to be working properly except the i2c 7 segment display showing the digits. The one's place seems to count properly, and the tenth's place seems to as well. However, for some reason, my hundredth's place seems to count in hex and increases in counting rate once the tenth's place is incremented.
I'm not sure what operation is going screwy, but any help would be greatly appreciated.
/*
* Code developed by T. Russell
* 2019-12-06
*
* 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 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;
byte flowDigits[5];
byte volumeDigits[5];
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);
float flowRate = (((float)pulseCounter)/ ((float)millis() - (float)pushTime[0]));
Serial.print("Flow Rate: ");Serial.print(flowRate);Serial.println(" gpm");
float volume = (((float)pulseCounter) / ((float)gallon)) + (galCount);
Serial.print("Volume: ");Serial.print(volume);Serial.println(" Gallons");
//i2c
int volDigit1 = volume;
int volDigit2 = (volume * 10) - (volDigit1 * 10);
int volDigit3 = (((volume * 10) - (10.0)) *10) - ((volDigit2 * 10.0));
//Serial.print("Volume digit 1: ");Serial.println(galCount);
//Serial.print("Volume digit 2: ");Serial.println(volDigit2);
//Serial.print("Volume digit 3: ");Serial.println(volDigit3);
Wire.beginTransmission(0x71); //Address for specific 7 segment display
Wire.write(0x76);
Wire.write(0x77);
Wire.write(0b00000001); //Decimal control
Wire.write((int)volume);
//Wire.write(0x7C);
Wire.write((int)volDigit2,HEX);
// Wire.write(0x7C);
// Wire.write((byte)volDigit2);
// Wire.write(0x7D);
Wire.write((int)volDigit3,HEX);
delay(5);
Wire.endTransmission();
//
// Wire.beginTransmission(0x72);
// Wire.write(volume);
// delay(500);
// 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;
pushTime[1] = pushTime[0];
pushTime[0] = millis();
pulseCounter++;
}