#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define PIN_POT A2
#define PIN_BUT 7
// States
#define IDLE 0 // wachten / inactief
#define CHANGE_SCALE 1
#define BLANK_SCREEN 2
// Functions
void drawBar();
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
uint8_t char0[8] = {0, 0, 0, 0, 0, 0, 0, 0 }; // 00000
uint8_t char1[8] = {16, 16, 16, 16, 16, 16, 16, 16}; // 10000
uint8_t char2[8] = {24, 24, 24, 24, 24, 24, 24, 24}; // 11000
uint8_t char3[8] = {28, 28, 28, 28, 28, 28, 28, 28}; // 11100
uint8_t char4[8] = {30, 30, 30, 30, 30, 30, 30, 30}; // 11110
uint8_t char5[8] = {31, 31, 31, 31, 31, 31, 31, 31}; // 11111
byte aantalKol;
int oldPotVal = 0;
byte stateLcd = IDLE; // ik steek '0' in de state
void setup() {
Serial.begin(9600);
pinMode(A2, INPUT);
// Init
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(2, 1);
lcd.print("Wokwi Rocks!");
lcd.createChar(0, char0);
lcd.createChar(1, char1);
lcd.createChar(2, char2);
lcd.createChar(3, char3);
lcd.createChar(4, char4);
lcd.createChar(5, char5);
lcd.begin(1, 0);
/*
lcd.print(char(0));
lcd.print(char(1));
lcd.print(char(2));
lcd.print(char(3));
lcd.print(char(4));
*/
}
void loop() {
switch (stateLcd) {
case IDLE: // 0 // wachten / inactief
if (analogRead(PIN_POT) != oldPotVal) {
// achtergrondlicht aan
stateLcd = CHANGE_SCALE;
oldPotVal = analogRead(PIN_POT);
}
if (!digitalRead(PIN_BUT)) {
stateLcd = BLANK_SCREEN;
}
break;
case CHANGE_SCALE: // 1
drawBar();
stateLcd = IDLE;
break;
case BLANK_SCREEN: // 2
lcd.clear();
delay(30); // delay voor het bouncen op te vange
stateLcd = IDLE;
break;
default:
Serial.println("Error");
}
}
void drawBar() {
Serial.println(analogRead(A2)); // PIN_POT
aantalKol = map(analogRead(PIN_POT), 0, 1023, 0, 80);
lcd.setCursor(0, 0);
for (int i = 0 ; i < aantalKol / 5 ; i++) {
lcd.print(char(255));
}
lcd.print(char(aantalKol % 5));
for (int i = aantalKol / 5 ; i < 16 ; i++) {
lcd.print(char(254));
}
}