Advertisement
pleasedontcode

Fuel Monitor rev_01

Aug 17th, 2024
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Fuel Monitor
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-17 22:01:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The application shall read fuel levels via a */
  21.     /* potentiometer, displaying results on an I2C */
  22.     /* LiquidCrystal display, and trigger an LED and */
  23.     /* buzzer when fuel is critically low, enhancing user */
  24.     /* interaction and safety. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* a menu for lcd to adjust buzzer and led settings */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  32. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38. void displayFuelLevel(float fuelLevel);
  39. void checkFuelLevel(float fuelLevel);
  40. void menu(); // Function to handle the menu for adjusting settings
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t fuelLevel_Potentiometer_Vout_PIN_A0       = A0;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t led_LED_PIN_D3        = 3;
  47. const uint8_t buzzer_ActiveBuzzer_output_PIN_D4     = 4;
  48.  
  49. /***** DEFINITION OF I2C PINS *****/
  50. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS      = 0x27; // Updated I2C address to common value for LCD
  51.  
  52. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  53. bool    led_LED_PIN_D3_rawData      = 0;
  54. bool    buzzer_ActiveBuzzer_output_PIN_D4_rawData       = 0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. // Create an instance of the EasyButton class for button handling
  58. #define BUTTON_PIN 2 // Define the button pin
  59. EasyButton button(BUTTON_PIN); // Initialize EasyButton with the button pin
  60.  
  61. // Create an instance of the LiquidCrystal_I2C class for LCD handling
  62. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // Initialize LCD with I2C address and dimensions
  63.  
  64. void setup(void)
  65. {
  66.     // put your setup code here, to run once:
  67.  
  68.     pinMode(fuelLevel_Potentiometer_Vout_PIN_A0,    INPUT);
  69.     pinMode(led_LED_PIN_D3,  OUTPUT);
  70.     pinMode(buzzer_ActiveBuzzer_output_PIN_D4,   OUTPUT);
  71.  
  72.     // Initialize the LCD
  73.     lcd.init();                      
  74.     lcd.backlight(); // Turn on the backlight for the LCD
  75.  
  76.     // Initialize the button
  77.     button.begin(); // Call begin to initialize the button
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.  
  84.     updateOutputs(); // Refresh output data
  85.  
  86.     // Continuously read the status of the button
  87.     button.read(); // Read the button state
  88.  
  89.     // Read fuel level from the potentiometer
  90.     float fuelLevel = analogRead(fuelLevel_Potentiometer_Vout_PIN_A0) * (100.0 / 1023.0); // Convert to percentage
  91.  
  92.     // Display the fuel level on the LCD
  93.     displayFuelLevel(fuelLevel);
  94.  
  95.     // Check the fuel level and update LED and buzzer
  96.     checkFuelLevel(fuelLevel);
  97.  
  98.     // Check if the button is pressed to open the menu
  99.     if (button.read()) {
  100.         menu(); // Open the menu for adjusting settings
  101.     }
  102. }
  103.  
  104. void updateOutputs()
  105. {
  106.     digitalWrite(led_LED_PIN_D3, led_LED_PIN_D3_rawData);
  107.     digitalWrite(buzzer_ActiveBuzzer_output_PIN_D4, buzzer_ActiveBuzzer_output_PIN_D4_rawData);
  108. }
  109.  
  110. void displayFuelLevel(float fuelLevel) {
  111.     lcd.setCursor(0, 0);
  112.     lcd.print("Fuel Level: ");
  113.     lcd.print(fuelLevel);
  114.     lcd.print(" %");
  115. }
  116.  
  117. void checkFuelLevel(float fuelLevel) {
  118.     if (fuelLevel < 15.0) { // Critical level threshold
  119.         led_LED_PIN_D3_rawData = HIGH; // Turn on LED
  120.         buzzer_ActiveBuzzer_output_PIN_D4_rawData = HIGH; // Turn on Buzzer
  121.     } else {
  122.         led_LED_PIN_D3_rawData = LOW; // Turn off LED
  123.         buzzer_ActiveBuzzer_output_PIN_D4_rawData = LOW; // Turn off Buzzer
  124.     }
  125. }
  126.  
  127. void menu() {
  128.     // Simple menu implementation to adjust buzzer and LED settings
  129.     lcd.clear();
  130.     lcd.setCursor(0, 0);
  131.     lcd.print("Menu:");
  132.     lcd.setCursor(0, 1);
  133.     lcd.print("1: Buzzer ON/OFF");
  134.     lcd.setCursor(0, 2);
  135.     lcd.print("2: LED ON/OFF");
  136.     lcd.setCursor(0, 3);
  137.     lcd.print("Press button to select");
  138.  
  139.     // Wait for button press to select an option
  140.     while (!button.read()) {
  141.         // Do nothing, just wait
  142.     }
  143.  
  144.     // Implement the logic to toggle buzzer and LED settings
  145.     // This is a placeholder for the actual implementation
  146.     // You can add more detailed logic here based on user input
  147. }
  148.  
  149. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement