/*
// FULL TUTORIAL: https://youtu.be/upE17NHrdPc
// Links related to this project:
// Arduino UNO - https://s.click.aliexpress.com/e/_AXDw1h
// Arduino breadboard prototyping shield - https://s.click.aliexpress.com/e/_ApbCwx
// 16x2 displays with IIC - https://s.click.aliexpress.com/e/_9Hl3JV
// 16x2 display with RGB backlight - https://s.click.aliexpress.com/e/_9wgpeb
// original sketch from YWROBOT - https://wokwi.com/arduino/libraries/LiquidCrystal_I2C/HelloWorld
// character creator - https://tusindfryd.github.io/screenduino/
// another character creator - https://maxpromer.github.io/LCD-Character-Creator/
// sprintf explanation - https://www.programmingelectronics.com/sprintf-arduino/
// custom characters simplest project - https://wokwi.com/projects/294395602645549578
// Arduino I2C scanner - https://playground.arduino.cc/Main/I2cScanner/
*/
#include <Wire.h> //This library allows you to communicate with I2C/TWI devices
#include <LiquidCrystal_I2C.h>
// set the i2c address to 0x27
// 20 chars and 4 lines display
LiquidCrystal_I2C lcd( 0x27,20,4 );
// define custom characters - every character is 5x8 "pixels"
byte gauge_left[8] = { B11111, B10000, B10000, B10000, B10000, B10000, B10000, B11111}; // left part of empty gauge [
byte gauge_center[8] = { B11111, B00000, B00000, B00000, B00000, B00000, B00000, B11111}; // center part of empty gauge _
byte gauge_right[8] = { B11111, B00001, B00001, B00001, B00001, B00001, B00001, B11111}; // right part of empty gauge ]
byte gauge_fill[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111}; // filled gauge █
int cpu_gauge; // value for the CPU gauge
int gpu_gauge; // value for the GPU gauge
char buffer[10]; // helper buffer to store C-style strings (generated with sprintf function)
const float gauge_step = 100.0/8.0;
void setup() {
cpu_gauge = 45; // set "random" value for CPU gauge
gpu_gauge = 70; // set "random" value for GPU gauge
// put your setup code here, to run once:
//Serial1.begin(115200);
//Serial1.println("Hello, Raspberry Pi Pico!");
Wire.setSDA( 8 );
Wire.setSCL( 9 );
//Turning the LCD on and getting it ready to show the time and date.
lcd.init();
lcd.createChar(0, gauge_left); // create special character on position 0, gauge left [
lcd.createChar(1, gauge_center); // create special character on position 1, gauge center _
lcd.createChar(2, gauge_right); // create special character on position 2, gauge right ]
lcd.createChar(3, gauge_fill); // create special character on position 3, gauge fill █
lcd.backlight();
lcd.setCursor( 0, 0 );
lcd.print( "------- Time -------" );
lcd.setCursor( 0, 2 );
lcd.print( "--- CurrentMonth ---" );
}
void writeGauge(uint row, int gauge_value) {
for (int i=0; i<8; i++) {
if (gauge_value <= gauge_step*i) { // value is smaller than step*i, draw "empty" character
if (i==0) {lcd.write(0);} // [ first cell, opening bracket
else if (i==7) {lcd.write(2);} // ] last cell, closing bracket
else {lcd.write(1);} // _ any other cell, lines top and bottom
}
else { // value is bigger than step*i, draw filled character - █
lcd.write(3);
}
}
}
void loop() {
// CPU gauge
lcd.setCursor(0,1); // move cursor to top left
sprintf(buffer, "CPU:%3d%%", cpu_gauge); // set a string as CPU: XX%, with the number always taking at least 3 character
lcd.print(buffer); // print the string on the display
writeGauge(2, cpu_gauge);
/*
// 8 characters to draw the gauge --- ████___]
float cpu_gauge_step = 100.0/8.0; // 100% is the maximum number, gauge is 8 characters wide, calculate one step
for (int i=0; i<8; i++) {
if (cpu_gauge <= cpu_gauge_step*i) { // value is smaller than step*i, draw "empty" character
if (i==0) {lcd.write(0);} // [ first cell, opening bracket
else if (i==7) {lcd.write(2);} // ] last cell, closing bracket
else {lcd.write(1);} // _ any other cell, lines top and bottom
}
else { // value is bigger than step*i, draw filled character - █
lcd.write(3);
}
}
*/
// GPU gauge
lcd.setCursor(0,3); // move cursor to second line
sprintf(buffer, "GPU:%3d%%", cpu_gauge); // set a string as GPU: XX%, with the number always taking at least 3 character
lcd.print(buffer); // print the string on the display
writeGauge(3, cpu_gauge);
/*
// 8 characters to draw the gauge --- [______]
float gpu_gauge_step = 100.0/8.0; // 100% is the maximum number, gauge is 8 characters wide, calculate one step
for (int i=0; i<8; i++) {
if (gpu_gauge <= gpu_gauge_step*i) { // value is smaller than step*i, draw "empty" character
if (i==0) {lcd.write(0);} // [ first cell, opening bracket
else if (i==7) {lcd.write(2);} // ] last cell, closing bracket
else {lcd.write(1);} // _ any other cell, lines top and bottom
}
else { // value is bigger than step*i, draw filled character - █
lcd.write(3);
}
}
*/
// increase the CPU value, set between 0-100
cpu_gauge = cpu_gauge +1;
if (cpu_gauge > 100) {cpu_gauge = 0;}
// incrase the GPU value, set between 0-100
gpu_gauge = gpu_gauge +1;
if (gpu_gauge > 100) {gpu_gauge = 0;}
delay(100); // wait for a while - 100ms = update the screen 10x in a second
}
void loop2() {
// put your main code here, to run repeatedly:
delay(1000); // this speeds up the simulation
}