#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define uparrow 18
#define downarrow 5
#define submitBtn 17
char* input = "[{\"id\":1,\"name\":\"Anurag Biswal\",\"sic\":\"21becc17\",\"votes\":0},{\"id\":2,\"name\":\"Prithvi Mandal\",\"sic\":\"21bect34\",\"votes\":0}]";
//char* input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
JsonDocument doc;
String candidates[5];
String selectedCandidate;
int idx = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello, ESP32!");
pinMode(uparrow, INPUT_PULLUP);
pinMode(downarrow, INPUT_PULLUP);
pinMode(submitBtn, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello World");
delay(500);
lcd.clear();
deserializeJson(doc, input);
Serial.println(doc.size());
for(int i=0;i<doc.size();i++){
candidates[i] = doc[i]["name"].as<String>();
}
for(int i=0;i<doc.size();i++){
Serial.print(candidates[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
selectedCandidate = SelectionDisplay(candidates);
Serial.println(selectedCandidate);
delay(1000); // this speeds up the simulation
}
String readBtnState(int btns[]) {
String btnstate = "111";
for (uint8_t i = 0; i < 3; i++) {
btnstate.setCharAt(i, digitalRead(btns[i]) + '0');
}
return btnstate;
}
String SelectionDisplay(String options[]) {
int idx =0;
String btnState;
lcd.setCursor(0,0);
lcd.print("Select Mode");
int btns[] = {18,5,17};
while(1){
btnState = readBtnState(btns);
if(btnState == "011") idx=idx+1;
else if(btnState == "101") idx = idx-1;
else if(btnState == "110") break;
idx = idx%doc.size();
lcd.setCursor(0,1);
lcd.print(options[idx]);
delay(100);
}
}