-
1Run a Program
/*
*
*
* Power LCD and Rotary encoder from the +5V pin of Arduino
* LCD RS -> pin 7
* LCD EN -> pin 6
* LCD D4 -> pin 5
* LCD D5 -> pin 4
* LCD D6 -> pin 3
* LCD D7 -> pin 2
* Encoder Switch -> pin 10
* Encoder Output A -> pin 9
* Encoder Output B -> pin 8
*/int Encoder_OuputA = 9;
int Encoder_OuputB = 8;
int Encoder_Switch = 10;int Previous_Output;
int Encoder_Count;#include //Default Arduino LCD Librarey is included
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //Mention the pin number for LCD connection
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);void setup() {
lcd.begin(16, 2); //Initialise 16*2 LCDlcd.print(" Rotary Encoder "); //Intro Message line 1
lcd.setCursor(0, 1);
lcd.print(" With Arduino "); //Intro Message line 2delay(2000);
lcd.clear();//pin Mode declaration
pinMode (Encoder_OuputA, INPUT);
pinMode (Encoder_OuputB, INPUT);
pinMode (Encoder_Switch, INPUT);Previous_Output = digitalRead(Encoder_OuputA); //Read the inital value of Output A
}void loop() {
//aVal = digitalRead(pinA);
if (digitalRead(Encoder_OuputA) != Previous_Output)
{
if (digitalRead(Encoder_OuputB) != Previous_Output)
{
Encoder_Count ++;
lcd.clear();
lcd.print(Encoder_Count);
lcd.setCursor(0, 1);
lcd.print("Clockwise");
}
else
{
Encoder_Count--;
lcd.clear();
lcd.print(Encoder_Count);
lcd.setCursor(0, 1);
lcd.print("Anti - Clockwise");
}
}Previous_Output = digitalRead(Encoder_OuputA);
if (digitalRead(Encoder_Switch) == 0)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Switch pressed");
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.