#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the I2C LCD with I2C address (0x27 or 0x3F, depende sa iyong LCD)
LiquidCrystal_I2C lcd(0x27, 20, 4); // 20x4 LCD with I2C address
const int len = 18;
// Example sine wave-like data points
float data[len];
// Custom character bar definitions (0-7 pixel heights)
byte bar0[8] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000 };
byte bar1[8] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000, B11111 };
byte bar2[8] = { B00000, B00000, B00000, B00000, B00000, B00000, B11111, B11111 };
byte bar3[8] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 };
byte bar4[8] = { B00000, B00000, B00000, B00000, B11111, B11111, B11111, B11111 };
byte bar5[8] = { B00000, B00000, B00000, B11111, B11111, B11111, B11111, B11111 };
byte bar6[8] = { B00000, B00000, B11111, B11111, B11111, B11111, B11111, B11111 };
byte bar7[8] = { B00000, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };
// Function to load custom characters into CGRAM
void createCustomChars() {
lcd.createChar(0, bar0);
lcd.createChar(1, bar1);
lcd.createChar(2, bar2);
lcd.createChar(3, bar3);
lcd.createChar(4, bar4);
lcd.createChar(5, bar5);
lcd.createChar(6, bar6);
lcd.createChar(7, bar7);
}
void insertNewValue(float arr[], float newValue, int insertToIndex, int arrSize) {
// Shift elements to the right to make space for the new value
for (int i = arrSize - 1; i > insertToIndex; i--) {
arr[i] = arr[i - 1];
}
// Insert the new value at the specified index
arr[insertToIndex] = newValue;
}
void deleteValue(float arr[], int deleteIndex, int arrSize) {
// Check if the index is within the array bounds
if (deleteIndex >= 0 && deleteIndex < arrSize) {
// Shift elements to the left starting from the deleteIndex
for (int i = deleteIndex; i < arrSize - 1; i++) {
arr[i] = arr[i + 1];
}
// Optionally, you can set the last element to a default value (e.g., 0)
arr[arrSize - 1] = 0;
} else {
Serial.println("Index out of bounds.");
}
}
// Function to display graph on 20x4 LCD
void barGraph(int x, int y, int h, int w, float yMinVal, float yMaxVal, float *dataVal) {
for (int i = 0; i <= w; i++) {
// Normalize data to graph height
float normalizedValue = (dataVal[i] - yMinVal) / (yMaxVal - yMinVal);
int scaledValue = int(normalizedValue * (h * 8)); // Scale to 0-7 pixels per row
// Display bar height across multiple rows
for (int row = 0; row < h; row++) {
int pixelHeight = (scaledValue - (h - row - 1) * 8);
if (pixelHeight < 0) pixelHeight = 0;
if (pixelHeight > 7) pixelHeight = 7;
lcd.setCursor(x + i, y + row); // Set cursor to (x, y) position
lcd.write(pixelHeight); // Display custom character for bar height
}
}
}
void setup() {
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight (optional)
createCustomChars(); // Load custom characters into CGRAM
// Draw graph at (5,1) with height 3, width 8, range from -10 to 10
barGraph(2, 1, 2, len, 0, 10, data);
delay(2000);
barGraph(2, 1, 2, len, 0, 10, data);
}
void loop() {
// Example usage:
int deleteIndex = 0; // Delete the value at index 5
// Call the function to delete the value
deleteValue(data, deleteIndex, len);
// Example usage:
float sensor = random(1, 10);
int newIndex = len-1; // Specify index at which the new value should be inserted
// Call the function to insert the value
insertNewValue(data, sensor, newIndex, len);
// Print the updated array
for (int i = 0; i < len; i++) {
Serial.print(data[i]);
Serial.print(" ");
}
Serial.println();
int x = 1;
int y = 0;
int h = 4;
int w = len;
barGraph(x, y, h, w, 0, 10, data);
}