Skip to content

Instantly share code, notes, and snippets.

@RenatoExpert
Created March 30, 2022 20:12
Show Gist options
  • Save RenatoExpert/0beaf4178020b3b06776d29b69e4a1ae to your computer and use it in GitHub Desktop.
Save RenatoExpert/0beaf4178020b3b06776d29b69e4a1ae to your computer and use it in GitHub Desktop.
Sensor level with Pump 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>
// MOTOR CONTROL
#define MOTOR_PORT 12
// 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 5000
LiquidCrystal lcd (LCD_RS,
LCD_E,
LCD_DB4,
LCD_DB5,
LCD_DB6,
LCD_DB7
);
int level;
void setup () {
pinMode (MOTOR_PORT, OUTPUT);
lcd.begin (LCD_W, LCD_H);
// lcd.autoscroll ();
/* // 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();
String upstr = "Default";
String levelstr = "Default";
int space = 0;
switch (level) {
case 100:
upstr = "Erro:";
levelstr = "Nivel excedido";
break;
case 75:
upstr = "Nivel";
levelstr = "Maximo";
space = 5;
break;
case 50:
upstr = "Nivel";
levelstr = "Intermediario";
space = 3;
break;
case 25:
upstr = "Nivel";
levelstr = "Minimo";
space = 5;
break;
case 0:
upstr = "Erro:";
levelstr = "Tanque vazio";
break;
default:
upstr = "Erro:";
levelstr = "Erro interno";
}
lcd.setCursor (6, 0);
lcd.print (upstr);
lcd.setCursor (space, 1);
lcd.print (levelstr);
}
void update_pump () {
if (level == 100) {
digitalWrite (MOTOR_PORT, LOW);
} else {
digitalWrite (MOTOR_PORT, HIGH);
}
}
void loop () {
set_level();
render_lcd ();
update_pump ();
delay(CLOCK);
}
@RenatoExpert
Copy link
Author

Screenshot from 2022-03-30 17-14-49

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