/*
SSD1306 Fuel Gauge
This example code is in the public domain.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int level = 0;
int x = 0;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// these constants won't change:
const int ambient = A0; // the pin that the potentiometer is attached to
const int fuelGross = A1; // the pin that the potentiometer is attached to
const int barCount = 4; // the number of barss in the bar graph
int blocks[] = {
1, 2, 3, 4, 5,
}; // an array of blocks
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(1000); // Pause for 1 seconds
// Clear the buffer
display.clearDisplay();
// Draw petrol pump
display.fillRect(1, 6, 30, 16, WHITE);
display.fillRect(20, 9, 8, 10, BLACK);
display.drawLine(0, 4, 0, 24, WHITE);
display.drawLine(22, 22, 22, 24, WHITE);
display.drawLine(22, 24, 8, 24, WHITE);
display.drawLine(8, 24, 8, 26, WHITE);
display.drawLine(8, 26, 8, 28, WHITE);
display.drawLine(8, 28, 22, 28, WHITE);
display.drawLine(22, 28, 28, 26, WHITE);
display.display();
}
void drawBlock0() {
display.fillRect(33, 0, 15, 64, WHITE);
// display.drawLine(level, 0, level, 64, WHITE);
display.display();
delay(2000);
display.fillRect(33, 0, 15, 64, BLACK);
display.display();
}
void drawBlock1() {
display.fillRect(33, 0, 15, 64, WHITE);
// display.drawLine(level, 0, level, 64, WHITE);
display.display();
}
void drawBlock2() {
display.fillRect(53, 0, 15, 64, WHITE);
display.display();
}
void drawBlock3() {
display.fillRect(73, 0, 15, 64, WHITE);
display.display();
}
void drawBlock4() {
display.fillRect(93, 0, 15, 64, WHITE);
display.display();
}
void undrawBlock2() {
display.fillRect(53, 0, 15, 64, BLACK);
display.display();
}
void undrawBlock3() {
display.fillRect(73, 0, 15, 64, BLACK);
display.display();
}
void undrawBlock4() {
display.fillRect(93, 0, 15, 64, BLACK);
display.display();
}
void loop() {
// read the ambient pressure:
int sensorReading = analogRead(ambient);
// read the Fuel pressure:
int sensorReading2 = analogRead(fuelGross);
int fuelNet = sensorReading2 - sensorReading;
// map the result to a range from 0 to the number of blocks:
int barLevel = map(fuelNet, 0, 1023, 0, 5);
// loop over the array:
for (int blocks = 0; blocks < 5; blocks++) {
Serial.println(barLevel);
switch (barLevel) {
case 0:
drawBlock0();
undrawBlock2();
undrawBlock3();
undrawBlock4();
break;
case 1: //
drawBlock1();
undrawBlock2();
undrawBlock3();
undrawBlock4();
break;
case 2: // y
drawBlock1();
drawBlock2();
undrawBlock3();
undrawBlock4();
break;
case 3: //
drawBlock1();
drawBlock2();
drawBlock3();
undrawBlock4();
break;
case 4:
drawBlock1();
drawBlock2();
drawBlock3();
drawBlock4();
break;
}
}
delay(1000); // Pause for 1 seconds
}
Loading
ssd1306
ssd1306