#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define set0(r, b) r &= ~(1<<b)
#define set1(r, b) r |= (1<<b)
#define test(r, b) r & (1<<b)
void setup() {
lcd.init();
lcd.backlight();
set1(ADMUX, ADLAR); // allign right
set1(ADCSRA, ADEN); // enable ADC
set1(ADCSRA, ADPS0); // set conversion to n/128
set1(ADCSRA, ADPS1); //
set1(ADCSRA, ADPS2); //
}
void loop() {
set1(ADMUX, 0); // use A2
set1(ADCSRA, ADSC); // start ADC conversion
while(test(ADCSRA, ADIF) == 0); // wait till conversion finishes
int pos1 = (ADCH<<2) | (ADCL>>6);
pos1 = map(pos1, 0, 1023, 0, LCD_COLUMNS);
for (int i = 0; i < pos1; i++) {
lcd.setCursor(i,0);
lcd.print('X');
}
for (int j = pos1; j < LCD_COLUMNS; j++) {
lcd.setCursor(j,0);
lcd.print(' ');
}
set0(ADMUX, 0); // use A1
set1(ADCSRA, ADSC); // start ADC conversion
while(test(ADCSRA, ADIF) == 0); // wait till conversion finishes
int pos2 = ADCH;
pos2 = map(pos2, 0, 255, 0, LCD_COLUMNS);
for (int i = 0; i < pos2; i++) {
lcd.setCursor(i,1);
lcd.print('X');
}
for (int j = pos2; j < LCD_COLUMNS; j++) {
lcd.setCursor(j,1);
lcd.print(' ');
}
}