// Pin definitions
const int STCPPin = 3; // Pin connected to ST_CP of 74HC595
const int SHCPPin = 4; // Pin connected to SH_CP of 74HC595 (can be SCK of SPI)
const int DSPin = 2; // Pin connected to DS of 74HC595 (can be MOSI of SPI)
const int resetButtonPin = 2; // Pin for the reset button
const int increaseButtonPin = 3; // Pin for the increase button
const int decreaseButtonPin = 4; // Pin for the decrease button
const int pulsePin = 5; // Interrupt pin for fuel usage pulses
volatile float fuelAmount = 70.0; // Start with 70 liters
const byte digits[10] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup()
{
pinMode(STCPPin, OUTPUT);
pinMode(SHCPPin, OUTPUT);
pinMode(DSPin, OUTPUT);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(increaseButtonPin, INPUT_PULLUP);
pinMode(decreaseButtonPin, INPUT_PULLUP);
SPI.begin(); // If using SPI for communication
attachInterrupt(digitalPinToInterrupt(pulsePin), pulseISR, FALLING);
}
void loop()
{
if (!digitalRead(resetButtonPin))
{
fuelAmount = 70.0;
}
if (!digitalRead(increaseButtonPin))
{
// Increment to the nearest number divisible by 5, with an upper limit of 70
float increment = 5.0 - fmod(fuelAmount, 5.0);
if (increment == 5.0)
{
increment = 0; // If already divisible by 5, don't add
}
fuelAmount += increment;
if (fuelAmount > 70.0)
{
fuelAmount = 0;
}
}
if (!digitalRead(decreaseButtonPin))
{
// Decrement to the nearest number divisible by 5
fuelAmount -= 1.0;
if (fuelAmount <= 0.0)
{
fuelAmount = 70.0;
}
}
displayFuelAmount(); // Update the display with the current fuel amount
}
void displayFuelAmount()
{
int fuelInt = (int)(fuelAmount * 100); // Convert fuel amount to integer for display
for (int i = 0; i < 4; ++i)
{
int digit = (fuelInt / (int)pow(10, i)) % 10; // Calculate each digit
digitalWrite(STCPPin, LOW);
shiftOut(DSPin, SHCPPin, MSBFIRST, digits[digit]); // Send digit pattern to 74HC595
digitalWrite(STCPPin, HIGH);
delay(5); // Small delay to ensure digit is visible
}
}
void pulseISR()
{
fuelAmount -= 1.0 / 16.0 / 100.0; // Each pulse represents 1/16 of 1/100th of a liter
if (fuelAmount < 0.0)
{
fuelAmount = 0.0; // Prevent going below 0
}
}