/**
  Arduino Uno PIND register demo

  https://wokwi.com/arduino/projects/314168546236039745

  Copyright (C) 2021, Uri Shaked.
*/

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BUTTON_PIN 12

void setup() {
  Serial.begin(200000);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lcd.init();
  lcd.backlight();
  //lcd.println("PIND value:");

  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
}

  //int b7 = digitalRead(0);
  //Serial.print(b7);
int value;
int value2;
int value3;
int value4;
int value5;
boolean out = false;
boolean notShow = false;
int counter = 0;
int counterinput = 1;

void convertToBinary(int count, int a)
{

    if (count > 1){
        convertToBinary(count - 1, a / 2);
    }
    if(count == 17){
      lcd.setCursor(0, 1);
    }

    lcd.print(a % 2);
}


void loop() {
  if(!out){
    lcd.setCursor(0, 0);
    lcd.print("Input");
    lcd.print(counterinput);
    lcd.print(":\n");
    //lcd.print(PIND);
    convertToBinary(8, PIND);
  }else if(out && !notShow){
    int out_put = ((value + value2) * value3 + value4) * value5;
    lcd.clear();
    lcd.setCursor(0, 0);
    //lcd.print(out_put);
    convertToBinary(32, out_put);
    notShow = true;
  }
  delay(100);
  if(digitalRead((BUTTON_PIN)) == LOW){
    if(counter == 0){
      value = PIND;
      counter++;
      counterinput++;
    }
    else if(counter == 1){
      value2 = PIND;
      counter++;
      counterinput++;
    }
    else if(counter == 2){
      value3 = PIND;
      counter++;
      counterinput++;
    }
    else if(counter == 3){
      value4 = PIND;
      counter++;
      counterinput++;
    }
    else if(counter == 4){
      value5 = PIND;
      out = true;
    }
  }

  //if (PIND != value) {
    //lcd.setCursor(6, 1);
    //lcd.print(PIND);
    //lcd.print("   ");
    //value = PIND;
  //}
}