Skip to content

Instantly share code, notes, and snippets.

@RenatoExpert
Created March 30, 2022 01:14
Show Gist options
  • Save RenatoExpert/5388ec5624f7dab37222cc996640c075 to your computer and use it in GitHub Desktop.
Save RenatoExpert/5388ec5624f7dab37222cc996640c075 to your computer and use it in GitHub Desktop.
Sensor level C++ code for Arduino - For Professor Rodrigo Icaro
/*
* Level Sensor C++ Code for Arduino
* Renato Araújo, 29 mar 2022
* renatorraraujo@gmail.com
* github: @renatoexpert
*/
/*
* NOTE
* Tinkercad eletronics are not working properly, since
* theres a bigger numbers of components and lines of code.
* The slideswitch is always ON by a bug. You may check it
* "cutting" off its wire.
*/
#include <LiquidCrystal.h>
#include <string.h>
// LCD SIZE
#define LCD_W 16
#define LCD_H 2
// LCD PORTS
#define LCD_RS 0
#define LCD_E 1
#define LCD_DB4 8
#define LCD_DB5 9
#define LCD_DB6 10
#define LCD_DB7 11
// SENSOR PORTS
#define S1 4
#define S2 5
#define S3 6
#define S4 7
// Clock-time in ms
#define CLOCK 2000
LiquidCrystal lcd (LCD_RS,
LCD_E,
LCD_DB4,
LCD_DB5,
LCD_DB6,
LCD_DB7
);
int level;
void setup () {
lcd.begin (LCD_W, LCD_H);
/* // Debug
* // Uncomment the following line to debug,
* // It may cause errors on LCD screen rendering
* Serial.begin (9600);
*/
}
// Read sensors and update level status
void set_level () {
if (digitalRead(S4)) {
level = 100;
} else if (digitalRead(S3)) {
level = 75;
} else if (digitalRead(S2)) {
level = 50;
} else if (digitalRead(S1)) {
level = 25;
} else {
level = 0;
}
}
void render_lcd () {
lcd.clear();
// Print out "Reservatory" on LCD
String reservatorio = "Reservatorio";
lcd.setCursor (centralizar (reservatorio), 0);
lcd.print (reservatorio);
// Print out its level on the downside line
auto levelstr = "Nivel: " + String (level) + "%";
lcd.setCursor (centralizar (levelstr), 1);
lcd.print (levelstr);
}
// Calculates the space needed in the left for a term,
// in order to centralize it on LCD
int centralizar (auto termo) {
int space = (LCD_W - termo.length())/2;
return space;
}
void loop () {
set_level();
render_lcd ();
delay(CLOCK);
}
@RenatoExpert
Copy link
Author

Screenshot from 2022-03-29 22-16-03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment