pleasedontcode

**Centered Voltmeter** rev_02

Feb 28th, 2026
35
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: **Centered Voltmeter**
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2026-02-28 22:14:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implemente o texto do valor em "volts" centrado de */
  21.     /* acordo com a dimensão do display com o texto */
  22.     /* "VOLTS" acima. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /********* Pleasedontcode.com **********\n\n\tPleasedontcode thanks you for automatic code generation! Enjoy your code!\n\n\t- Terms and Conditions:\n\tYou have a non-exclusive, revocable, worldwide, royalty-free license\n\tfor personal and commercial use. Attribution is optional; modifications\n\tare allowed, but you're responsible for code maintenance. We're not\n\tliable for any loss or damage. For full terms,\n\tplease visit pleasedontcode.com/termsandconditions.\n\n\t- Project: Project_3968\n\t- SOURCE CODE NOT YET FINALIZED. STILL UNDER GENERATION PHASE.\n\t- Source Code created on: 2026-02-28 22:14:05\n\t- Source Code generated by: Sebastião\n\n********* Pleasedontcode.com **********/\n\n// Include necessary libraries for I2C communication and OLED display\n#include <Wire.h>\n#include <Adafruit_GFX.h>\n#include <Adafruit_SSD1306.h>\n\n/****** SYSTEM REQUIREMENTS *****/\n/****** SYSTEM REQUIREMENT 1 *****/\n\t/* Implemente o texto do valor em \"volts\" centrado de */\n\t/* acordo com a dimensão do display com o texto */\n\t/* \"VOLTS\" acima. */\n/****** END SYSTEM REQUIREMENTS *****/\n\n\n\n/****** FUNCTION PROTOTYPES *****/\nvoid setup(void);\nvoid loop(void);\nvoid displayVoltageValue(float voltage);\nint16_t calculateCenteredX(const char* text, uint8_t textSize);\nint16_t calculateCenteredY(uint8_t textSize, uint8_t linePosition);\n\n/***** DEFINITION OF I2C PINS *****/\nconst uint8_t displayOLED_SSD1306OledDisplay_I2C_PIN_SDA_A4\t\t\t= A4;\nconst uint8_t displayOLED_SSD1306OledDisplay_I2C_PIN_SCL_A5\t\t\t= A5;\nconst uint8_t displayOLED_SSD1306OledDisplay_I2C_SLAVE_ADDRESS\t\t= 0x3C;\n\n/***** DEFINITION OF DISPLAY PARAMETERS *****/\n#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 32\n#define OLED_RESET -1\n\n/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/\n// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);\n\nvoid setup(void)\n{\n\t// Initialize Serial for debugging (optional)\n\tSerial.begin(9600);\n\t\n\t// Initialize the display with I2C address\n\tif(!display.begin(SSD1306_SWITCHCAPVCC, displayOLED_SSD1306OledDisplay_I2C_SLAVE_ADDRESS)) {\n\t\tSerial.println(F(\"SSD1306 allocation failed\"));\n\t\t// Loop forever if initialization fails\n\t\twhile(1);\n\t}\n\t\n\t// Clear the display buffer\n\tdisplay.clearDisplay();\n\t\n\t// Display initial content\n\tdisplay.display();\n\tdelay(500);\n}\n\nvoid loop(void)\n{\n\t// Example: Display a voltage value of 5.0 volts\n\t// This demonstrates the voltage display centered according to display dimensions\n\tdisplayVoltageValue(5.0);\n\t\n\tdelay(1000);\n\t\n\t// Example: Display a different voltage value\n\tdisplayVoltageValue(3.3);\n\t\n\tdelay(1000);\n\t\n\t// Example: Display another voltage value\n\tdisplayVoltageValue(4.75);\n\t\n\tdelay(1000);\n}\n\n// Function to display the voltage value centered on the display\n// According to System Requirement 1: Display the voltage value text centered\n// with the text \"VOLTS\" above it, aligned with display dimensions\nvoid displayVoltageValue(float voltage)\n{\n\t// Clear the display buffer\n\tdisplay.clearDisplay();\n\t\n\t// Set text properties for \"VOLTS\" label\n\tdisplay.setTextSize(1);\n\tdisplay.setTextColor(SSD1306_WHITE);\n\t\n\t// Draw \"VOLTS\" text centered horizontally at the top\n\tconst char* labelText = \"VOLTS\";\n\tint16_t labelX = calculateCenteredX(labelText, 1);\n\tdisplay.setCursor(labelX, 2);\n\tdisplay.println(labelText);\n\t\n\t// Prepare the voltage value string\n\tchar voltageStr[10];\n\tdtostrf(voltage, 4, 2, voltageStr);\n\t\n\t// Set text properties for voltage value display\n\t// Use larger text size for better visibility\n\tdisplay.setTextSize(2);\n\tdisplay.setTextColor(SSD1306_WHITE);\n\t\n\t// Calculate centered position for voltage value\n\t// The voltage value is displayed below \"VOLTS\" with larger font\n\tint16_t voltageX = calculateCenteredX(voltageStr, 2);\n\tint16_t voltageY = 18;\n\t\n\t// Draw the voltage value centered\n\tdisplay.setCursor(voltageX, voltageY);\n\tdisplay.println(voltageStr);\n\t\n\t// Update the display to show the new content\n\tdisplay.display();\n}\n\n// Function to calculate the centered X position for a given text\n// This ensures the text is horizontally centered according to display width\nint16_t calculateCenteredX(const char* text, uint8_t textSize)\n{\n\t// Get the length of the text\n\tint16_t textLength = strlen(text);\n\t\n\t// Each character is 6 pixels wide at textSize 1\n\t// Character width scales with text size: 6 * textSize\n\tint16_t charWidth = 6 * textSize;\n\t\n\t// Calculate total text width\n\tint16_t totalTextWidth = textLength * charWidth;\n\t\n\t// Calculate centered X position\n\t// Center position = (Display Width - Text Width) / 2\n\tint16_t centeredX = (SCREEN_WIDTH - totalTextWidth) / 2;\n\t\n\t// Ensure X position doesn't go below 0\n\tif(centeredX < 0) {\n\t\tcenteredX = 0;\n\t}\n\t\n\treturn centeredX;\n}\n\n// Function to calculate the centered Y position for a given text line\n// This ensures the text is vertically centered according to display height and line position\nint16_t calculateCenteredY(uint8_t textSize, uint8_t linePosition)\n{\n\t// Each character line is 8 pixels tall at textSize 1\n\t// Character height scales with text size: 8 * textSize\n\tint16_t charHeight = 8 * textSize;\n\t\n\t// Calculate Y position based on line position\n\t// linePosition 0 = top, 1 = middle, etc.\n\tint16_t yPosition = linePosition * charHeight;\n\t\n\treturn yPosition;\n}
  29.  
  30. /* END CODE */
  31.  
Advertisement
Add Comment
Please, Sign In to add comment