Hello, I new to arduino and using openai to program arduino. By changing the code I'm learning more and more.
The problem I'm facing is that the value of boost doenst change. Only if I put the data pin direct 5V it changes. Can some one see if the code is correct and should give an proper value?
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
// Initialize serial communication
Serial.begin(9600);
// Clear the display buffer
display.clearDisplay();
}
void loop()
{
// Clear the previous contents of the display buffer
display.clearDisplay();
// Read analog input from pin A0 (MPX4250 sensor)
int sensorValue = analogRead(A0);
// Convert sensor reading to voltage (0V to 5V)
float voltage = sensorValue * (5.0 / 1023.0);
// Map the voltage to the boost level (0.0 to 1.5)
float boostLevel = map(voltage, 0.0, 5.0, 0, 150) / 100.0;
// Display the boost level as text
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(8, SCREEN_HEIGHT - 15); // Move cursor above the bar graph
display.print(boostLevel, 1); // Print the boost level with one decimal place
display.setTextSize(1); // Change text size to 1
display.setCursor(44, SCREEN_HEIGHT - 8); // Move cursor to add "bar"
display.print("Bar"); // Print "bar" after boost level
// Draw the boost bar graph
int boostBarWidth = linearInterpolation(0.0, 1.5, 0, 61, boostLevel); // Adjusted width for boost bar graph
int barHeight = 7; // Adjust the height of the bar graph
display.fillRect(0, SCREEN_HEIGHT - 25, boostBarWidth, barHeight, SSD1306_WHITE); // Adjusted coordinates
// Draw the TPS window outline
int windowWidth = 128;
int windowX = 0;
int windowY = SCREEN_HEIGHT - 32;
int windowHeight = 3;
display.drawRect(windowX, windowY, windowWidth, windowHeight, SSD1306_WHITE);
// Draw vertical lines at 25%, 50%, and 75% TPS
int lineY1 = windowY;
int lineY2 = windowY + windowHeight + 2;
int line25 = windowX + windowWidth / 4;
int line50 = windowX + windowWidth / 2;
int line75 = windowX + windowWidth * 3 / 4;
display.drawFastVLine(line25, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 25% TPS
display.drawFastVLine(line50, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 50% TPS
display.drawFastVLine(line75, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 75% TPS
// Draw a shorter vertical line at the middle of the screen
int lineMiddleX = SCREEN_WIDTH / 2;
int lineMiddleLength = 64;
int lineMiddleY1 = windowY + 6;
int lineMiddleY2 = lineMiddleY1 + lineMiddleLength;
display.drawFastVLine(lineMiddleX, lineMiddleY1, lineMiddleY2 - lineMiddleY1, SSD1306_WHITE); // Draw the line
// Display the AFR value as text (for testing purposes)
float afrValue = 14.7; // Example AFR value
display.setTextSize(2); // Normal 1:1 pixel scale for smaller text
display.setCursor(80, SCREEN_HEIGHT - 15); // Same height as boost level
display.print(afrValue, 1); // Print the AFR value with one decimal place
// Draw the text "AFR" below the AFR value (for testing purposes)
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(95, SCREEN_HEIGHT - 26); // Move cursor below the AFR value
display.print("AFR"); // Print "AFR"
// Update the display
display.display();
// Send voltage over serial communication
Serial.print("Voltage: ");
Serial.println(voltage, 2); // Print voltage with 2 decimal places
// Optional delay
delay(100);
}
// Function for linear interpolation
int linearInterpolation(float x0, float x1, int y0, int y1, float x)
{
return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
}
