#include #include #define outputA 2 #define outputB 3 int counter = 0; int aState, bState; int aLastState; String Medicine1 = "Panadol"; String Medicine2 = "Accutane"; String Medicine3 = "strepsils"; String Medicine4 = "Vicks"; String Medicine5 = "Vitamin C"; LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { pinMode (outputA,INPUT_PULLUP); pinMode (outputB,INPUT_PULLUP); Serial.begin (9600); // Reads the initial state of the outputA aLastState = digitalRead(outputA); //initialize lcd screen. screen messes up without this lcd.init(); // turn on the backlight lcd.backlight(); } void loop() { aState = digitalRead(outputA); // Reads the "current" state of the outputA // If the previous and the current state of the outputA are different, that means a Pulse has occured if (aState != aLastState){ // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise if (digitalRead(outputB) != aState) { counter ++; if ( counter > 30) counter = 0; } else { counter --; if ( counter < 0) counter = 30; } lcd.setCursor(0, 0); lcd.print("Med: "); if (counter >= 0 && counter < 7){ lcd.clear(); lcd.print("Medicine #1"); lcd.setCursor(0,1); lcd.print(Medicine1); } if (counter > 6 && counter < 13){ lcd.clear(); lcd.print("Medicine #2"); lcd.setCursor(0,1); lcd.print(Medicine2); } if (counter > 12 && counter < 19){ lcd.clear(); lcd.print("Medicine #3"); lcd.setCursor(0,1); lcd.print(Medicine3); } if (counter > 18 && counter < 25){ lcd.clear(); lcd.print("Medicine #4"); lcd.setCursor(0,1); lcd.print(Medicine4); } if (counter > 24 && counter < 31){ lcd.clear(); lcd.print("Medicine #5"); lcd.setCursor(0,1); lcd.print(Medicine5); } } aLastState = aState; // Updates the previous state of the outputA with the current state }