#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define PIN_POT A2
#define PIN_BUTTON 7
//definieer toestanden
#define IDLE 0
#define CHANGE_SCALE 1
#define BLANK_SCREEN 2
// declaratie variabelen
byte aantalKol;
byte oudeAantalKol = 0;
byte stateLcd = IDLE;
// declareer functies
void tekenPotBar ();
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
uint8_t char0[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; //10000
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
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);
}
void loop() {
aantalKol = map(analogRead(PIN_POT), 0, 1023, 0, 80);
Serial.print(analogRead(A2));
Serial.print (" ");
Serial.print (map(analogRead(PIN_POT), 0, 1023, 0, 80));
Serial.print(" ");
Serial.println (map(analogRead(PIN_POT), 0, 1023, 0, 80) / 5);
//lcd.begin(1, 0);
switch (stateLcd) {
case IDLE: //nietsdoen
if (aantalKol != oudeAantalKol) {
//achtergrond aansteken
stateLcd = CHANGE_SCALE;
}// nieuwe status = change scale
if (PIN_BUTTON == 0) {
stateLcd = BLANK_SCREEN;
}
break;
case CHANGE_SCALE: // pot meter inlezen en schaal aanpassen
tekenPotBar ();
stateLcd = IDLE;
break;
case BLANK_SCREEN: //scherm leeg maken
lcd.clear();
delay(30);
stateLcd = IDLE;
break;
default:
break;
}
oudeAantalKol = aantalKol;
}
void tekenPotBar() {
Serial.print("tekenpotbar");
delay (100);
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));
}
}