Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *************************************************************************
- // Name : mouse_encoder *
- // Author : Tarilayefa Edward *
- // Notice : Tari Electronics & Embedded Systems (TEES), 2018. *
- // : tarielectronics@yahoo.com *
- // : tarielect.edward@gmail.com *
- // : +23408184754883,+23408062251186, Nigeria. *
- // : https//:web.facebook.com/groups/teestraining/ *
- // : https//:web.facebook.com/groups/picarduino/ *
- // Date : 18/07/2018 *
- // Version : 1.0 *
- // Notes : A program to read mouse encoder & decode values on lcd & *
- // : serial monitor. *
- // Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx *
- // Compiler : avrgcc *
- // IDE : Arduino 1.8.5 *
- // Target : Atmega 328, Arduino Uno, Nano. *
- //*************************************************************************
- //#include <LiquidCrystal.h> //add the lcd library
- //LiquidCrystal lcd(7,6,5,4,3,2); //(rs,en,d4,d5,d6,d7)
- # include <LiquidCrystal_SR.h> //add 2/3 wire lcd library
- LiquidCrystal_SR lcd(8,9); //(data,clock)
- //------------------------------------------------------------------------
- int EncbtnA = 2; //encoder button switch A
- int EncbtnB = 3; //encoder button switch B
- int lastState = 0;
- int steps = 0; //encoder count steps (0 - 255)
- int cw = 0; //encoder direction (-1, 0, 1)
- int AState = 0;
- int BState = 0;
- int State = 0; //state (0, 1, 2, 3)
- int clearBtn = 10;
- //------------------------------------------------------------------------
- //------------------------------------------------------------------------
- void setup() //setup function
- {
- pinMode(EncbtnA, INPUT_PULLUP);//set pin to input mode
- pinMode(EncbtnB, INPUT_PULLUP);
- pinMode(clearBtn, INPUT_PULLUP);
- //------------------------------------------------------------------------
- lcd.begin(16,20); //initialize 16x2 char lcd
- lcd.clear(); //clear screen
- delay(1000); //wait for 1s
- //------------------------------------------------------------------------
- Serial.begin(9600);
- lcd.print(" Mouse Encoder ");
- Serial.println("Mouse Encoder");
- delay(3000); //wait for 3s
- }
- //------------------------------------------------------------------------
- //------------------------------------------------------------------------
- void loop() //main loop function
- {
- int clearState = digitalRead(clearBtn);
- getEncoder();//call encoder decode function
- if(clearState == 0){
- steps = 0;
- }
- //------------------------------------------------------------------------
- Serial.print("Mouse Encoder Steps: ");
- Serial.println(steps);
- //------------------------------------------------------------------------
- lcd.setCursor(0,1);
- lcd.print("Steps: ");
- lcd.print(steps);
- lcd.print(" ");
- }
- //------------------------------------------------------------------------
- //------------------------------------------------------------------------
- void getEncoder(){ //encoder decode function
- //read the encoder input pin:
- AState = digitalRead(EncbtnA); //read encoder buttonA
- BState = digitalRead(EncbtnB) << 1; //read encoder buttonB
- State = AState | BState; //read encoder button state
- //------------------------------------------------------------------------
- if (lastState != State){
- switch (State) {
- case 0: //encoder sequence 0
- if (lastState == 2){
- steps++;
- cw = 1;
- }
- else if(lastState == 1){
- steps--;
- cw = -1;
- }
- break;
- //------------------------------------------------------------------------
- case 1: //encoder sequence 1
- if (lastState == 0){
- steps++;
- cw = 1;
- }
- else if(lastState == 3){
- steps--;
- cw = -1;
- }
- break;
- //------------------------------------------------------------------------
- case 2: //encoder sequence 2
- if (lastState == 3){
- steps++;
- cw = 1;
- }
- else if(lastState == 0){
- steps--;
- cw = -1;
- }
- break;
- //------------------------------------------------------------------------
- case 3: //encoder sequence 3
- if (lastState == 1){
- steps++;
- cw = 1;
- }
- else if(lastState == 2){
- steps--;
- cw = -1;
- }
- break;
- //------------------------------------------------------------------------
- }
- }
- //------------------------------------------------------------------------
- lastState = State; //set encoder state
- //------------------------------------------------------------------------
- delay(1); //wait for 1ms
- }
- //------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement